- Home /
LERP gives final value way too early
Hi people, I hope someone could help me with my problem. I use the following coroutine to interpolate my main camera from one place to another. Everything works OK except the fact that the two LERP functions reach the final value when fraction is still like 0.2, and from 0.2 to 1.0 they keep returning the final value... I just cannot understand where's the mistake...
private IEnumerator tweenCameraToPosition(Vector3 finalPosition, Quaternion finalRotation, float seconds, System.Action callback)
{
float elapsedTime = 0;
Transform startTr = this.gm.getCharacterTransform();
Vector3 currPosition;
Quaternion currRotation;
while (elapsedTime < seconds)
{
float fraction=elapsedTime/seconds;
currPosition=Vector3.Lerp(startTr.position,finalPosition,fraction);
currRotation=Quaternion.Lerp(startTr.rotation,finalRotation,fraction);
elapsedTime += Time.deltaTime;
this.gm.moveCharacterTo(currPosition,currRotation);
yield return null;
}
if (callback != null) {
callback();
}
}
Answer by Utamaru · Mar 19, 2015 at 12:17 PM
I assume this is because you lerp from camera's current position, not from start position. Try this:
Transform startTr = this.gm.getCharacterTransform();
// Save camera's start position
Vector3 startPosition = startTr.position;
...
// And lerp from it
currPosition = Vector3.Lerp(startPosition,finalPosition,fraction);
Answer by Cherno · Mar 19, 2015 at 10:40 AM
It could be because Vector3.Lerp used a value t which is clamped between 0 and 1:
So if in your script the fraction value reaches 1 it means the Lerp is complete.
Your answer
Follow this Question
Related Questions
Lerp to target position in X amount of time 2 Answers
Help using Lerp inside of a coroutine. 1 Answer
better way to rotate instead Coroutine 1 Answer
Multiple Cars not working 1 Answer