2D enemy Field of Vision script

Hi, I want to make a script that gives a enemy a field of vision, if the player steps into this field of vision, an alarm is triggered. Its for a 2D platformer, Here’s an image to illustrate what I mean. The field of vision will also need to move up and down as it is a camera type enemy.

Any ideas on how I would make a script that does this?


I have no idea what you mean by a “camera type enemy” and why that means it has to move up and down. But if you can define a vector for where the “eyes are,” finding out if an object is in the field of vision is simple. For example, say you were using transform.right for your eye position/direction, you could do something like:

if (Vector3.Angle(transform.right, playerPosition - EnemyPosition) < 22.5) {
    Debug.Log("I'm seeing the player");

Note Vector3.Angle returns an unsigned value. Also this is the angle from the vector and the code measures to each side, so the actual field of vision would be 45 degress.