Jerky movement issues

Hello,

`import Pathfinding;
#pragma strict

var targetPosition : Transform;
private var seeker : Seeker;
private var controller : CharacterController;

private var path : Pathfinding.Path;

var speed : float = 100;

private var nextWaypointDistance : float = 3;
private var currentWaypoint : int = 0;

function Start ()
{
seeker = GetComponent(Seeker);
controller = GetComponent(CharacterController);
//seeker.StartPath (transform.position,targetPosition, OnPathComplete);
CalculatePath();
}

function OnPathComplete(p : Pathfinding.Path)
{
Debug.Log(“Got a path, but did it have an error?” + p.error);

if(!p.error)
{
	path = p;
	currentWaypoint = 0;
}

}

function Update()
{
if (path == null) {
//We have no path to move after yet
return;
}

    if (currentWaypoint >= path.vectorPath.Length) {
        Debug.Log ("End Of Path Reached");
        return;
    }
    
    //Direction to the next waypoint
    var dir : Vector3 = (path.vectorPath[currentWaypoint]-transform.position).normalized;
    dir *= speed * Time.fixedDeltaTime;
    controller.SimpleMove (dir);
    
    //Check if we are close enough to the next waypoint
    //If we are, proceed to follow the next waypoint
    if (Vector3.Distance (transform.position,path.vectorPath[currentWaypoint]) < nextWaypointDistance)
    {
        currentWaypoint++;
        return;
    }

}

function CalculatePath()
{
while(true)
{
seeker.StartPath (transform.position,targetPosition.position, OnPathComplete);
yield WaitForSeconds(0.4);
}
}`

When the object is far away from its target (+ 5m) it moves smoothly. However, if it comes any closer, the movement gets slower and slower, and starts moving in jerky little jumps. Help?

  1. Use Time.deltaTime, not Time.fixedDeltaTime
  2. When you recalculate the path, the first point might be behind the player for the first few frames. It will then try to turn back, resulting in jerky movement. Instead place the if (Vector3.Distance) check in a while loop and place it at the start of the Update function.
  3. Also consider using Vector3.ClampToMagnitude instead of normalize, but that is not required.

Never mind