Vector3.Lerp isn't working :(

Hi, everyone

please help me to fix my problem, the problem is the following:

I have two vectors:

private Vector3 camPosition1 = new Vector3(-0.1018324F,10.98117F,0.6179969F);
private Vector3 camPosition2 = new Vector3(-0.1018324F,9.027315F,0.6179969F);

So i want the camera to move from camPosition1 to camPosition2 using Vector3.Lerp

I used the following code:

void Update()

{

 transform.position = Vector3.Lerp(camPosition1, camPosition2, Time.time);

}

but Lerp doesn’t seem to work, the camera jumps directly without smooth movement
i did even try Time.deltaTime and again no luck!

is it because the two vectors are very close? i mean from y = 10.98117F to y = 9.027315F

Thank you all for your time, any help is greatly appreciated.

You can use:

 transform.position = Vector3.Lerp(transform.position, camPosition2, Time.deltaTime * speed);

‘speed’ is a variable you define or it can be replaced by a constant. This will produce and eased movement towards the goal. Alternately you can do:

 transform.position = Vector3.MoveTowards(transform.position, camPosition2, Time.deltaTime * speed);

This will be a linear movement (no easing). ‘speed’ in this form means units per second. Since your two positions are about 2 units a part, if you put in 1.0f for speed, it will take approximately two seconds to make the movement.

Lerp works in a way where the output value is interpolated between the first 2 inputs based on the time parameter, which is in a range between 0 and 1.

private Vector3 vec1 = new Vector3(0f, 0f, 0f);
private Vector3 vec2 = new Vector3(10f, 10f, 10f);
    
transform.position = Vector3.Lerp(vec1, vec2, 0f);
// position is set at (0f, 0f, 0f)
    
transform.position = Vector3.Lerp(vec1, vec2, 0.5f);
// position is set at (5f, 5f, 5f)
    
transform.position = Vector3.Lerp(vec1, vec2, 1f);
// position is set at (10f, 10f, 10f)

Your error is using Time.time… since it reached ‘1’ in an instant, you’ve already lerped to the max value. When using only Time.deltaTime, you’re hopefully never reaching 1 as you don’t need this long for a frame to execute.

So, how do we lerp the right way?

float t = 0;
void DoSomething()
{
	t += Time.deltaTime;
	Vector3.Lerp (vec1, vec2, t);
}

Now you are adding up to a time variable at each frame and are successfully lerping. If that is to fast or too slow, change it this way.

float t = 0;
float speed = 0.5f;
void DoSomething()
{
	t += Time.deltaTime;
	Vector3.Lerp (vec1, vec2, t * speed);
}