how to add a wait time in my patrolling script?

Hello everyone,

i’m using a little patrolling script that makes my player with a navmeshagent to visit different empty objects in my scene. In my animator, i only have my Walking animation.

Here is the script:

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

public class Patrol : MonoBehaviour
{
    public Transform[] pointsavisiter;
    private int destPoint = 0;
    private NavMeshAgent agent;

    void Start()
    {
        agent = GetComponent<NavMeshAgent>();
        agent.autoBraking = false;
        VaAuProchainPoint();
    }

    void VaAuProchainPoint()
    {
        if (pointsavisiter.Length == 0)
            return;
        agent.destination = pointsavisiter[destPoint].position;
        destPoint = (destPoint + 1) % pointsavisiter.Length;
    }

    void Update()
    {
        if (!agent.pathPending && agent.remainingDistance < 0.5f)
            VaAuProchainPoint();
    }
}

What i’d like to do is for my player to stop at each of the visited point, wait a couple of seconds, then resume patrolling? How could i do it? Also, could i use a idle animation when he is stopped?

thanks a lot for your help.

if I remember correctly, would it be possible to try using a coroutine with WaitForSeconds? (link to manual for coroutine)?

here’s the link to the scripting API as well for coroutine: link

but the relevant part I would look at doing from that link is:

    private IEnumerator WaitAndPrint(float waitTime)
    {
        yield return new WaitForSeconds(waitTime);
        print("Coroutine ended: " + Time.time + " seconds");
    }

if you want to know more about WaitForSeconds (what I’ve used, granted, not for patrol routes, so grain of salt), here’s the link to the scripting reference: link

Like I said, grain of salt cuz I don’t know the most about patrol routes, but I don’t see why it wouldn’t work.

for the animation thing, you would need to hook up the idle animation in the animator as well and look at the ‘state machine’ that you get to meddle with there. (can post pics if you don’t understand me and want clarification/ examples. also sorry if you already knew this part.)