- Home /
Quaternion.Lerp inside a Coroutine
I'm trying to wrap a Quaternion.Lerp inside a Coroutine although its not lerping its just jumping straight to the target Quaternion.
function Start() {
StartCoroutine(rotateToPond(2f));
}
function rotateToPond (time : float) { yield WaitForSeconds (5.0); var originalTime = time; while (time > 0.0f){ time -= Time.deltaTime; transform.rotation = Quaternion.Lerp(transform.rotation,Quaternion(0,180,0,0),time / originalTime); yield; } }
Any ideas? thanks - C
Answer by Bunny83 · Apr 18, 2011 at 09:06 PM
The problem is that's not the way lerp works... You have to provide two constant rotations and the lerping value that have to go from 0.0 to 1.0.
- you lerp in the wrong direction, you go from 1 to 0 but that would be no problem if you swap start and end.
- you use transform.rotation as start value and that get changed every iteration. That's why the lerping situation is different every iteration.
If you want a "linear" interpolation you have to store the start rotation as well.
function rotateToPond (time : float) {
yield WaitForSeconds (5.0);
var originalTime = time;
var originalRotation = transform.rotation;
while (time > 0.0f){
time -= Time.deltaTime;
transform.rotation = Quaternion.Lerp(originalRotation ,Quaternion(0,180,0,0),1-(time / originalTime));
yield;
}
}
As i said you can also swap start and end to lerp from 1 to 0:
transform.rotation = Quaternion.Lerp(Quaternion(0,180,0,0),originalRotation,time / originalTime);
Answer by Joshua · Apr 18, 2011 at 08:02 PM
Is time/originalTime possibly larger then 1 or smaller then 0?
edit for explanation:
Queternion.Lerp is a function that requires three parameters. A startrotation, an endrotation and time. Your third parameter, I see in your code, is "time/originalTime". For the function to work the third parameter has to be between 0.0 (in which case startrotation is returned) and 1.0 (in which case the endrotation is returned). If the time/originalTime you feed it is outside these bounds it wont work.
sorry I've read that back 10 times still not getting you, please elaborate. :D - c
Your answer
Follow this Question
Related Questions
Increment variable over time 4 Answers
Blinking & resource consumption 1 Answer
problem with #pragma strict and Function type 1 Answer
Mathf.Clamp01 CoRoutine 1 Answer