AIPath.ReachedDestination is never true

I’ve got some very simple code, since I’m just getting started, and the character does a good job making his way to the destination, slowing down and stopping when he reached there. But reachedDestination is never being returned as true. reachedEndOfPath is being returned as true, but the suggestion is to use reachedDestination.

Simplified code:

AIPath pathFinder;
pathFinder = GetComponent<AIPath>();
pathFinder.destination = goalPositions[currentGoal];
...
	void Update ()
       {

            if (pathFinder.reachedDestination)
            {
                Debug.Log("REACHED DESTINATION!!!!!!");
                currentGoal++;
                if (currentGoal >= goalPositions.Count)
                {
                    currentGoal = 0;
                }
            }
            pathFinder.destination = goalPositions[currentGoal];
       }

Hi

Is the destination at somewhat the same y coordinate as the character? It will not report that the destination has been reached if the y coordinate is higher up than the character’s head or further down than below 0.5 times the height of the character below it.

Hi,
I have the same problem. To be sure the y coordinate of the destination is the same as the ai I explicitly set the y of the target position to that of the ai:

private void SetTarget()
    {
        while (true)
        {
            var i = Random.Range(0, _targets.Length);
            if (_targets[i] != _currentTarget)
            {
                _currentTarget = _targets[i];
                break;
            }
        }

        var pos = _currentTarget.position;
        pos.y = _ai.transform.position.y;
        _currentTarget.position = pos;

        _ai.destination = _currentTarget.position;
        _ai.SearchPath();
    }

But still the _ai.reachedDestination is always false at the end of the path.
in my project the target destinations are mostly on on the (Recast) graph though!

Hi

Does ai.reachedEndOfPath ever return true?
What is your ‘end reached distance’ parameter set to?
Could you show a screenshot of this (ideally with ‘always draw gizmos’ enabled on the AIPath script and the seeker showing the green path line).

I just get the same problem. reachedDestination never true when the position is below:

character position: 411.9061, -6.701982e-06, 194.6005

destination position: 412.5, 0, 194.8557

radius 20
height 25
slow down distance = 2
end reached distance = 8

when i drag the character away in the scene, it can still move to the destination, and stop there. but IAStarAI.reachedDestination is always false in script.

The most weird part is some characters have this problem, while other characters do not. They all use the same script.

Hi

Can you post a picture of this? I am especially interested in how the path and the graph looks when the character is close to the destination.

I just find I miss this post. I cannot repeat the bug, it just does not happen now.

1 Like

Some context, my project is stuck at 2018.2.20 so i downloaded 4.2.4 Pro which is closest in release date to my Unity version.

My AI runs up and down hills and starts at about position.y 30. the method “reachedDestination” compares the character height to the destinations position.y but does not account for the Ai’s position.y SO i added the Ai’s position.y to the comparing var “h” which solved it.

was this fixed in a later version or am i missing something?

public bool reachedDestination
        {
            get
            {
                if (!reachedEndOfPath) return false;
                // Note: distanceToSteeringTarget is the distance to the end of the path when approachingPathEndpoint is true
                if (approachingPathEndpoint && distanceToSteeringTarget + movementPlane.ToPlane(destination - richPath.Endpoint).magnitude > endReachedDistance) return false;

                // Don't do height checks in 2D mode
                if (orientation != OrientationMode.YAxisForward)
                {
                    // Check if the destination is above the head of the character or far below the feet of it
                    float yDifference;
                    movementPlane.ToPlane(destination, out yDifference);
                    var h = (tr.localScale.y * height) + tr.position.y;   // here i added tr.position.y

                    if (yDifference > h || yDifference < -h * 0.5) return false;
                }

                return true;
            }
        }

Sorry for the late answer, I have been away for some time.
Yes, this was fixed in version 4.2.10.

The implementation for that property now looks like

public override bool reachedDestination {
	get {
		if (!reachedEndOfPath) return false;
		// Distance from our position to the current steering target +
		// Distance from the steering target to the end of the path +
		// distance from the end of the path to the destination.
		// Note that most distance checks are done only in the movement plane (which means in most cases that the y coordinate differences are discarded).
		// This is because those coordinates are often not very accurate.
		// A separate check is done below to make sure that the destination y coordinate is correct
		if (distanceToSteeringTarget + movementPlane.ToPlane(steeringTarget - richPath.Endpoint).magnitude + movementPlane.ToPlane(destination - richPath.Endpoint).magnitude > endReachedDistance) return false;

		// Don't do height checks in 2D mode
		if (orientation != OrientationMode.YAxisForward) {
			// Check if the destination is above the head of the character or far below the feet of it
			float yDifference;
			movementPlane.ToPlane(destination - position, out yDifference);
			var h = tr.localScale.y * height;
			if (yDifference > h || yDifference < -h*0.5) return false;
		}

		return true;
	}
}