Noobie question: wait a random time

Hey everyone,
i’m kinda new on Unity and I’m trying to make a navmeshagent ( a spider) moving in a more or less natural way. So with a YT tuto I make a script for the spider to go to a trigger, which is randomly teleport to another position. And it’s work pretty fine. But the fact that the spider never stops his movement isn’t realistic at all.
So i’m searching a way to make my agent to wait a random time before going to the next destination.

I’ve read that I should use WaitforSecond and a Coroutine, but i’m not undestand how it works. Could somebody help me to do this please?

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

public class DestinationChange : MonoBehaviour
{
    public float xPos;
    public float yPos;
    public float zPos;
   

    private void OnTriggerEnter(Collider other)
    {
        if (other.tag == "NPC")
        {
  
            xPos = Random.Range(73, 63);
            yPos = Random.Range((float)17.8, (float)17.9) ;
            zPos = Random.Range(62, 58);

            this.gameObject.transform.position = new Vector3(xPos, yPos, zPos);
        }
         
    }
}

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

 public class DestinationChange : MonoBehaviour
{
    public float xPos;
    public float yPos;
    public float zPos;

    private NavMeshAgent nav;

    private void Start()
    {
        nav = GetComponent<NavMeshAgent>();
    }

    private IEnumerator OnTriggerEnter(Collider other)
    {
        if (other.tag == "NPC")
        {

            xPos = Random.Range(73, 63);
            yPos = Random.Range((float)17.8, (float)17.9);
            zPos = Random.Range(62, 58);

            nav.isStopped = true;
            this.gameObject.transform.position = new Vector3(xPos, yPos, zPos);
            yield return new WaitForSeconds(Random.Range(2, 5));

            nav.isStopped = false;
        }

    }
}

Any further help would be appreciated please :slight_smile: