- Home /
Raycast radius
I'm trying to make a stealth game and am trying to code in a raycast for the guard vision so that it's a cone (Or at least, spread out over a certain radius) and will also not see the player through walls. I've looked up some documentation and other questions, and one suggested capsule cast. Is this the only way to achieve this effect?
Answer by robertbu · Oct 16, 2014 at 12:11 AM
Three step process:
Check the distance. If it is too far, the player cannot be seen
Use the forward vector at the 'eyes' and the vector between the eyes and the player in Vector3.Angle(). If the angle is bigger than the angle of view of the enemy, then the player cannot be seen.
Raycast() or better yet Linecast() between the eyes and the player to determine if there is something is blocking. How many raycast will depend on your game. The more casts, the better the detection. Placing some empty game objects on the player to use as casting targets can help. Using the corners of the mesh.bounds can work, but it will produce some false positives.
Note that a capsule cast will not work since it does not spread out as the distance increases. Plus a hit by a capsule cast will not tell you if the player can be seen...just that something is partially blocking the player.
Your answer
Follow this Question
Related Questions
Raycast stealth enemy? 1 Answer
Enemy Ai Field of View 0 Answers
raycast hit not detecting && Nav mesh agent ai stealth 1 Answer