- Home /
Raycast stealth enemy?
Hello !, I tell you that I am already finishing the enemy of my FPS but I found an error, lack the range and angle of vision (only forward) so that when you are behind a wall, do not see me, but if I leave this alter, I already know how to do it, but the very simple system even of raycasting is not how it is handled, does anyone have any line of code that serves me? Thank you!
Answer by JonPQ · Jan 23, 2019 at 04:57 PM
pseudocode... (for a left/right horizontal field of view check, once you learn how this works, you can do another check for up/down field of vision).
List item
1) make a vector from ai to player Vector3 look = (player_Pos - ai_Pos).normalized;
List item
2) take ai's transform.forward vector
List item
3) set y=0 for both vectors, and re-normalize both. (so we jsut do x/z plane check), for left/right field of vision check....
List item
4) do a Vector3.dotProduct(vec1,vec2) this gives you the cosine of the view angle between Ai's forward vector, and the vector towards the player. (a single value representing angle) result of 1= looking exactly at player. 0= 90 degrees, -1 = looking backwards (180 degrees)
List item
so now you can just compare that result with cos on angle you want to set for the AI's view angle... If you want your AI to have 180 degree vision just check if the dotProduct result is >0 to see if they can potentially see the player. Then add a raycast if you want to see if obscured by other geometry.
List item
The dotProduct check is a good first optimization also, to do before doing the raycast.
Your answer
Follow this Question
Related Questions
Raycast radius 1 Answer
Enemy line of sight 1 Answer
How to stop enemies from shooting each other 1 Answer
Raycast target sight? 0 Answers
Can someone help me to understand why this raycast is not working properly? 1 Answer