- Home /
Mathf.Lerp supposed to be in Update()?
I am experimenting with some mathf.Lerp... (nothing I haven't done before) but I can't figure out why my numbers keep resetting every frame? Isn't Matf.Lerp supposed to interpolate from a to b? anyways.. Here's my code simplified:
void Update()
{
if(someBool == true)
{
someFloat = Mathf.Lerp(0, 10, Time.deltaTime * 3);
}
}
Answer by Kiloblargh · Jul 08, 2013 at 06:55 PM
Time.deltaTime is the time in seconds elapsed between this frame and last frame.
Time.deltaTime * 3 is the time it takes to render 3 frames. It will probably be around .05 but will never reach 1 unless your game is seriously lagging.
Lerp will give you a value between the first value and the second value based on where the third value is between 0 and 1.
So it will output something like 0.5 every time, varying based on your FPS. You're not incrementing anything so it will not increase.
I don't get why Lerp is so hard for people to understand.
but what if I wanted to have my variable Lerp from 0 to 10 in 5 seconds? what should I write? something like Time.time / 5???
var startTime : float;
var elapsedTime : float;
startTime = Time.time; //call once to begin the transition
elapsedTime = Time.time-startTime; // do this part in Update
if (elapsedTime <=5)
{
someFloat = $$anonymous$$athf.Lerp (0, 10, elapsedTime/5);
}
Can you see why that works? $$anonymous$$athf.Lerp is actually unnecessary in this case. You could just say,
someFloat = elapsedTime *2;
and get the same result.
Answer by TonyLi · Jul 08, 2013 at 06:51 PM
The third parameter should always be in the range [0..1]. Rewrite your method to normalize the value properly.