# Field of view for an enemy

Im trying to make a metal gear style project, and ive got my enemies arround waypoints, they rotate and face each one while they walk, pretty neat and its working just fine no errors nothing… since i need them to be able to spot you when you cross their field of view i need help to make them have one … sooo ? what should i create to be used as a field of view… im quite new to programming so if you could please explain carefully i will appreciate that , thanks
Also i need to make a function repeat until the transform.position of the object is equal to another… is there any way to do that? i tried do while and everything i tested out with that just crashed or freezed the game…

The average human has a FOV of approximately 160degrees.

Regarding the repeating until the positions match… Just compute a vector that moves the one point closer to the other at each frame. So, if one point was A=(0,0,0), and B=(10,0.0), and you want A to get to B in one second, then you need a small offet vector a=(1/fps*10). Just add a to A each frame and A will get closer to B.

You can’t rely on the position of one object being equal to any specific position - there are too many ways for this to go wrong: small height differences, small direction misalignment, floating point inaccuracies etc. Usually you should find the vector object->target, clear the y component to ignore height differences and compare its magnitude to some small distance value:

```  var vec = target.position - transform.position;
vec.y = 0;
if (vec.magnitude <= 1.5){ // compare to some suitable min distance
// target reached
}
```

About the field of view, you should use a vector too: calculate the vector enemy->target, then get the angle between it and the enemy forward direction: if it’s less than half the angle of view, the target is visible:

```  var targetDir = target.position - transform.position;
var angle = Vector3.Angle(targetDir, transform.forward);
if (angle <= viewAngle/2){
// target is visible
}
```

Tag your player GameObject “Player”, tag other objects as something else (set the tags by naming Elements in the tag manager), and there you go - the code below determines what the ray hit first:

``````if (Physics.Raycast(transform.position, dir, hit, distance))
{
if (hit.collider.gameObject.tag == "Player")
{
//Player has been seen
}
if (hit.collider.gameObject.tag != "Player")
{
//Player hasn't been seen
}
}
``````

To work out when the object has reached a position, just put in an invisible object and check for a collision, or that the distance to it is less than a certain amount (as below).

``````var distancez = (target.position - enemy.position).magnitude;
if (distancez < 5)
{
//close enough, so trigger whatever needs to happen
}
else
{
//out of range so keep moving
}
``````