- Home /
Mathf.Lerp not working
With this script I am unable to get the "speed" var to pass 1.05. I am unsure why this is happening?
public float speed;
void Update ()
{
speed = Mathf.Lerp(1f, 8f, 0.5f * Time.deltaTime);
{
Could someone clarify he reason? I have used Mathf.Lerp before and never got this effect before. I know the lerp slows down when it gets closer to the end var, but it s off by so much.
speed = $$anonymous$$athf.Lerp(speed, 8.0f, Time.deltaTime * 0.5f);
Answer by Eric5h5 · Dec 14, 2015 at 02:57 AM
Lerp works fine (it's a very basic math function), but the way you're attempting to use it is wrong. Lerp stands for Linear Interpolation, where the function interpolates from one number to the next, where you normally have the third parameter advance from 0.0 to 1.0. If we assume Time.deltaTime is probably around 0.017 (60fps), then 0.5 * Time.deltaTime
means 0.5 * 0.017
or 0.0085. So Lerp (1, 8, 0.0085) returns 1.0595 always, with some minor variation depending on the exact framerate.
The correct way to use Lerp is to not use Time.deltaTime in the third parameter; aside from the above, it makes your code framerate-dependent due to math which I won't get into here. The easiest way to use Lerp correctly is inside a coroutine (not Update):
var t = 0.0f;
while (t <= 1.0f) {
t += 0.5 * Time.deltaTime;
speed = Mathf.Lerp(1f, 8f, t);
yield return null;
}
BTW, Lerp is not supposed to "slow down at the end"; that's the opposite of Linear. It's the effect of using Lerp incorrectly. If you do want a slow-down effect, use the Sinerp function from here: http://wiki.unity3d.com/index.php?title=$$anonymous$$athfx
You misunderstood what I meant. I meant the rate the value is climbing to the end value, as closer the value gets the slower the rate of the value becomes. Also great answer. I thought couratines only worked with yieldforseconds and after reading about them, I am upset about not knowing how to work them before. They save a lot of unneeded variables Very helpfull answer.
No, I know exactly what you meant, but it's not the intended usage of Lerp.
O$$anonymous$$G, this is amazing. After looking for hundred of answers of how to use lerp, this is the only one that explains how lerp is really working. I thought that the point of using lerp is to get that slow down effect at the end. Now I realize that lerp doesn't work like that :). So if you want that effect at the end try this:
while (elapsedTime <= time) {
elapsedTime += 3*Time.deltaTime;
gameObjectTodrag.transform.position = Vector3.Lerp (fromPos, toPos, $$anonymous$$athf.Sin(elapsedTime * $$anonymous$$athf.PI * 0.5f));
yield return null;
}
Answer by TeohRIK · Dec 14, 2015 at 02:45 AM
Maybe you should try with this
public float speed;
void Update ()
{
speed = Mathf.Lerp(speed, 8f, 0.5f * Time.deltaTime);
{
Thanks. I knew it was something dumb. Btw, why wont it work the other way?
you mean the one you posted?
Well, if base on my understanding. The starting value(the first parameter) didn't change every times it loop, so you keep getting the same starting value.
Note that this code will behave differently on machines with different framerates...not by a huge amount, but since it's easy enough to avoid it entirely, I'd strongly recommend not using it.
I thought that what "Time.deltaTime" was used for? Normalizing the differences between frame rate on different computers? or dose this not count for $$anonymous$$atf.Lerp?
If you're feeding Time.deltaTime back into itself, the framerate becomes part of the equation, which makes it dependent on framerate, not independent. With this code, if you have the framerate be 1fps, after 10 seconds, speed would equal somewhere around 6.7. If the framerate is 100fps, after 10 seconds, speed would be around 7.9.
--Eric
Answer by KaveeM2007 · Apr 25 at 05:08 AM
Guys, I just found a solution for this. when you assign the result of this (Mathf.Lerp(a,b,t)) you shouldn't do it to a new variable. for example If I want to set "k" to "M" by "t' in every frame using Mathf.Lerp. This is what I'd do - float K = 10; float M = 30;
float t = 0.2f;
Update()
{
Lerp();
}
void Lerp()
{
K = Mathf.Lerp(K ,M, t * Time.deltaTime)`
float m = K;
Debug.Log(m);
}`
What happens is, I set the value returned by Mathf.Lerp to K which is the value I want to interpolate to ITSELF. By this way, in every frame K is updated. If you assign this returned value to a new variable, this K will stay same forever and will give the same value every time you run the code as it is not changing withing the Mathf.Lerp method. this method does not change it's arguments.. But, by assigning the returned value of the method to itself K is UPDATED EVERY FRAME. then you won't stuck in the same value every time you run your code. If you want this K value for another variable, assign it just after the method. (I've done it as a comment) Debug.Log is the way how I took output here.
Your answer
Follow this Question
Related Questions
Moving 2D sprite along a path 0 Answers
Move object forward then backwards 1 Answer
(Vector3.Lerp) Camera doesn't reach the specified Destination! 1 Answer
Rotate object towards mouse position 1 Answer
Slerp Probems 1 Answer