- Home /
Check if player is in enemys fov?
Hey there, i'd like to let my enemy start chasing the player when he is in his fov (~180°). The enemy is flying btw.
I read about using Physics.OverlapSphere
Anyone got a solution for this? I don't want to use triggers because it really annoying adding all those triggers to my different kind of enemies.
My idea is making an 180° raycast on the ground (from the position of the enemy) and check if the player got hitted too! If true the enemy should directly face the player and send out a standard raycast to check if there is an object between both (the player is hidden).
But how to make a 180° hit check?
Greets
it's way easier since you're ai$$anonymous$$g for 180°. do enemy.transform.InverseTransformPoint(player.transform.position). if the resulting z value is larger than 0 the player is in front of the enemy.
It's basically how you see a position in another's view, like sitting in a class and calculating where the $$anonymous$$cher in once own relative space is.
Answer by ThisGuyThatsHere · Feb 23, 2017 at 10:27 PM
I would just get the vector from enemy's position to player's position (eV - pV, or reverse, always getting confused there) and then check angle: Vector3.Angle(deltaV, enemy.transform.forward);
And that's it, you've got the angle from enemy's view to perfect view at player.
It's reverse ^^. A vector is like an arrow. You always do "Head $$anonymous$$us Tail" or "To $$anonymous$$us From". Since you want the vector from your enemy to your player you have to do "pV - eV".
Yeah it's reverse. Vector3.angle works perfectly! Thank you :)
Vector3 targetDir = player.position - transform.position;
float angleToPlayer = (Vector3.Angle(targetDir, transform.forward));
if (angleToPlayer >= -90 && angleToPlayer <= 90) // 180° FOV
Debug.Log("Player in sight!");
idk why but this exact method doesn't work for me, I even copied your code here but it still won't work
Your answer

Follow this Question
Related Questions
how can i make my enemy face player as well as be parallel to the ground with smooth rotation? 1 Answer
Non uniform movement radius 1 Answer
How to make "enemy" have a frustum(cone) view and not a circle radius 2 Answers
Chase script. I added a FOV now he won't engage 0 Answers
How to make a Raycast enemy? 0 Answers