# Is Lerp acting weird?

Here is my code.

``````#pragma strict
#pragma implicit
#pragma downcast

public var theCamera : Transform;

function OnMouseUp()
{
theCamera.position = Vector3.Lerp(theCamera.position, transform.position, Time.time);
}
``````

Instead of moving nicely over 1 second, it moves instantly. This script is attached to a Gameobject (with a collider), not a GUI Object.

Am I doing something wrong? Or is there a glitch?

Dave

OnMouseUp only runs once, when the mouse button is released. Lerp is not a function that “runs over time” or anything, it’s just a very simple math function that returns a value immediately. (Specifically, (b - a) * t + a, where t is clamped between 0 and 1.) Also you should not use Time.time in the 3rd parameter, since it needs a value between 0 and 1.

If you want movement over time, it’s generally easiest to use a coroutine, like this.

You do a lot worng Lerp is ment to interpolate between two fix positions / values. You use the current position as start value.

So for example when your camera is at 0,0,0 at the beginning and your target position is at 100,0,0 it will process like this:

``````Frame   start     end    t     resulting value
0       0         100    0     0
1       0         100    0.05  5
2       5         100    0.1   14.5
3       14.5      100    0.15  27.325
4       27.325    100    0.2   41.86
5       41.86     100    0.25  56.395
``````

You need to use the same start and end position

``````Frame   start     end    t     resulting value
0       0         100    0     0
1       0         100    0.05  5
2       0         100    0.1   10
3       0         100    0.15  15
4       0         100    0.2   20
5       0         100    0.25  25
``````

Lerp is not black magic. It’s very very simple. That’s how it’s implemented:

``````function Lerp(from : Vector3, to : Vector3, t : float)
{
return from + (to - from) * Mathf.Clamp01(t);
}
``````