AIPath.reachedDestination remains false

  • A* version: 5.2.5
  • Unity version: 2022.3.42f1

I followed the getting started guide setup with a plane, capsule, and grid graph setup: Get Started Guide - A* Pathfinding Project

I then wanted to implement some random “wander” logic for my AI.

On Start(), I use Wandering AI Tutorial - A* Pathfinding Project to select and navigate to a random location:

private void Start()
{
    _aiPath = GetComponent<AIPath>();
    if (_aiPath == null)
    {
        Debug.LogError("AIPath component not found! Attach this script to a GameObject with AIPath.");
        return;
    }
    // Start initial wandering behavior
    GotoRandomDestination();
}

On Update, I am checking whether the AI reached the location with:

private void Update()
{
    Debug.Log($"Distance to destination: {Vector3.Distance(transform.position, _aiPath.destination)}, reachedDistance: {_aiPath.reachedDestination}");
    // Do nothing if we didn't reach destination
    if (!_aiPath.reachedDestination) return;
    
    // Decrease the timer if it's greater than 0
    if (_timer > 0f)
    {
        _timer -= Time.deltaTime;
    }
    else
    {
        // Timer has expired, pick a new destination
        _timer = wanderDelay; // Reset the timer
        GotoRandomDestination();
    }
}

GotoRandomDestination is simple:

private void GotoRandomDestination()
{
    // Pick a random walkable point on the graph
    var sample = AstarPath.active.graphs[0].RandomPointOnSurface(NNConstraint.Walkable);

    // Use the random point as the destination
    _aiPath.destination = (Vector3)sample.position;

    Debug.Log($"New wandering destination: {_aiPath.destination}");
}

But reachedDestination is always false. I logged the destination point and it’s always 1.00 whilst the AI only manages to get within 1.08 of the distance on the Y axis. I assume this is the problem, but not sure how to fix it.

I know there is a way to override what is considered “reached” threshold with endReachedDistance, but I would prefer not to do so. I would like to fix the root cause of why my AI can never reach its true path. I believe it should be possible because it’s not a multi-level building, just a simple flat plane.

I noticed the “skin width” property on the AI character controller is 0.08, and after changing that to the minimum of 0.00001 (or so), my AI can get closer - so I assume it’s related to this. But a value of 0 is not valid here.

I don’t know why that is causing it or what skin depth even is, but what might a solution be to let the AI navigate fully to the end of the path? My AI is perfectly positioned with a Y axis of 1 in the editor, but as soon as I hit play mode it goes to 1.08.

Have you tried setting whenCloseToDestination? I’m not sure how accurate the math would get your units to the exact spot of their destination, as an aside. It may be worth accepting close enough and using endReachedDistance after all.

Hi

I haven’t heard about AIPath - A* Pathfinding Project before, can you share an example of how that might look please?

I think it should be ok to accept “close enough”, I just figured there should be a way for it to just work - but I guess it’s not uncommon for the destination to be fully reached?

I did since update my code to use if (!_aiPath.reachedEndOfPath) return; instead of reachedDestination, and that appears to work.

But I don’t know why, or if that’s what I should be using here. For example, should I keep using reachedEndOfPath or use reachedDestination but with an adjusted endReachedDistance?

Here’s a section from the documentation that may shed some light on how those variables play with each other:

When the end of the path is within this distance then IAstarAI.reachedEndOfPath will return true. When the destination is within this distance then IAstarAI.reachedDestination will return true.

Note that the destination may not be reached just because the end of the path was reached. The destination may not be reachable at all.

And for your other question:

CloseToDestinationMode is exposed in the editor with it’s two valuables- listed as “When Close to Destination”. This also plays into the variables listed above- reachedEndofPath, reachedEndOfDestination, etc. Also from the documentation, here’s the two options you can set and what they do:

Stop: The character will stop as quickly as possible when within endReachedDistance (field that exist on most movement scripts) units from the destination.

ContinueToExactDestination: The character will continue to the exact position of the destination.