- Home /
Checking For Player Distance In Front Only
Im setting up a pathfinding enemy AI that will check for distance of the player. That part I have no issue with. What Im unsure about is how to only check in front of the enemy. Anyone know the best way to go about that?
did you try with the mathf rotation ranges for ray?
well don't understand everything and I'd propably need 4 hours to set that but my concept of working toards your problem is this
Answer by Loius · Jan 22, 2013 at 05:19 AM
Check the angle of (playerPosition-AIposition) and (aiTransform.forward).
if ( Vector3.Angle(ai.forward, vectorToPlayer) > sightAngle ) { // 'can't see player'
Vector3 targetDir = target.transform.position - transform.position; Vector3 forward = transform.forward; float angle = Vector3.Angle(targetDir, forward); if (angle < 60.0f)
This game is straight over head, so essentially its 2D.
This is what Im using. It works somewhat...If Im behind the enemy at top side when the enemy is moving downward, they do not see me. But, if Im behind them at bottom side, heading upward, they will see me behind them. If I set the angle > 60, it will be the exact opposite.
Vector3.Angle returns the angle in degrees between two vectors.
Your AI sees along "AI.transform.forward" by definition (that's its forward facing, unless you're working with Y+ as 'not up').
The direction from X to Y is always (Y-X), so from your AI to your player is (player.transform.position - AI.transform.position).
If the angle between those vectors is zero, your AI is staring directly at your player. At angle==180, the player is directly behind the AI, and at angle==90, the player is directly beside the AI, but you can't tell which side (without more math).
Add this to your AI's update and it'll draw debug lines indicating the AI's visual range on the X/Z plane, which will help you debug why you're being seen when you shouldn't be:
var rot : Quaternion = Quaternion.Euler(0,60,0);
var dir : Vector3 = transform.forward * 20;
Debug.DrawLine(transform.position, transform.position + rot*dir);
Debug.DrawLine(transform.position, transform.position + (-rot)*dir);
If your guy doesn't rotate then you need to use whatever direction you've stored as its 'facing direction' in place of the .forwards I've been using. I use this myself for simple AI and it works for me.
The angle is more information than you need. Check the sign of the dot product (AI.transform.forward, vector from ai to player). Positive means the player is in front of the AI, negative means he is behind.
If you want your enemy to have a field of view however, you can either use the angle, or normalize the vectors for the dot product and check if it's > something between 0 and 1. Re$$anonymous$$der : angle = acos(A · B)
Your answer
Follow this Question
Related Questions
How would I confine an object to the ground? 2 Answers
Need help with basic AI script. 2 Answers
focusing on a unit during unit detection in an RTS 1 Answer
How to avoid enemies walk up in the air 2 Answers
Following the next item in an array 0 Answers