- Home /
RayCast2D not returning values as expected
Hey all,
I'm having an issue with the RayCast2D. After reading the documentation it should be working, but I can't seem to figure out what I'm doing wrong. The code below is designed to stop the NPC from shooting if there is a wall between it and its target, however its still ignoring the walls completely and just shooting at them. My best guess is that its a logic issue of some kind but I can't figure out whats wrong.
//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.
}
I've double checked to make sure the walls are all properly tagged, and that its spelled right and everything.
Any ideas? Thanks in advance.
$$anonymous$$ay seem obvious, but do the walls have colliders?
well does the debug print the correct hits? Try drawing your raycasts so you can see if they actually hit a wall (maybe sleepDistance does not reach the wall?)
Your answer
Follow this Question
Related Questions
AI raycasting problem 0 Answers
Bug/Error in script CollectPapers 0 Answers
Raycast detection / lag issue 0 Answers
180 Degree raycast 1 Answer