FollowerEntity reaching end of path but reachedEndOfPath doesn't get set unless inspector is open

I’m facing a rather annoying issue, with the FollowerEntity.

Basically, sometimes after the agent reaches its destination, it will just stand there because reachedEndOfPath never becomes true. I have stopping distance set at 2 and the agent is definitely within that range.

The weird thing is, this never happens if I have the gameobject with the FollowerEntity component selected in the hierarchy, and the component is expanded. The bug still happens if the gameobject is selected but the component is minimized in the inspector though.

I cannot make use of the debug info within the component inspector, because whenever the bug happens and I go select the agent to view the inspector, it instantly fixes itself and the agent reaches the destination correctly.

If this was only an editor bug I could deal with it, however the issue also persists in builds.

I’m happy to provide more information, but I do not know what information would be useful right now, so feel free to ask.

I’m using version 5.3.7 on Unity 6000.0.43f1

Are you able to package the relevant parts of your project? I think this is one I may need to click around with my own hands.

That’s going to take a while, as the control flow of the game is quite complex. I will try to figure out a way.

The issue is caused by setting facingDirection when calling SetDestination to anything else than default.

I noticed the issue only kept happening when the agent was reaching targets which I had set a specific facing direction on. For now I have removed the facing direction completely, and the issue has disappeared. I’m still looking for a proper fix however, because it has an impact for how believable the AI looks arriving to certain things with the wrong orientation.

Here are the relevant parts I use with that.

Vector3 facing;
    Vector3 end;
    protected void SetDestination(Vector3 position, Vector3 facing)
    {
        var start = agent.position;
        end = position;
        facing = Vector3.zero;
        this.facing = facing;
        if (TimeSince(lastTimePathCalculated) < minPathCalculateInterval)
        {
            shouldCalculate = true;
            return;
        }
        if (validatePathBeforeMoving || customPathEndCondition)
        {
            pathPending = true;
            var constraint = NNConstraint.None;
            constraint.UseSettings(agent.pathfindingSettings);
            var newPath = ABPath.Construct(start, end, OnPathComplete);
            newPath.traversalProvider = this.traversalProvider;
            newPath.nnConstraint = constraint;
            newPath.enabledTags = agent.pathfindingSettings.traversableTags;
            newPath.tagPenalties = agent.pathfindingSettings.tagPenalties;
            if(customPathEndCondition)
            {
                var ec = new PathDistanceEndCondition(newPath);
                ec.maxDistance = minPathEndDistanceFromTarget;
                ec.planar = distanceCheckIsPlanar;
                newPath.endingCondition = ec;
            }
            AstarPath.StartPath(newPath);
        }
        else
        {
            agent.SetDestination(position, facing);
        }
        lastTimePathCalculated = base.elapsedTime;
        shouldCalculate = false;
    }
    protected void SetDestination(Vector3 position)
    {
        SetDestination(position, default);
    }
    void OnPathComplete(Pathfinding.Path p)
    {
        pathPending = false;
        if (!this.isRunning) return;
        if (p.error && validatePathBeforeMoving)
        {
            EndAction(false);
        }
        else
        {
            if (customPathEndCondition)
            {
                agent.SetPath(p);
            }
            else
            {
                agent.SetDestination(end, facing);
            }
            
        }
    }

Worth noting that I noticed that the issue fixes itself as well if movementOverrides.AddBeforeMovementCallback or movementOverrides.RemoveBeforeMovementCallback is called when the agent has reached the destination (similarly how expanding the inspector of FollowerEntity does).

Also having posted the code, I might as well ask, is there no way to add a PathEndingCondition directly to FollowerEntity, similarly as you can set the travesalProvider? I couldn’t find anything in the docs. As you can see right now I have to calculate the path manually and then set it with SetPath, which makes me unable to set the facing direction.

Let me know if this is enough for a reproduction. If not, I’ll keep digging to see if there is something else I’m missing that could have an impact.

Hi

I assume this is happening when not using your custom ending condition code?

Yes indeed, I should have been a little more clear, but both validatePathBeforeMoving and customPathEndCondition are false for this scenario.

EDIT: I noticed that with those two parameters being false most of the code I posted is actually redundant, and the only notable thing here is setting the facing direction… oh well.

Also here are the settings for follower entity. Agent traversalProvider is also set to null in this scenario. I’m also using a script based on the MecanimBridge demo for root motion movement.