- Home /
how to use raycasting for enemy sight (C#)?
The issue im having is that my enemy can see through walls. Ive been trying to do this for hours and cant quite get it working. Ive seen several other posts about raycasting, but alot of them are outdated or using javascript. Ive tried just regular raycasting to see if it can hit the player, but it results with the enemy seeing him through the walls. I also tried making an array of all the objects that are hit and then checking to see if the player is closer than the others, but this seemed to make the enemy glitch out to See the player and not see the player every frame. Does anybody know how to do this?
as of right now this is what i have
RaycastHit[] hits;
hits = Physics.RaycastAll (transform.position + Vector3.up * 1.5f, transform.forward, sightDistance);
for (int i = 0; i < hits.Length; i++) {
if (hits [i].collider.gameObject.Equals (player)) {
Debug.Log ("I C YOU");
animator.SetBool ("isAlert", true);
playerDistance = Vector3.Distance (transform.position, player.transform.position);
} else {
otherDistance = Vector3.Distance (transform.position, hits [i].transform.position);
}
}
if (otherDistance < playerDistance)
{
animator.SetBool ("isAlert", false);
Debug.Log ("NOPE");
}
}
}
This is the closest i can get to making it work. the enemy sees the player and starts shooting, then immediately stops seeing the player even when hes right in front of him. i need him to see the player when hes in the line of sight, and need him to stop seeing th eplayer if he moves behind a wall
Can we see your code? Raycast should stop at the walls provided the walls have a collider. Raycast all will go straight through the walls and there is a chance the returned array of colliders hit will not be in the order that they were actually hit.
Answer by Jwizard93 · Jul 13, 2017 at 05:13 PM
Use Raycast instead of Raycast all. Raycast all goes through everything. and returns everything that is hit. You are iterating through everything that was hit to see if the player is in there and he always will be provided he is within sightDistance. .Make sure obstacles also have colliders.
I tried regular raycasts and he was having difficulty even seeing the player. I need him to be able to detect walls in case the player walked behind one. The problem im having with this script is that the enemy thinks that the walls are closer than the player even though they are behind him
In you original attempt is it possible that there are walls both between the player and the enemy and walls behind the player but still within sight distance? In that event otherDistance will be set to the distance of the furthest wall hit and the player will still be targeted.
When you tried using just Raycast ins$$anonymous$$d of RaycastAll were you hitting the enemy itself? It may be best to start the ray at a position a little bit in front of the enemy and outside his own collider.
Answer by pyroantimonic_entity · May 14, 2018 at 11:33 PM
https://www.youtube.com/watch?v=xppompv1DBg&t=147s Watch the whole video
Answer by Ex6tra · Jun 10, 2020 at 01:37 PM
I think your problem comes from RaycastAll returning arrays that are not in order. you should use something like System.Array.Sort(hits, (x, y) => x.distance.CompareTo(y.distance));
to order your RaycastAll array and then check if the first Hit is on the player.