ai.reachedDestination is false when can't reach destination

My unit does not understand that it cannot reach the destination.

See the gif below:
the unit becomes red if ai.reachedDestination = true and is cyan when ai.reachedDestination = false.

How can I know when the destination is reached? (meaning the unit can’t go any further on the path it is on)


The script I’m using:

public class Test : MonoBehaviour
{
public Transform Destination;
private AIPath Path;
private MeshRenderer Mr;

void Awake()
{
    Path = GetComponent<AIPath>();
    Mr= GetComponentInChildren<MeshRenderer>();
}

void Update()
{
    if (Destination != null)
    {
        Path.destination = Destination.position;

        if (Path.reachedDestination)
        {
            Mr.material.color = Color.red;
        }
        else
        {
            Mr.material.color = Color.cyan;
        }
    }
}

}

You can use PathUtilities.IsPathPossible to determine if it could get there at all.

However I would also like to know how we can tell if it has finished without reaching. For example I want to mimic the behaviour of “the ai should try and get to target and make a decision when it can’t”.

What is IsStopped for you?

reachedDestination is true if the agent has reached the destination.
There is also the related reachedEndOfPath property which is true when the agent has reached the end of the path it is following. You might want to use that one instead.
Please read the documentation for that property though. It was introduced earlier and it has some annoying gotchas. I hope to fix those in a future version, but it’s tricky to keep backwards compatibility.

yes I had tried “reachedDestination” before the result with it is the same.

IsStopped is still set to false in this case.

PathUtility.IsPathPossible is interesting but I still would like the unit go towards the direction of the destination until it is blocked.
Has something changed?.. I don’t remember having this problem before.

I ended up solving this by manually keeping track of when the AI comes to a stop by checking it’s position compared to the previous frame.

Hi,

I’m still struggling with this problem. In order to determine whether the agent has failed to reach it’s destination but cannot go any further i do reachedEndOfPath && !reachedDestination. What i want to do, when this scenario occurs, is to set another destination. In setting a new destination reachedDestination immediately becomes false but i would also expect the path to get reset. Given that this happens some frames later I’m not sure what to do here.

The following steps are all happening in the same window of time until the agent repaths. This is for a scenario where an agent navigates to a place in the scene and when it gets there immediately navigates to another place:

  • Agent reaches the end of the path & the destination successfully.
  • AIPath.destination is set to a new location which resets the reachedDestination property on AIPath.
  • The next check for whether the agent has failed to reach the destination incorrectly passes.
  • Some time late the agent starts navigating to the new destination. reachedEndOfPath becomes false.

This is very frustrating. I somehow need to know that a new destination has been asked for and the aipath is “going to start navigating to that destination soon” so that i can ignore false failed-to-reach-destinatiions when switching.

Thanks,

Hi

Probably your best bet is to subclass the movement script and override the OnPathComplete method. In that method, just call the base method and then do your check. Then you know a path calculation was just done and thus your checks should be more robust.

Isn’t the issue with that going to be that I’ll get that callback called for any path completion, including ones before repathing?

So i think i have a solution for this. Basically when the game logic requests that an AI go to a location i have some logic in the AI that determines whether it could ever get there and if not it choose a position near to the target for the agent to nav to. Navigation will always succeed but the success in terms of the game is where it successfully navigated to, whether it’s the exact target or some alternative nearer target.

I ended up noticing that reachedEndOfPath will not be set to true unless your unit has a radius that is smaller than whatever radius you set for on the graph.