NavMesh AI: follow only if seen

Hello everyone,
As the title says, I want to know how can an enemy with NavMesh AI follow ONLY if it sees the player?

Here is the script for the AI:

`
var speed : float = 10.0;
var player : Transform;
private var dir : Vector3;
private var dirFull : Vector3;

    function FixedUpdate()
    {
        dir = (player.position - transform.position).normalized;
        var hit : RaycastHit;
     
        if (Physics.Raycast(transform.position, transform.forward, hit, 1)) // 20 is raycast distance
        {
           if (hit.transform != this.transform)
           {
             Debug.DrawLine (transform.position, hit.point, Color.white);
     
             dir += hit.normal * 20; // 20 is force to repel by
           }
        }
     
        // more raycasts   
        var leftRay = transform.position + Vector3(-0.125, 0, 0);
        var rightRay = transform.position + Vector3(0.125, 0, 0);
     
        if (Physics.Raycast(leftRay, transform.forward, hit, 1)) // 20 is raycast distance
        {
           if (hit.transform != this.transform)
           {
             Debug.DrawLine (leftRay, hit.point, Color.red);
     
             dir += hit.normal * 20; // 20 is force to repel by
           }
        }
     
        // check for rightRay raycast
        if (Physics.Raycast(rightRay, transform.forward, hit, 1)) // 20 is raycast distance
        {
           if (hit.transform != this.transform)
           {
             Debug.DrawLine (rightRay, hit.point, Color.green);
     
             dir += hit.normal * 20; // 20 is force to repel by
           }
        }
     
        var rot = Quaternion.LookRotation (dir);
     
        transform.rotation = Quaternion.Slerp (transform.rotation, rot, Time.deltaTime);
     
        transform.position += transform.forward * (2 * Time.deltaTime); // 20 is speed
     
     
    }

`

I am not fully certain what you are trying to do in your code, but there is a realtively easy way to do this. Assuming that you have an open area where the AI’s view is not obstructed by geometry, you would have an early out and the actual view check. The early out would be if the Player is within sight Range.

So if your Ai can see 20 units, you would calculate the vector to the Player from the AI and then check the magnitude to see if the player is within the 20 units.

if( (player.Transform - ai.Transform).magnitude > 20 ) return;

Then after that you could do a check to see if the player is in view range. So say the player can see 120 degrees in front of him, you would do a check

if( Vector3.Angle(ai.Forward, player.Transform - ai.Transform) <= (120/2) ) // AI can see player

120 is divided by 2 because the AI wants to look on both sides of his forward vector. Thus he can see 60 degress to the right and 60 degrees to the left of his forward vector. This code is optimal for your situation, but like I said will not fully work if objects are in the way. In that case you could do a raycast with the toPlayer vector(player.Transform - ai.Transform) and see if it collides with any objects.