Potential bug in TargetReached after setting target? (RESOLVED)

I’m not sure if this is a bug or if I’m doing something incorrectly.

When I set a target using RichAI.target, on the next call in Update on the GameObject, RichAI.TargetReached will be True, when it should be False. That is, the distance to the new target is definitely more than the “endReachDistance” but TargetReached is saying otherwise.

I should note that after setting the new target, I call RichAI.UpdatePath immediately.

Also, this is not 100%, but I did notice a pattern when it fails. When RichAI.TargetReached returns True incorrectly, RichAI.ApproachingPathEndpoint is TRUE. But, when RichAI.TargetReached returns False correctly, RichAI.ApproachingPathEndpoint is FALSE as it should be.

I found that the problem was my own. The correct way to check if the AI reached its target is to check:

if(richAI.TargetReached && !richAI.PathPending)

In fact, the comment above RichAI.TargetReached says just that :slight_smile:

1 Like

Oops. Seems I forgot about this thread.
Yes, that’s the correct way to do it.
I am however considering changing this behavior as several users have run into this exact issue.

The new behavior would remove the targetReached property and introduce 2 properties:

reachedEndOfPath = distance to current end of path < endReachedDistance;
reachedDestination = (distance to current end of path) + (distance from current end of path to destination) < endReachedDistance;

The current TargetReached property is equivalent to reachedEndOfPath, but the name is a lot more descriptive.
The reachedDestination property will only trigger if the character is actually close to the destination, it will not trigger if for example the destination was set to a point that the AI couldn’t reach and the AI moved to the closest point that it could reach (but which was still far away).
I’m not 100% convinced that this is the right approach though. Do you have any suggestions?