Vector3.lerp problem

So im trying to adapt the code used in the documentation to move my camera from point A to point B.

var start : Transform;
var end : Transform;
function Update () {
transform.position = Vector3.Lerp(start.position, end.position, Time.time);

Once adapted my code looks like this:

var cam : GameObject;
var camEnd = Vector3(4,0.5,0);
	cam.transform.position = Vector3.Lerp(cam.transform.position, camEnd, Time.time);

However, rather than travelling from A to B with a smooth movement over 1 sec like the documentation states, the movement is instant.

I also tried add a smoothing factor as shown in the documentation.

cam.transform.position = Vector3.Lerp(cam.transform.position, camEnd, Time.time * 5);

But this didnt appear to change anything at all.

Anyone know where im going wrong here?

Thanks.

EDIT: Oops. I forgot to add what find of function calls these actions.

function OnCollisionEnter (col: Collision) {

:slight_smile:

Shouldn’t it be Time.deltaTime? Otherwise the interpolation factor would be huge, and therefore the transition will be instant.

cam.transform.position = Vector3.Lerp(cam.transform.position, camEnd, Time.deltaTime * smoothFactor);

I think the time.Time part of the code is related to how FAR between A and B it will travel rather than how LONG it takes.

This is because the value for time.Time in this function is clamped between 1 and 0.

If I replace time.Time with the number 1, it instantly returns as normal, if i replace it with 0 it doesnt move at all and if i replace it with 0.5 it instantly returns to a position half way between A and B.

But thanks for the help anyway :slight_smile:

Yes, I know, and that’s why Time.deltaTime works with that structure. When you do this:

cam.transform.position = Vector3.Lerp(cam.transform.position, camEnd, Time.deltaTime * smoothFactor)

you must note that you are interpolating from the current position to the final position an amount dictated by the third parameter. If it’s 0, there’s no interpolation. If it’s 1, you get the final position. If it’s 0.5, you are exactly in the middle. But you are storing that operation… in the current position.

So you are updating the position at each time step. If you use Time.deltaTime (not Time.time) you will change the position just a little bit each time.

Ahhhh i see.

Because I tried time.deltatime before posting and it only moved a tiny bit.

But this explains it, because if i’m using the function :

function OnCollisionEnter (col: Collision) {

then it will only fire once.

I’ll try using the update function and see what happens.

EDIT: btw when i said ‘Thanks for the help anyway’ I meant that in the sincerist of terms. However on reading my post a second time it just sounds like i was being a douchebag. Sorry :).

Don’t worry, I didn’t undestand it that way. But now that you mention… :wink:

http://www.unifycommunity.com/wiki/index.php?title=MoveObject

–Eric