Search Unity

Instantiated Nav Mesh Agents not patrolling when instantiated on terrain.

Discussion in 'Navigation' started by The702Guy1, May 4, 2022.

  1. The702Guy1

    The702Guy1

    Joined:
    Apr 24, 2022
    Posts:
    48
    Have an odd issue and I can't figure out what's happening. I have enemies that randomly get instantiated on a timer with spawn points placed over the map. When instantiated on an object that I have imported like a road they patrol just fine even if they walk on to the terrain, however if they are instantiated directly on to the terrain they just get stuck there until my player comes within sight range then they will chase and go back to patrolling if the player gets out of their sight range.

    My code controlling the navigation:

    Code (CSharp):
    1.   void Update()
    2.     {
    3.         //Check for sight and attack range
    4.         playerInSightRange = Physics.CheckSphere(transform.position, sightRange, whatIsPlayer);
    5.         playerInAttackRange = Physics.CheckSphere(transform.position, attackRange, whatIsPlayer);
    6.  
    7.         if (this.GetComponent<NavMeshAgent>().enabled == true)
    8.         {
    9.             if (!playerInSightRange && !playerInAttackRange) Patrolling();
    10.             if (playerInSightRange && !playerInAttackRange) ChasePlayer();
    11.             if (playerInAttackRange && playerInSightRange) AttackPlayer();
    12.         }
    13.  
    14.     }
    15.  
    16.     private void Patrolling()
    17.     {
    18.         animator.SetBool("isMoving", true);
    19.  
    20.         if (this.gameObject.name == "Running Zombie")
    21.             animator.SetBool("playerChase", false);
    22.  
    23.         if (!walkPointSet) SearchWalkPoint();
    24.  
    25.         if (walkPointSet)
    26.             agent.SetDestination(walkPoint);
    27.  
    28.         Vector3 distanceToWalkPoint = transform.position - walkPoint;
    29.  
    30.         //Walkpoint reached
    31.         if (distanceToWalkPoint.magnitude < 1f)
    32.             walkPointSet = false;
    33.     }
    34.  
    35.     private void SearchWalkPoint()
    36.     {
    37.  
    38.         //Calculate random point in range
    39.         float randomZ = Random.Range(-walkPointRange, walkPointRange);
    40.         float randomX = Random.Range(-walkPointRange, walkPointRange);
    41.  
    42.         walkPoint = new Vector3(transform.position.x + randomX, transform.position.y, transform.position.z + randomZ);
    43.  
    44.         if (Physics.Raycast(walkPoint, -transform.up, 2f, whatIsGround))
    45.             walkPointSet = true;
    46.     }
    47.  
    48.     private void ChasePlayer()
    49.     {
    50.         animator.SetBool("isMoving", true);
    51.  
    52.         if (this.gameObject.name == "Running Zombie")
    53.             animator.SetBool("playerChase", true);
    54.  
    55.         agent.SetDestination(player.position);
    My guess as to what the issue is is that when they are getting instantiated the terrain they are getting stuck within to terrain until the player gets close enough to break them out of the patrol. I have tried adjusting the Y value of the spawn point and I have also verified that the NavMesh is baked and the area they are spawning in is walkable already. Like I said its only initially when they spawn until my player gets close enough then they work as normal. Any help would be greatly appreciated thank you.
     
  2. bramvanvliet

    bramvanvliet

    Joined:
    Dec 31, 2019
    Posts:
    1
    Did you manage to figure this out? I have currently the exact problem.