Broken pathfinding - seeker movement becomes extremely jerky/laggy as it nears target

I’ve got a problem with my seekers becoming extremely jerky/laggy as they near their target.

Example video: https://www.youtube.com/watch?v=XcPd2iuZrW0&feature=youtu.be

Note how the intended smooth movement plays out early in the run, but then it quickly just begins stuttering/lagging.

Inspector for the seeker GameObject: https://i.imgur.com/vrKi5Wk.png

Code for the seeker (inside the Soldier.cs file):

private CharacterController controller;
private Seeker seeker;
private Path path;
private Vector3 targetPosition;

public float speed = 0.1f;

// The max distance from the AI to a waypoint for it to continue to the next waypoint
public float nextWaypointDistance = 3;
// The waypoint we are currently moving towards
private int currentWaypoint = 0;
// How often to recalculate the path (in seconds)
public float repathRate = 0.5f;
private float lastRepath = -9999;

// Use this for initialization
void Start () {
    seeker = GetComponent<Seeker>();
    controller = GetComponent<CharacterController>();
}

// Update is called once per frame
public void Update()
{
    if (Time.time - lastRepath > repathRate && seeker.IsDone())
    {
        lastRepath = Time.time + Random.value * repathRate * 0.5f;
        // Start a new path to the targetPosition, call the the OnPathComplete function
        // when the path has been calculated (which may take a few frames depending on the complexity)
        seeker.StartPath(transform.position, targetPosition, OnPathComplete);
    }
    if (path == null)
    {
        // We have no path to follow yet, so don't do anything
        return;
    }
    if (currentWaypoint > path.vectorPath.Count) { return; }

    if (currentWaypoint == path.vectorPath.Count)
    {
        Debug.Log("End Of Path Reached");
        currentWaypoint++;
        return;
    }
    // Direction to the next waypoint
    Vector3 dir = (path.vectorPath[currentWaypoint] - transform.position).normalized;
    dir *= speed * Time.deltaTime; //yes I've tried it without the Time.deltaTime multiplier too.
    // Note that SimpleMove takes a velocity in meters/second, so we should not multiply by Time.deltaTime
    controller.Move(dir);
    Debug.Log("Moving in dir: " + dir);
    // The commented line is equivalent to the one below, but the one that is used
    // is slightly faster since it does not have to calculate a square root
    //if (Vector3.Distance (transform.position,path.vectorPath[currentWaypoint]) < nextWaypointDistance) {
    if ((transform.position - path.vectorPath[currentWaypoint]).sqrMagnitude < nextWaypointDistance * nextWaypointDistance)
    {
        currentWaypoint++;
        return;
    }
}

Could anyone shed some light on what might be wrong here? I followed the fairly basic tutorial outlined in the startup guide, but I’m having no joy with it.

Hi

The script described in the get started tutorial is indeed very basic. Among other things it stops completely once it is within ‘nextWaypointDistance’ of the destination. I think that in your case this is what is happening (your agent seems to be only 0.1 meters in diameter compared to the nextWaypointDistance of 3).

If you want to skip writing a custom movement script you can use one of the included ones such as AIPath.
Here is the section which describes that: https://www.arongranberg.com/astar/documentation/dev_4_1_7_6425cc50/getstarted.php#addingai
Or you could take a look at the video tutorial which explains that step in more detail: https://www.youtube.com/watch?v=OpgUcYzRpwM&feature=youtu.be

Wow, thanks for the quick reply!

I’d definitely recommend adding that video to the offline quick-start documentation that accompanies the asset from Unity’s asset store… the one included was made a few years back, and aside from all the code being done in Javascript, it also didn’t mention the inbuilt pathfinders.

Much easier to work now that I’ve watched that - thanks again!