Get furthest node on Point graph

Hi,

I have a point graph with obstacles, it can happen that the destination can’t be reached because of it.
How would I get the furthest node that is reachable towards the destination? And say that it reached it’s destination when it reached the furthest node.

Thank you in advance!

I’m doing a scan before checking if the end of the path has been reached, instead of checking if the destination is reached. This works, but somehow doesn’t set the end of path to the correct node when there’s no obstacle in front. It always does one before.

    public void SetDestination(Transform _destination)
    {
        EventSystemNew<bool>.RaiseEvent(Event_Type.KNIGHT_OR_ENEMY_MOVING, true);

        destination = _destination;
        destinationSetter.target = destination;

        if (waitTillDestinationReached != null) StopCoroutine(waitTillDestinationReached);
        waitTillDestinationReached = StartCoroutine(WaitTillDestinationReached());
    }

    private IEnumerator WaitTillDestinationReached()
    {
        aStarPath.Scan();

        yield return new WaitForSeconds(0.5f);

        yield return new WaitUntil(() => pathfinder.reachedEndOfPath);

        EventSystemNew<bool>.RaiseEvent(Event_Type.KNIGHT_OR_ENEMY_MOVING, false);
    }

It’s supposed to go to the yellow square, because the debug log also shows that this destination is right. But it only goes there if I destroy the walls one node further.

Hi

This should be the default behaviour as long as A* Inspector → Settings → Max Nearest Node Distance is high enough.

That unfortunately wasn’t it. It’s weird, because it knows the destination, it just doesn’t reach it

So I’m setting the correct destination, the only problem is that the AIPath script sees it as an infinity destination. Until I delete one more wall.

The actual destination is also not obstructed, because if I break a wall a lot further, it will walk to the previous destination with no problem

    private IEnumerator WaitTillDestinationReached()
    {
        aStarPath.Scan();

        yield return new WaitForSeconds(0.5f);
        yield return new WaitUntil(() => pathfinder.reachedEndOfPath);

        aStarPath.Scan();

        destinationSetter.target = destination;

        EventSystemNew<bool>.RaiseEvent(Event_Type.KNIGHT_OR_ENEMY_MOVING, false);
    }

This way it works tho

For some strange reason this works, it needs some type of delay before scanning, no matter how small:

    private IEnumerator WaitTillDestinationReached()
    {
        yield return new WaitForEndOfFrame();
        aStarPath.Scan();
        destinationSetter.target = destination;
        yield return new WaitUntil(() => pathfinder.reachedEndOfPath);

        EventSystemNew<bool>.RaiseEvent(Event_Type.KNIGHT_OR_ENEMY_MOVING, false);
    }

pathfinder.reachedEndOfPath only works for the first time i’m setting the target. After the first time, it’s always instantly reached. How can that happen? Or better, how can I solve it?

I got progress, it now works for a few times, and then it becomes instantly for one time. After that it works for a few times, and so on. I’m not sure why this behavior exists, but I’m getting closer

    private IEnumerator WaitTillDestinationReached()
    {
        yield return new WaitForEndOfFrame();
        aStarPath.Scan();
        destinationSetter.target = destination;
        pathfinder.SearchPath();
        yield return new WaitUntil(() => !pathfinder.pathPending);
        yield return new WaitUntil(() => pathfinder.reachedEndOfPath);

        Debug.Log("End Reached");
        EventSystemNew<bool>.RaiseEvent(Event_Type.KNIGHT_OR_ENEMY_MOVING, false);
    }

Hi

That’s odd. Your last code in particular shouldn’t be possible to run instantly. The SearchPath method will set pathPending to true. Can you verify that this is the case?

Hi,

It turned out that I made a calculation mistake in my code, making it find no correct path. Ultimately returning an instant path.