Jerky movement

Im using the following to change the position of an object, but when the position is changed, the object jerks to the new position, is there anyway to remove this jerk? Ive tried playing around with the Time.deltaTime * 0.4 figure but no luck…

``````  transform.position = Vector3.Lerp(transform.position, target, Time.deltaTime * 0.4);
``````

That looks right. I use it just the way you do. Maybe your scale is so big, that 0.4 as a factor isn’t small enough. Try something like 0.00004.
Also if your frame rate is something like 0.3 fps, then you might see some jerkiness as well

EDIT: Do you mean the jerkiness, that appears when starting to move the object?
Then do something like this with Mathf.SmoothStep

``````var targetPos : Transform;
var fac = 0.4;
var originalPos : Vector3;
var stepFrac : float;

function Awake() {
originalPos = transform.position;
stepFrac = 0;
}

function Update () {

stepFrac += Time.deltaTime*fac;

transform.position = Vector3( Mathf.SmoothStep( originalPos.x, targetPos.position.x, stepFrac ),
Mathf.SmoothStep( originalPos.y, targetPos.position.y, stepFrac ),
Mathf.SmoothStep( originalPos.z, targetPos.position.z, stepFrac ) );
}
``````

Well, here’s how the Lerp function works. It moves you to the point in between the two locations that is at the percentage you provide.

So, if you wanted to smoothly go between the two locations, you should do something like this:

``````var spot1 : Vector3;

var posi : float = 0;
var totalTime : float = 3;

function MoveTo(spot2 : Transform)
{
spot1 = transform.position;
while (posi < 1) {
transform.position = Vector3.Lerp(spot1, spot2.position, posi);
posi += Time.deltaTime / totalTime;
}
}
``````

This makes the script’s Transform (transform) go from where it was to the location passed to the function (spot2) in even steps with the total time (in seconds) of totalTime.

naw it actually works by moving it the problem is it does so in one go.

What you would do is add the accumulator (thats what the posi += … is) in the update by using the "projected target for 0.x seconds ahead as (s)lerp target and filling up your accumulator with steps that ensure that it is a 1.0 in 0.x seconds. once it is >= 1.0f you reset it to 0 and get a new movement target.

Oh, sorry. Heh! Just add yield; to the while loop at the end. It was going the whole way in one frame.

sorry, your code moves the object ridicolously fast, how can I slow it down?

Also, how can I rotate the object to look at the new target?

You’d slow it down by increasing the “totalTime” it takes to travel the distance. Short distances will pass by very slowly (since they’re traversing a short distance in 3 seconds), but longer distances will go very quickly.

To have a constant speed, you’d need to set your desired speed in a variable and when spot2 is selected, you’d do a distance calculation with Vector3.Distance(spot1, spot2) and divide that by your speed and set the result to your total time (since velocity = distance / time => time = distance / velocity)

ive changed the time to:
var totalTime : float = 100000000000000;

and it still moves too fast…

Im really stuck, been trying to do this all weekend with no progress, using Lerp and trying to get a constant speed, whats wrong with my code?

``````var myspeed = Vector3.Distance(transform.position, target);
// Debug.Log(spee);
// transform.position = Vector3.Lerp(transform.position, target, myspeed);
``````