unity3d - Unity:敌人检查前的玩家不起作用,Physics.Raycast?
问题描述
这个游戏场景有两个用例,如下:
玩家无法被敌人看到:
玩家可以被敌人看到:
我正在使用以下代码来决定玩家和敌人之间的视线是否清晰,敌人可以攻击,即只有在玩家和敌人之间没有墙壁或任何其他碰撞器时,敌人才应该攻击:
void playerInRangeOfEnemy()
{
// After checking player is in range of the enemy, check for the line of vision between them
Transform Player = FindObjectOfType<Player>().transform;
Vector2 rayDirection = Player.position - transform.position; // --> transform.position is of the enemy
RaycastHit hit;
if(Physics.Raycast(transform.position, rayDirection, out hit, minAttackDistance))
{
Debug.Log("Ray hitting the player"); // Not logging in either of the two cases mentioned above
if (hit.collider.gameObject.CompareTag("Player"))
{
// The following line is also not being logged
Debug.Log("Make the enemy attack the player");
}
}
}
敌人和玩家都是动态刚体。minAttackDistance
只是我要投射的光线投射的长度,现在设置为 100 以进行测试。
如何使条件为真以便执行两个日志?上面的代码有什么更新吗?
解决方案
您使用的是 3d 物理而不是 2d。请改用 Physics2D.Raycast。
推荐阅读
- typeorm - 为什么 TypeORM 在这个简单的查询中不返回任何记录?
- opencv - 深度学习 - 如何执行 RANDOM CROP 并且不丢失数据中的任何信息(更改地面实况标签)
- javascript - WebGL2 draw multiple textures in one draw call
- c# - 如何在c#中使用具有相同名称但不同属性和结构的元素反序列化XML
- android - Android - 从 Google Play 商店下载的应用程序在开发过程中无法正常运行
- python - keras自定义图层的输出形状
- javascript - 在 Nodejs 中托管多个网页
- spring - 为什么 Spring KafkaTemplate 没有将其实例变量“messageConverter”设置为 volatile?
- google-cloud-platform - 收到错误“消息”:“KMS 密钥不包含位置。” 在数据流作业中使用 withKmsKey() 选项时
- python - Python中的print(f'和f'有什么区别?