Approaching destination with RichAI (A* Pro 4.3.54)

I’m trying to make the RichAI agent approach a destination but not to its centre, but rather stop in front of it. What is the best approach for this?

Setting the EndReachedDistance only slows the agent down, it doesn’t stop it.

I tried to stop the agent before it reaches the destination like this:
ai.isStopped = Vector3.Distance(transform.position, target.position) < margin;
but this results in a jittery movement and over reaching as that’s not the actual destination of the agent and it didn’t have time to slow down. Also, this can stop the agent prematurely when navigating around walls.

So I tried to set the destination in front of the target like this:
ai.destination = target.position + (transform.position + target.position).normalized * margin;
This approach has a problem of being imprecise if around walls/obstacles as the direction towards the target from the agent doesn’t have to be the direction the agent actually approaches the target from.

So I tried to first make the agent approach the target normally as target ai.destination = target.position and when ai.remainingDistance is smaller than margin * 2, I recalculated the destination as ai.destination = target.position + (transform.position + target.position).normalized * margin;.
But this doesn’t work because remainingDistance seems too imprecise and often returns smaller values than reality.

I tried to trim the path, but that doesn’t work on RichAI because its not using vectorPath. I also tried to use EndingConditions on XPath but that didn’t work with RichAI either. I tried to calculate the path first, get the point on it some margin from the end and send the agent to that point, but the point’s are off for some reason.

What is the best approach to this? I must say, this seems like a very common use case, did I overlook some obvious solution? Because, at this point, I’m thinking of quickly sending an invisible agent first, getting it’s position some margin before the end and then sending the real agent to that position.

Thanks for any help

For anyone trying to achieve this, after experimenting with this for a week now, my understanding is that this simply can’t be done. RichAI can’t approach a target. Thanks for all your suggestions Aron.


Sorry for the late reply, I missed this thread.
I would think that running

void Update () {
    ai.isStopped = ai.remainingDistance < margin;

would work. It doesn’t look like that was one of the things you tried.

remainingDistance for the RichAI script is an approximation, but it should be an over-estimate, not an under-estimate, I think. If you want something more accurate (at the cost of performance) you can call ai.GetRemainingPath and calculate the length of the returned polyline.
See IAstarAI - A* Pathfinding Project

Let me know if this works for you :slight_smile:

Hi Aron,
thank you for the reply. Unofrtunatelly isStopped doesn’t really stop the agent in the correct place as there is some inertia in the movement. It would generally be much better to actually set the destination correctly in front of the target.
As you suggested, I’m currently using GetRemainingPath and/or remainingDistance to set the destination in front of the target at the right moment.
Can I ask how do I know that RichAI is done calculating the path after assigning the destination with _richAI.destination? I tried to wait for it with

while (_richAI.pathPending) {
   yield return null;
_richAI.remainingDistance    <= Returns wrong value

It doesn’t seem to wait long enough and the returned remainingDistance is wrong.

Ah, ok. Yes, it has some inertia. I suppose you could compensate for that relatively well by making it stop slightly before your desired margin.

You can do:

_richAI.destination = ...;
while (_richAI.pathPending) {
   yield return null;

The SearchPath call will make it start to calculate a path immediately and thus pathPending will become true until it is done.

Thank you, navigating to the centre of the target and then changing the destination to the place in front of it when remainingDistance is small enough sort of works. It is though a bit inefficient.

Given how great and how optimised this asset otherwise is, it is a bit of a shame. I think it would be better if approaching target from distance was supported directly by RichAI or if it was at least possible to trim the path used by the RichAI.

Still, thank you again for all your help.


Would you mind explaining a bit further on what use case in your game this is for? There might be a better solution.

Yes sure, two very typical use-cases come to mind:

  • An NPC approaching the player and engaging in melee combat. (as wanted in, e.g., [2], [3], [4], [9])
    For this, unfortunatelly endReachedDistance doesn’t work [1]. Setting the destination slightly in front of the target breaks when the obstacles are in the way [3]. Trimming the path works [2], but not for RichAI [3] as it’s not using vectorPath. Using XPath and EndingConditionProximity doesn’t work for recast graph [4]. Using isStopped when near the target breaks parts of the RichAI such as slowdownTime [7].
    One solution seems to be changing the way slowdownFactor is calculated within RichAI [1], but shouldn’t this work by default?
  • An NPC following the player from certain distance (e.g., 1 meter) (as wanted in, e.g., [5], [6], [8])
    For this, EndReachedDistance could be used, but is not perfect and doesn’t properly work as it was designed to for values similar to the agent’s radius [5].

I’m open minded to the idea that I overlooked something obvious. Is there some better solution?