Wander + AIPath issues

Towa

New member
Hi there,

I've noticed the wander task tends to pick a new target destination several times after the initial destination is reached. I'm using this tree to make an entity wander around for some time then stay idle some more using this tree:

1419

Upon debugging the wander task does recalculate a new target many times, I think this is because of the way HasArrived method in the IAstarAIMovement class works:

C#:
// There is no success or fail state with wander - the agent will just keep wandering
public override TaskStatus OnUpdate()
{
    if (HasArrived()) {
        SetDestination(Target());
    }
    return TaskStatus.Running;
}
HasArrived() checks agent.remainingDistance which seems to not be a reliable way to know this since the paths are asynchronous:

C#:
protected override bool HasArrived()
{
    return agent.reachedDestination || agent.remainingDistance < arriveDistance.Value;
}
These are the documentation commends for remainingDistance and comes with a warning at the end which explains this issue:

Remaining distance along the current path to the end of the path.
For the RichAI movement script this may not always be precisely known, especially when
far away from the destination. In those cases an approximate distance will be returned.

If the agent does not currently have a path, then positive infinity will be returned.

Note: This is the distance to the end of the path, which may or may not be at the <see cref="destination"/>. If the character cannot reach the destination it will try to move as close as possible to it.

Warning: Since path requests are asynchronous, there is a small delay between a path request being sent and this value being updated with the new calculated path.
I'm not sure what would be the fix for this, maybe wait for the path to be calculated before using agent.remainingDistance for the HasArrived check? I know AiPath calls this when the path is calculated so it could be an aproach?

Any ideas? This is mainly an issue for me because is making my enemy sprites flip left or right several times when getting a new wander destination and looks bad.
 

Justin

Administrator
Staff member
If you set HasArrived to the following post does it work for you?

 

Towa

New member
It doesn't work with the wander task because it never gets to the Stop method when a destination is reached since this task in particular will always return the running status, it gets to stop when the timer has passed but by then the agent could have gotten to many destinations already and failed the HasArrived multiple times per destination update.
 

Towa

New member
Hi Justin,

I tried removing the remainingDistance check and rely only on reachedDestination since it would be enough for the kind of behavior I want for this entity.

Still this is not very reliable for other tasks and I'm even sure how it'll affect those yet. Looks like a reliable approach would be start a path search and keep the path instance and check if it's still calculating or wait until is done calculating then ask for the new remainingDistance.

Not really sure what to do with this, any thoughts?
 

Justin

Administrator
Staff member
I was just going to suggest removing the distance check and only have the reachedDestination call. Based on the A* docs it does look like reachedDestination should be sufficient since it resets when the destination is set.

I just went through the Movement Pack demo scene with just reachedDestination and all of the tasks appeared to work - is there a setup in particular that isn't working for you?
 

Justin

Administrator
Staff member
Glad it worked - I just updated the A* integration on the site.

I wish that I could add the destination check code but is specific to AIPath and the tasks operate on the IAStarAI interface. Maybe Aron can add it to the interface?
 
Top