Unit behaving strangely (regarding path updates)


Basically, I got a ton of units on mobile using your system and it all works great. We are using recast graph with tiles, we have a few units using alternative path modifiers, others using funnel and so on. The problem is, sometimes the unit get kind of lost. It is moving towards its target, but then a new path is ready and it actually has to move backwards to go back to the starting position of the new path, and if frame rate drops low enough (30fps), this happens extremely often, like all the time.

So basically what Im asking is, what is the best way to have a unit follow its target (moving target) decently, I mean, with an updated path at least every half second or every second? Because recreating the path at a fixed interval has proven to be extremely problematic…

Thanks for the attention,


Yes, this can happen if the path is calculated, but it takes a long time for it to calculate, so the unit has moved some distance before the calculated path is returned. The included movement scripts have some support for mitigating this however.

What movement script are you using?

We are using a custom script but it is very simple… basically we have a pathfinder:


And a character motor which takes the input from pathfinder:


Which supported scripts mitigate this? Any way to easily use those?

Thanks for the attention


If you want to continue using that script, the necessary changes will have to be done in the Pathfinder script, in the OnPathComplete method. The core idea is that since the agent has moved some distance, possibly it should not start at waypoint 0, but rather at a higher index.

Here is the excerpt from the code in AIPath for handling this

    // Simulate movement from the point where the path was requested
    // to where we are right now. This reduces the risk that the agent
    // gets confused because the first point in the path is far away
    // from the current position (possibly behind it which could cause
    // the agent to turn around, and that looks pretty bad).
    waypointIndex = 0;
    Vector3 p1 = p.originalStartPoint;
    Vector3 p2 = transform.position;
    Vector3 dir = p2-p1;
    float magn = dir.magnitude;
    dir /= magn;
    int steps = (int)(magn/pickNextWaypointDist);

    for (int i = 0; i <= steps; i++) {
            p1 += dir;

In place of CalculateVelocity, you likely just want some some code for checking like the one you have in your motor script.

 while (has reached next waypoint approximately) {

Otherwise you can try using the RichAI or AIPath scripts.