can't make my enemy stop at a point for a certain time

Hello I am using Navmesh to control my enemy, and I make him go to certain points one after the other The system works fine until I try to make him wait for an amount of time, which is just not working, in my head it makes total sense, but what am I doing wrong? Thanks in advance.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;

public class EnemyPatrolAIController : MonoBehaviour
{

[SerializeField]
Transform destination;

[SerializeField]
List<Waypoint> patrolPoints;

public NavMeshAgent navMeshAgent;
public float totalWaitTime = 3.0f;

private int amountOfPatrolPoints;
private int currentPatrolPoint;
private float timeWaited;
private bool isPatrolMoving;
private bool isGoingForward;
private bool isWaiting;

public void Start()
{
    currentPatrolPoint = 0;
    isPatrolMoving = false;

    amountOfPatrolPoints = patrolPoints.Count;
    Debug.Log(amountOfPatrolPoints);
    SetDestination();
}

public void Update()
{
    if (isPatrolMoving == true && navMeshAgent.remainingDistance <= 1.0f)
    {
        isPatrolMoving = false;
        isWaiting = true;

        if(isWaiting == true)
        {
            timeWaited += Time.deltaTime;
            navMeshAgent.isStopped = true;

            if(timeWaited >= totalWaitTime)
            {
                isWaiting = false;
                navMeshAgent.isStopped = false;
            }
        }
        if (isWaiting == false)
        {
            ChangePatrolPoint();
            SetDestination();
        }
    }
    if(!isPatrolMoving)
    {
        isWaiting = true;
    }
}

private void SetDestination()
{
    if(destination != null)
    {
        Vector3 targetVector = patrolPoints[currentPatrolPoint].transform.position;
        navMeshAgent.SetDestination(targetVector);
        isPatrolMoving = true;
    }
}

private void ChangePatrolPoint()
{
    if (currentPatrolPoint > patrolPoints.Count - 1.0f)
    {
        currentPatrolPoint = 0;
    }

    currentPatrolPoint += 1;
}

}

One thing I can see is: In your update loop you have an if check against isPatrolling == true, but in the first line you immediately set isPatrolling == false. So that function only runs for one frame and the timer will not count until totalWaitTime. Try changing the logic around so that they are in separate if statements:

public void Update()
 {
     if (isPatrolMoving == true && navMeshAgent.remainingDistance <= 1.0f)
     {
         isWaiting = true;
         isPatrolMoving = false;

         // I also moved this line here so that it only gets called once
         // instead of every frame during the wait period
         navMeshAgent.isStopped = true;
     }

     else if(!isPatrolMoving)
     {
         if(isWaiting)
         {
             if(timeWaited >= totalWaitTime)
             {
                 // Also you want to reset your timer var for the next iteration
                 timeWaited = 0;

                 isWaiting = false;
                 navMeshAgent.isStopped = false;

             } else timeWaited += Time.deltaTime;
         }

         else
         {
             ChangePatrolPoint();
             SetDestination();
         }
     }
 }

The only other thing that I can see is that the check on line 60:

destination != null

may return true because it was given the first control point as destination on the first iteration of code. I’m not sure if Unity NavMesh wipes the destination upon arrival, so you may run into an issue where they are at the first control point and then don’t move to the next one after waiting. In which case you can add a Debug statement within the codeblock to test and check this issue:

private void ChangePatrolPoint()
 {
       Debug.Log("Changing Patrol Point");
       if(destination != null) { /*Do stuff*/ }
 }

Since isPatrolling gets set to true within this code block only if destination != null, your update loop will stay in “!isPatrol” > “!isWaiting” and continuously call “ChangePatrolPoint()”, so you’ll see the debug in console in rapid succession versus just being called once inbetween points.