我遇到了RayCast2D的问题。阅读完文档后,它应该可以工作,但我似乎无法弄清楚我做错了什么。下面的代码旨在阻止NPC在其与目标之间存在墙壁时进行射击,但是它仍然完全忽略了墙壁而只是射击它们。我最好的猜测是它的某种逻辑问题,但我无法弄清楚什么是错的。
//Check has line of sight on target
RaycastHit2D hit = Physics2D.Raycast(transform.position, transform.right, sleepDistance); //Uses raycast to see whats in front of player
if (hit)
{
if (!hit.collider.transform.CompareTag("Obstacle"))//Checks if wall is not in front of player
transform.GetChild(0).SendMessage("fireGun", SendMessageOptions.DontRequireReceiver); //Fires gun if wall is not in front of player
else
Debug.Log("Cast: " + hit.transform.tag); //Debug to see when wall is in front of player, never triggered.
}
我已仔细检查以确保墙壁都已正确标记,并且拼写正确且一切。
有什么想法吗?提前谢谢。