# Move problem: Random and Self-mending

I have been working on this script for some time now. It is used to make NPCs move around the scene area by placing GameObjects, that are used as waypoints with a simple script, around the scene.

Now my problem is the jerkyness of the NPC’s movement. It sometimes jumps right on top of a waypoint and sometimes middleway and then smoothly walks rest of the way. Then suddenly and totally randomly the NPC “mends” itself and starts moving all smoothly. Below the code:

//--- SmoothStep variables
var speed : float = 0.0;

//--- Where the NPC is or should go
var activeWaypoint : int;

//--- Waypoints
public var wayPoints : GameObject[];

//--- Waypoint manouvering
function DestinationReached()
{

//--- Stops moving
//~ StopCoroutine("SmoothMove");
//~ print("Destination Reached");

//--- Activates the next waypoint
//--- If path end reached, start over
if(activeWaypoint < wayPoints.length)
activeWaypoint++;
else
activeWaypoint = 1;

//--- If we are not at the end of the path
if(activeWaypoint < wayPoints.length)
{
transform.LookAt(wayPoints[activeWaypoint].transform.position);
yield SmoothMove(wayPoints[activeWaypoint-1].transform.position, wayPoints[activeWaypoint].transform.position, speed);
}
else if( activeWaypoint == wayPoints.length)			//--- When we have reached the last waypoint
{																	//--- head back to first waypoint
transform.LookAt(wayPoints[0].transform.position);
yield SmoothMove(wayPoints[wayPoints.length-1].transform.position, wayPoints[0].transform.position, speed);
}

}

//--- Moving function
function SmoothMove (startpos : Vector3, endpos : Vector3, seconds : float)
{
var t = 0.0;

while (t <= 1.0)
{
t += Time.deltaTime/seconds;
transform.position = Vector3.Lerp(startpos, endpos, Mathf.SmoothStep(0.0, 1.0, t));
yield;
}
}

function Start(){

//--- Head for the first waypoint
activeWaypoint = 1;
yield SmoothMove(transform.position, wayPoints[0].transform.position, speed);
}

I believe it is a problem with the coroutines. I have little experience with them and cannot fix the issue myself. Any help is appreciated.

I’m taking a long shot here, but check out this piece of your code:

transform.position = Vector3.Lerp(startpos, endpos, Mathf.SmoothStep(0.0, 1.0, t));

You’re actually using interpolation inside a Lerp. I’m not sure if this is what you want, but SmoothStep does ease in and out, whereas Lerp is a linear interpolation.

So you might want to try something like this:

transform.position = Vector3.Lerp(startpos, endpos, t);

I’m not sure if this is the result you’re looking for, but at least you can tell if the problem lies in here.

This did indeed change the way the NPC moves to a better direction, but the original problem is still there.

Okay got it working. I changed the way NPC moves. Since we already make the NPC face the next waypoint. We can use Vector3.forward to keep the same pace and make the NPC reach the wanted waypoint.

Still interested to know why my previous code did not work. If someone has an idea why it didnt, I’d love to know.

//--- SmoothStep variables
var speed : float = 0.0;

//--- Where the NPC is or should go
var activeWaypoint : int;

//--- Waypoints
public var wayPoints : GameObject[];

//--- Waypoint manouvering
function DestinationReached()
{

//--- Activates the next waypoint
//--- If path end reached, start over
if(activeWaypoint < wayPoints.length)
activeWaypoint++;
else
activeWaypoint = 1;

//--- If we are not at the end of the path
if(activeWaypoint < wayPoints.length)
{
transform.LookAt(wayPoints[activeWaypoint].transform.position);
}
//--- When we have reached the last waypoint, head back to first waypoint
else if( activeWaypoint == wayPoints.length)
{
transform.LookAt(wayPoints[0].transform.position);
}

}

function Update(){

//--- Move the NPC
transform.Translate((Vector3.forward / 100) * speed);
}

function Start(){

//--- Head for the first waypoint
activeWaypoint = 1;
transform.LookAt(wayPoints[0].transform.position);
}