Support Forum

How to send a RandomPath to AIPath

Hello again,

I’m trying to implement a wander action. I was wondering how to properly combine AIPath and RandomPath behaviour.

I have an agent that runs on a custom class inherited from AIPath most of the time. Occasionally I would like it to wander and found out about RandomPath.

However, I don’t know what to do with the path returned by RandomPath.Construct. I tried giving it to the Seeker component via StartPath and it does seem to work for a while but then it gets interrupted by the AIPath’s destination recalculation (which I have set to 0.5 seconds) because the actual “destination” variable doesn’t change.

What is the intended way to solve this? I can only think of workarounds.

The workaround I have is this:

RandomPath path = RandomPath.Construct(agent.transform.position, gScore);
path.spread = (int)(maxDistance.value - minDistance.value) * 1000;

var seeker = agent.GetComponent<Seeker>();
seeker.StartPath(path);
path.BlockUntilCalculated();
agent.destination = path.endPoint;

Hi

The recommended way to do this is to use the AIPath’s SetPath method along with setting ai.canSearch to false to prevent it from recalculating its path automatically.

RandomPath path = RandomPath.Construct(agent.transform.position, gScore);
path.spread = (int)(maxDistance.value - minDistance.value) * 1000;

ai.canSearch = false;
ai.SetPath(mypath);

See https://arongranberg.com/astar/docs/aibase.html#SetPath

1 Like