- Home /
I'm not using Lerp properly
Hi, I'm trying to get a smooth movement with the camera from one spot to another and i used Vector.Lerp for it but it blatently ignores my lerptime i put in it. Although i see that my time variable is getting incremented correctly it just lerps way to fast. What am I doing wrong here?
public float lerpTime = 12f;
IEnumerator CameraMovementTransition(Transform beginPosition, Transform endPosition)
{
isMoving=true;
float time = 0.0f;
while (time < 1.0f)
{
time += Time.deltaTime * (Time.timeScale/lerpTime);
Debug.Log(time);
transform.position = Vector3.Lerp(beginPosition.position,endPosition.position,Mathf.SmoothStep(0.0f,1.0f,Mathf.SmoothStep(0.0f,1.0f,time)));
transform.rotation = Quaternion.Slerp(beginPosition.rotation,endPosition.rotation,Mathf.SmoothStep(0.0f,1.0f,Mathf.SmoothStep(0.0f,1.0f,time)));
yield return null;
}
isMoving = false;
}
@sparkzbarca 's solution below will produce an eased movement at the end. It is the most used implementation of Lerp you will find on UA, but it is a "misuse" of Lerp in that Lerp is intended to have the last parameter range from 0 to 1 and produce a linear motion. If you want to fix your code for a s$$anonymous$$dy linear movement make these changes.
Line 8 becomes:
while (time <= lerpTime);
line 10 becomes:
time += Time.deltaTime;
Line 12 becomes:
transform.position = Vector3.Lerp(beginPosition.position,endPosition.position,time/lerpTime);
And you'll make a similar change for the 't' parameter for the Slerp.
Yes I am misusing the Lerp function intentionally. I'm am trying to get an ease in and out feel for the camera movement. I tested my code in an other scene first and there it worked fine. It's seems that the problem I have lays with my parameters (beginPosition and endPosition. When I use my public variables ins$$anonymous$$d of the parameters it works just fine.. I think my thought process of the Lerp function is still not right..
Answer by sparkzbarca · May 12, 2013 at 03:35 AM
you don't understand how the third variable works.
basically your third variable should be
speed * time.deltatime everytime.
or
speed * time.fixedtime if your using fixedupdate.
also your begin position should update every frame.
this is a simple line to lerp a camera.
float cameraspeed = 10f;
camera.transform.position = vector3.lerp(camera.transform.position, endposition,cameraspeed * time.deltatime);
thats a properly done lerp.