Search Unity

NavMeshAgent moves with delay

Discussion in 'Navigation' started by qqepijackxu, Apr 15, 2020.

  1. qqepijackxu

    qqepijackxu

    Joined:
    Jun 14, 2019
    Posts:
    26
    I have poolmanager where I store zombieprefabs. Zombies are randomly spawned from this pool. Everything is okay with pooling (tried disabling it with no effect) but something is wrong with navmesh agents. Animations works great but zombies are not moving. After some delay some inviduals starts to move. Moving zombies with simple forward command works normal. It has to be something with destination settings and timing but I can't get it work properly. Here is my latest movescript.

    Code (CSharp):
    1.  
    2.     private NavMeshAgent zombieAgent;
    3.     Transform finalGoal;
    4.     private Animator anim;
    5.     public float agent_walkSpeed = 0.7F;
    6.     public float agent_runSpeed = 2F;
    7.     int rand;
    8.     bool isWalking;
    9.     bool isRunning;
    10.     void Start()
    11.     {
    12.  
    13.         anim = GetComponent<Animator>();
    14.         zombieAgent = GetComponent<NavMeshAgent>();
    15.         rand = Random.Range(0, 2);
    16.  
    17.         if (rand == 0)
    18.         {
    19.             isWalking = true;
    20.         }
    21.  
    22.         if (rand == 1)
    23.         {
    24.             isRunning = true;
    25.         }
    26.  
    27.         finalGoal = goalManager.instance.goal.transform;
    28.  
    29.     }
    30.  
    31.     void Update()
    32.     {
    33.         setDestination();
    34.         // Vector3 movement = (transform.position += transform.forward * Time.deltaTime * movementSpeed); // move player forward test
    35.  
    36.         if (isRunning)
    37.         {
    38.             transform.GetComponent<Animator>().Play("Zombie Run");
    39.             zombieAgent.speed = agent_runSpeed;
    40.             //  Vector3 movement = (transform.position += transform.forward * Time.deltaTime * agent_runSpeed); //test
    41.             isWalking = false;
    42.         }
    43.  
    44.         if (isWalking)
    45.         {
    46.             transform.GetComponent<Animator>().Play("Walking");
    47.             zombieAgent.speed = agent_walkSpeed;
    48.             // Vector3 movement = (transform.position += transform.forward * Time.deltaTime * agent_walkSpeed); //test
    49.             isRunning = false;
    50.         }
    51.  
    52.     }
    53.  
    54.     void setDestination()
    55.     {
    56.         if (zombieAgent != null && zombieAgent.enabled == true)
    57.         {
    58.             zombieAgent.SetDestination(finalGoal.position);
    59.             Vector3 goalPosition = new Vector3(finalGoal.position.x, transform.position.y, finalGoal.position.z);
    60.             transform.LookAt(goalPosition);
    61.         }
    62.  
    63.     }
    64.