Search code examples
c#unity-game-enginenavmesh

How to make NavMesh Agent stop and then continue his movement


I am trying to make enemy patrolling system, where evrytime guard reaches his point, he stopes for 10 seconds, and then continue his movement. I've tried combining animations from Blend tree with isStopped property from NavMeshAgent.

EDIT: My current script makes agent move to point, then he stopes for some time, and then only walk animation plays, but he staing on one place.

public Transform[] points;
private int destPoint = 0;
public NavMeshAgent agent;
public Animator animator;
public int time;

void Start()
{
    agent = GetComponent<NavMeshAgent>();
    animator = transform.Find("Enemy").GetComponent<Animator>();

    // Disabling auto-braking allows for continuous movement
    // between points (ie, the agent doesn't slow down as it
    // approaches a destination point).
    //agent.autoBraking = false;
}


void GotoNextPoint()
{
    // Returns if no points have been set up
    if (points.Length == 0)
        return;

    // Set the agent to go to the currently selected destination.
    agent.destination = points[destPoint].position;

    // Choose the next point in the array as the destination,
    // cycling to the start if necessary.
    destPoint = (destPoint + 1) % points.Length;
    //agent.speed = 1f;
    //animator.SetFloat("Blend", agent.speed);
}

void Update()
{
    if (agent.remainingDistance == 0f && time == 100000)
    {
        agent.speed = 1f;
        Debug.Log(agent.remainingDistance);
        animator.SetFloat("Blend", 1);
        GotoNextPoint();
    }
    else if (agent.remainingDistance <= 0.5f && agent.remainingDistance != 0f && time == 100000)
    {
        animator.SetFloat("Blend",0);
        agent.enabled = false;
        GotoNextPoint();
    }
    else if(animator.GetFloat("Blend") == 0)
    {
        time--;
    }

    if (time == 99000 && animator.GetFloat("Blend") == 0)
    {
        time = 10000;
        agent.enabled = true;
        agent.isStopped = false;
        animator.SetFloat("Blend", 1);
        agent.autoRepath = true;
        GotoNextPoint();
    }
}

I changed few lines of code, now agent moves after first stop, but second time he stops at second poitm,walking animation still working, time doesn't decrementing

if (time == 99000 && animator.GetFloat("Blend") == 0)
    {
        time = 10000;
        agent.enabled = true;
        agent.isStopped = false;
        animator.SetFloat("Blend", 1);
        //New lines of code
        agent.ResetPath();
        destPoint = (destPoint + 1) % points.Length;
        agent.SetDestination(points[destPoint].position);
    }[enter image description here][1]

Solution

  • First of all, I would use the "SetDestination" function in order to set the next destination.

    In the end you wrote:

    if (time == 99000 && animator.GetFloat("Blend") == 0)
    {
        time = 10000;  *-> needs to be 100000, not 10000*
        agent.enabled = true;
        agent.isStopped = false;
        animator.SetFloat("Blend", 1);
        agent.autoRepath = true;
        GotoNextPoint();
    }
    

    You can use "NavMeshAgent.ResetPath" to reset the path instead of using "autoRepath".

    After the "ResetPath", use the "SetDestination" inside the function GotoNextPoint().

    One more very important thing - Check that there is more than one point. If there is just one point, your guard will just walk at the same spot.

    For more information, is suggest to check out Unity NavMeshAgent