Support Forum

Disable Pathfinding behaviour when reaching destination


I’ve been tinkering with the pathfinding for my project now for a day or two, and it’s currently working very well. However, I’ve been trying to do a simple thing for a while now and thought I’d ask:

Currently, when the units I want to go to the target reaches it, I continue to see Path Completed Messages in the console:

Path Completed : Computation Time 0.00 ms Searched Nodes 0 Path Length 1

It clutters the console, and it shouldn’t need to compute any new Paths, since the unit is at the target position. Is there a way to stop all pathfinding activity when on the target?

I should also mention that I want the activity to start again when I assign a new target point.


I have methods that enable and disable the pathfinding scripts, for example if you’re using RichAI you can do something like:

_richAi.enabled = false;

I don’t disable the Seeker script but you should stop the logging from the A* game object before you release the game.

Note that reaching the destination and not moving can lead to weird things. For example if an obstacle appears over the agent it will not try to move out from the obstacle. Granted, this is probably something that does not happen in your game, so it may very well work fine for you to ignore such a scenario.

You can do this using e.g

void Update () {
   ai.canSearch = Vector3.Distance(transform.position, ai.destination) > someThreshold;

You cannot use the ai.reachedEndOfPath because that depends on the path calculation in order to know if it has reached the end of the path.

If you want to reduce clutter in your console you can disable path logging under A* inspector -> Settings -> Path Log Mode.


I tried doing that (I’m using Ai.Lerp) but got an NullReferenceException.

NullReferenceException: Object reference not set to an instance of an object
Pathfinding.Util.PathInterpolator.get_tangent () (at Assets/AstarPathfindingProject/Core/Misc/PathInterpolator.cs:25)
Pathfinding.AILerp.CalculateNextPosition (UnityEngine.Vector3& direction, Single deltaTime) (at Assets/AstarPathfindingProject/Core/AI/AILerp.cs:557)
Pathfinding.AILerp.MovementUpdate (Single deltaTime, UnityEngine.Vector3& nextPosition, UnityEngine.Quaternion& nextRotation) (at Assets/AstarPathfindingProject/Core/AI/AILerp.cs:515)
Pathfinding.AILerp.Update () (at Assets/AstarPathfindingProject/Core/AI/AILerp.cs:503)

However, this works if I enable or disable the Ai.Lerp component in the Inspector while playing. Is there something I’m doing wrong here?


Yeah, I think not moving will work for my game.

Thanks, I’ll try that out. But do you think the answer that juststan gave would work? Or is it better to disable “canSearch”, rather than disabling the whole component?

I think you are using an older version. I believe that bug was fixed in a somewhat recent version.

Disabling the whole component will disable all movement whatsoever, disabling canSearch will only disable recalculating the path. However as you are using the AILerp script this will not be significantly different from canSearch (other movement scripts also use things like gravity which may be important to keep even though the unit is at the destination).

Weird, I’m sure got the newest one, 4_1_12. Maybe I’ve done something in the scripts to mess it up.

Ok, I see. For my purpose, I think I can go with disabling the component then, if i can get it to work

Huh. Try deleting the package and import everything again. Unless I have recently introduced some bug I think that should work perfectly fine. I think I even have unit tests for it.

I tried importing everything again, and when I disable the component I still get the Exception. However, I’ve tried disabling it in OnTargetReached, which from what I understand isn’t too great? Is there a better place where I can try to disable it?

Ah, yeah disabling it inside that method is probably not too great because it will continue with the movement calculations even though the component has been disabled.

You can do something like this in the Update method.

void Update () {
    if (ai.reachedEndOfPath) ai.enabled = false;

which should be roughly equivalent to doing it in OnTargetReached.

1 Like

Thank you, now it works! :smiley:

It was the OnTargetReached method that caused all the trouble.

how would i go about setting the canSearch variable to false when i cant link the AIPath script to one of my scripts?

I am trying to use ai.canSearch = Vector3.Distance(transform.position, ai.destination) > someThreshold;
I get errors under ai and someThreshold. can you please elaborate on how to use this?