Search Unity

Desperately need help with NavMesh agent POV problem

Discussion in 'Navigation' started by TheDuples, Jun 14, 2020.

  1. TheDuples

    TheDuples

    Joined:
    Nov 13, 2018
    Posts:
    39
    So I have a little navmesh maze set up with a player character and a monster navmesh agent. I've set up several waypoints throughout the maze that determine where the monster can go when the player is not visible or within a certain aggro radius. When the player enters the aggro range or is spotted by a raycast going out from the forward vector of the agent, I mark the player as a waypoint themselves, (which in the transform array is 0) and the navmesh agent moves towards them.

    My problem is that while the aggro radius works fine, if the player moves ahead of that radius and it is only the raycast that can see them, the agent keeps trying to execute its patrol function and pull away from it course towards the player.

    Here are the variables and the code in the Update function that determines the agent's field of view and whether or not the player is in sight:

    Code (CSharp):
    1.  private NavMeshAgent Mob;
    2.     public GameObject Eyes;
    3.     public GameObject Target;
    4.     public float mobProximityRun = 10.0f;
    5.     public float maxFOVAngle = 45.0f;
    6.     public float lookRadius = 8.0f;
    7.  
    8.     public Transform[] moveSpots;
    9.     private int randomSpot;
    10.  
    11.     private float waitTime;
    12.     public float startWaitTime = 1f;
    13.  
    14.     bool inSight = false;
    15.     void Start()
    16.     {
    17.         Mob = GetComponent<NavMeshAgent>();
    18.         Target = GameObject.FindGameObjectWithTag("Player");
    19.  
    20.         waitTime = startWaitTime;
    21.         randomSpot = Random.Range(1, moveSpots.Length);
    22.     }
    23.     // Update is called once per frame
    24.     void Update()
    25.     {
    26.         Vector3 fovRadius = Eyes.gameObject.transform.forward * lookRadius;
    27.         float proximityToPlayer = Vector3.Distance(transform.position, Target.transform.position);
    28.         float targetAngle = Vector3.Angle(Target.gameObject.transform.position - Eyes.gameObject.transform.position, fovRadius);
    29.        
    30.         //If the angle from the player to the chaser is within the field of view of the chaser:
    31.         if (targetAngle < maxFOVAngle)
    32.         {
    33.             Debug.DrawRay(Eyes.transform.position, Target.transform.position - Eyes.transform.position);
    34.             RaycastHit hit;
    35.             //Draw a Ray to the player. Is the player obstructed? If yes, the player is not in sight. If no, then the player is sighted.
    36.             if (Physics.Raycast(Eyes.transform.position, Target.transform.position - Eyes.transform.position, out hit))
    37.             {
    38.                 if (hit.collider.CompareTag("Player"))
    39.                 {
    40.                     inSight = true;
    41.                 }
    42.                 else
    43.                 {
    44.                     inSight = false;
    45.                 }
    46.             }
    47.         }

    Here too in the Update function is the code that determines when the agent should be chasing or not, and the Patrol function below it:

    Code (CSharp):
    1.  //Chase
    2.         //If the player is closer than x units from chaser OR is within view of the chaser:
    3.         if (proximityToPlayer < mobProximityRun || inSight == true)
    4.         {
    5.             //Chase the player
    6.             Mob.SetDestination(moveSpots[0].position);
    7.         }
    8.         //Wander
    9.         else
    10.         {
    11.             Mob.SetDestination(moveSpots[randomSpot].position);
    12.             Patroling();
    13.         }
    14.     }
    15.  
    16.     void Patroling()
    17.     {
    18.         Mob.SetDestination(moveSpots[randomSpot].position);
    19.         if (Vector3.Distance(transform.position, moveSpots[randomSpot].position) < 2.0f)
    20.         {
    21.             startWaitTime = Random.Range(0, 10);
    22.             if (waitTime <= 0)
    23.             {
    24.                 randomSpot = Random.Range(1, moveSpots.Length);
    25.  
    26.                 waitTime = startWaitTime;
    27.             }
    28.             else
    29.             {
    30.                 waitTime -= Time.deltaTime;
    31.             }
    32.         }
    33.     }
    I've spent the entire day trying to figure this problem out, and I have a feeling the solution is something really stupid I did that's staring me right in the face, please help.
     
  2. TheDuples

    TheDuples

    Joined:
    Nov 13, 2018
    Posts:
    39
    NEVER MIND! There was nothing wrong with my code. You know what was wrong with it? There was a cube in the way of my damn raycast, that's what was wrong.