AIPath, Complete stop mid-path?


I’m having massive troubles getting my AIPath agent to stop competely mid path and forget about the path.

I’ve tried this:

ai.canSearch = false;
ai.canMove = false;

But as soon as I re-enable canSearch and canMove the AI continues down the old path. The only thing that work is:

ai.isStopped = true;
ai.destination = transform.position;

But the problem with this is that it makes my character face in a random direction.

What should I do?


You only need to call
ai.isStopped = true
no need to set the destination again, this will ensure that the agent comes to a stop, still facing the currently ongoing direction.

Thanks for the suggestion!

I tried it but it seems to work the same way as canMove. As in when I re-enable movement again the AIPath continues the old path.

To explain my problem better:
I have a click to move MOBA game where I click to move to a position and I do it like this:
ai.destination = pos; ai.SearchPath ();
Then I have a hotkey that when I press it I want my character to stop the current movement and clear any paths. The stop would only take 1 frame and then the player can move again.

once the character finished stopping, you can call

I can’t get it to work. When ai.isStopped is set to false again the character continues the old path.

You want your character to immediately stop, without coming to a smooth halt?
If so, Then you can just completly disable the movement manager:

ai.enabled = false;

Hmm, okay I managed to fix it by disabling “Can Serch” from the AIPath component. Which is bizzare because I thought that needs to be on to be able to use ai.destination for pathfinding. But it works now by using ai.SetPath(null).


If you want to stop your AI immediately and forget about it I would recommend:

// This will clear the path
// This will prevent the agent from immediately recalculating a new path
ai.canSearch = false;

Make sure you set canSearch = true again when you do want the agent to start moving again.


It still won’t work. After pausing for one frame the AI continues the old path.

This is in my Input component (this is in Update):

      if (Input.GetMouseButtonDown (1) && canMove) {
        characterMovement.MoveTo (hit [0].point);

This is my movement component MoveTo method:

    public void MoveTo (Vector2 location) {
      ai.destination = location;
      ai.SearchPath ();

This is my stop method:

    public void Stop () {
      ai.SetPath (null);
      ai.canSearch = false;
      ai.canSearch = true;

Edit: Something’s really funky here. I put an Debug.Log (ai.hasPath); in my Update and first it just spams False, but after giving one move order it spams both False and True at the same time. Even when the AI reaches end of the path.


Why are you setting ai.canSearch = true in your Stop method?

1 Like

I wanted to be able to move again next frame, but I didn’t even think about it being dumb. :smiley: Thank you, it should obviously be in the move function instead.

1 Like