Facing an enemy

Hello there,
I want to create a system for detecting if is player facing enemy.
I made this picture, it should help you better understand my problem.

20536-scr.jpg

I was searching around here and I don’t find something useful.
My only idea was creating some king of invisible triangle and detecting when is triangle in collision with something…
If you have better or any other ideas and suggestions, help me please. :slight_smile:

Bye, Marshall.

You know the Vector3 transform.position of your player, and of each of your enemies, right? So you can work out the vector from your player to the enemy (enemy.transform.position - player.transform.position). You also know the player’s facing direction, which is given by player.transform.forward. So you can use Vector3.Angle to calculate the angle between these two vectors.

You can also work out the distance to each enemy using Vector3.Distance from enemy.transform.position and player.transform.position.

You then test these two values: if the angle between the vector to the enemy and the player’s facing direction is less than the player’s field of vision, and if the distance to the enemy is less than the player’s focal distance, then the enemy is within view.

I will suggest you to make the triangular shape with the help of boxcolliders . And you can get the enemy in OnTriggerEnter() and in onTriggerEnter you can get the position of your enemy . now use LookAt() function on your player to face the enemy.

Another option will be that you can draw a triangle with three box collider placed as three sides of the imaginary triangle. And use the OnTriggerEnter() function which will tell you that enemy hit the triangle boundary, now check the distance of your enemy from your player, if it is less than from range distance than use LookAt() function to make the player to face towards enemy.