Support Forum

Agent path issue

Hi,

I have noticed an issue regarding the way an agent moves along its path (probably due to something silly I’m doing): I set canSearch and canMove (on the RichAI component) to false when I want to “freeze” an agent, to make sure it doesn’t move at all for a while.

But when I “unfreeze” the agent (after assigning it a new destination and using SearchPath) I noticed it still keeps following its previous path (to the old destination) for a fraction of a second before starting on its new path. Which of course looks weird.

So my question is: Is there a way to wipe or clear the previous path from an agent so that it doesn’t attempt at all to go there, even if just for a fraction of a second?

You can set the path to null.

Or you could add the canMove = true to be called on the callback from the seeker

Thanks for your reply! So I tried calling SetPath(null) on the RichAI script after “freezing” the agent but I got errors from the AIBase.cs script:

NullReferenceException: Object reference not set to an instance of an object
Pathfinding.AIBase.SetPath (Pathfinding.Path path) (at Assets/AstarPathfindingProject/Core/AI/AIBase.cs:459)

Hey sorry,

richPath.Clear(); on the RichAI agent would be what you are looking for

Mmh it seems the richPath variable on the RichAI script is both protected and read-only. What would be the correct way to access the RichAI path in order to clear it?

I made a function on the RichAi class

public void ClearPath()
{
   richPath.Clear();
}

Thanks Toasty! I tried that but it doesn’t get rid of the short period of time when the agents follow their old destination before switching to the new one unfortunately.

Also I’m a little bit reluctant to hack the source code itself as those changes would have to be tracked and re-applied the next time A* is updated.

I mean I would much prefer an official fix, or to be told I’m doing it wrong and how to do it properly… :sweat_smile:

Then using the call back would be the easiest.

Writing this from my mobile so apologies for the formatting.

The seeker StartPath allows you to set a callback.
https://arongranberg.com/astar/docs/class_pathfinding_1_1_seeker.php#a4f2da7a807627bc633bebca73ee9cc76
Here is an example of how to use it:

public void StopAgent() 
{
    Agent.canMove = false;
    Agent.canSearch = false;
} 

public void StartNewPath( Vector3 destination)
{
    Seeker.StartPath(transform.position, destination, () => {
        Agent.canMove = true;
        Agent.canSearch = true;
    }) ;
}
1 Like

If there are still issues, we’ll have to probably reset the velocity or something. If you can’t get it working I’ll set up a similar environment at home and work it out for you :slight_smile:

1 Like

Hi

When setting canMove to false the agent literally just stops updating its movement. This has the side effect of that when it is enabled again the agent thinks no time has passed at all. So it will both continue to use its previous path and continue with its previous velocity.

The simplest way to get the behaviour you want is to disable /enable the whole component.

ai.enabled = false;

This will reset both the path and the velocity out of the box. SetPath(null) only works in the current beta and in the next beta it will also be possible to manually set the agent’s velocity to zero.

1 Like

Excellent, thank you! That did the trick. :slight_smile:

(Edit: Please see my next post, as I believe this one can be ignored)

Well I spoke too soon. Here’s what’s happening. I submit my path request like so:

	if (!ai.richAIScript.pathPending)
		{
		ai.richAIScript.SearchPath();// => immediate
		}

But the problem is that when disabling the richAI component, although the path is supposed to be wiped clean (so that the actor directly starts on a new path when it’s enabled again) the pathPending variable still remains true, both before and after the disabling.

So after getting re-enabled the agent remains stuck on the spot and I can see the green line pointing to the last destination before it was disabled, indicating clearly that it will never go to the new destination since it considers it’s still on the old path (pathPending being true).

I also tried removing the pathPending condition and search the path right away but then the agent eventually becomes stuck and I get a wall of warnings saying:

Path Failed : Computation Time 0.00 ms Searched Nodes 0
Error: Couldn't find a node close to the end point
...

I just need a way to stop an agent for a while (like a second or so) and then simply send it on its way to somewhere else, without the previous path parasitizing its locomotion.

Oh boy. I found out at last what was happening: there were actually 2 graphs superimposed on top of each-other. And I know exactly how it happened: In my game I load the graph via code at the scene’s arrival, but at one point I clicked on Generate Cache (in the A* UI) and I didn’t notice that Cache Startup got automatically checked!

So then at play time I had not only the graph I loaded via code, but also the sneaky one imported by A*. So my request is this: Could you please make it so that Cache Startup does NOT get automatically checked when you refresh a cache? Cheers!

1 Like

Hi did you find a solution to this? I’m having the same problem and I manage to “fix” that by disabling and renabling the RichAI, but that’s not the nicest solution :sweat_smile: