Detect when FleePath end is reached

I’m trying to figure out how to call function X when FleePath is done, I thought the callback in StartPath
would be called when path is done, but seems its called when I call StartPath:

public void Enter()
    {
        agent.canSearch = false;
        var pointToAvoid = enemy;
        var path = FleePath.Construct(agent.position, pointToAvoid, 1000 * 250);

        path.aimStrength = 1;
        path.spread = 2000;

        Seeker seeker = unit.GetComponent<Seeker>();

        seeker.StartPath(path, PathCompleted);
    }

    public void PathCompleted(Path path)
    {
        Debug.Log("Path end reached");
    }

I am calling this from one of my states in my state machine, how can I check when unit reached end of flee path?

Hi

The PathCompleted method is called when the path has finished calculating, not when the agent has reached its destination.

Firstly. If you are using one of the built-in movement scripts I would recommend that you use agent.SetPath(path) instead of seeker.StartPath that will make other agent properties like pathPending work properly.

Secondly, you can check if the agent has reached the end of its path using

!agent.pathPending && agent.reachedEndOfPath

The pathPending check is required because reachedEndOfPath returns the result for the path the agent is currently following, which may be a previous path for the few frames it takes for the new path to be calculated.

1 Like