# My FOV for enemy script is not working

Hello! Good Day! My script works in some kind of way but the problem is distance and when other objects are blocking, the enemy is able to target the player even from far away or with a wall blocking its sight, only the angle of view works… Here my script

void CheckPlayer()
{
Vector3 forward = transform.TransformDirection(Vector3.forward);
Vector3 toPlayer = target.transform.position - transform.position;

if (Vector3.Dot(forward, toPlayer) > 10 && Vector3.Distance(myTransform.position, target.position) < awareness)
{
wander = false; //would turn off random roaming and target player
}
else if (Vector3.Dot(forward, toPlayer) < 10 && Vector3.Distance(myTransform.position, target.position) > awareness)
{
wander = true; //would turn on random roaming
}
}

toPlayer = Vector3.SqrMagnitude(target.transform.position - transform.position)

There are some things confusing. First of all this line:

Vector3 forward = transform.TransformDirection(Vector3.forward);

is just the same as

Vector3 forward = transform.forward;

Next i’m not sure you understand what this actually means: Vector3.Dot(forward, toPlayer) > 10 This will only be true if the player is at least 10 units away from your enemy, but only along the enemy’s forward axis. On the other hand this: Vector3.Dot(forward, toPlayer) < 10 would be true whenever the player is too close or behind the enemy.

When you said FOV you probably wanted to define a field of view. For this you need normalized vectors. In this case the Dot product returns the cosine between the two vectors. However it probably would make more sense to use Vector3.Angle which returns the angle between the two vectors in degrees. Something like this:

void CheckPlayer()
{
Vector3 forward = transform.forward;
Vector3 toPlayer = (target.transform.position - transform.position).normalized;
bool inView = Vector3.Angle(forward, toPlayer) < 45;
bool inRange = Vector3.Distance(myTransform.position, target.position) < awareness;
if (inView && inRange)
{
wander = false; //would turn off random roaming and target player
}
else if (!inView && !inRange)
{
wander = true; //would turn on random roaming
}
}

This gives the enemy a 90° field of view (± 45°). If you use “90” instead it would have a 180° vision. If you want to use Vector3.Dot instead you would need to compare the result to the cosine of the half of your desired angle. Though Vector3.Angle actually does use Vector3.Dot internally but also normalizes the vectors and converts the cosine into an angle.