How to know if AIPath has reached the objective

I am trying to get if the ai has reached the target, but it is not working.
reachedEndOfPath & reachedDestination are allways false, and aIPath.remainingDistance <= aIPath.endReachedDistance is not working, becase remainingDistance is returning high values when close to destination.

I am setting this like:

        path = GenerateRandomPath(spread);
        aIPath.maxSpeed = UnityEngine.Random.Range(minSpeed, maxSpeed);
        aIPath.enabled = true;
        aIPath.SetPath(path);
        aIPath.canMove = true;
        StartCoroutine(ReachedDestination());

    protected IEnumerator ReachedDestination()
    {
        while (!aIPath.reachedEndOfPath || !aIPath.reachedDestination || aIPath.remainingDistance <= aIPath.endReachedDistance)
        {
            yield return null;
        }


        print("Destination reached");
        rVOController.locked = true;
        aIPath.enabled = false;
    }

Solved waiting for path calculate and manually calculating the distance:

        path = GenerateRandomPath(spread);
        aIPath.SetPath(path);
        StartCoroutine(WaitForPathCalculate(path));

    private IEnumerator WaitForPathCalculate(RandomPath path)
    {
        while (!path.IsDone())
        {
            yield return null;
        }

        aIPath.enabled = true;
        aIPath.destination = path.endPoint;
        aIPath.canMove = true;
        aIPath.SearchPath();
        StartCoroutine(ReachedDestination());
    }

    protected IEnumerator ReachedDestination()
    {
        float distanceFloat = 1;
        while (distanceFloat > 0.1f)
        {
            Vector3 distance = aIPath.destination - transform.position;
            distance.z = 0;
            distanceFloat = (float)System.Math.Round((aIPath.destination - transform.position).magnitude, 2);
            yield return null;
        }

        rVOController.locked = true;
        aIPath.enabled = false;
        aIPath.canMove = false;
        aIPath.canSearch = false;
        aIPath.SetPath(null);
    }
1 Like