GML Linear interpolation (lerp) analogues?

Edwin

Member
I want to slowly "move" my value from, for example: 2 to 0 without "this":
Webp.net-gifmaker.gif

Sometimes, values that need to slowly decrease or increase are starting to "twitch", trying to get into a certain interval or position.

How to fix it?
 

dannyjenn

Member
The twitching seems like you're overshooting the 0, then overcorrecting, then overshooting again, and so on. You could probably solve that in a number of ways. e.g. max(0,amount) will prevent it from overshooting. min(0,amount) will prevent it from overcorrecting.
 

samspade

Member
I want to slowly "move" my value from, for example: 2 to 0 without "this":
View attachment 20575

Sometimes, values that need to slowly decrease or increase are starting to "twitch", trying to get into a certain interval or position.

How to fix it?
Remember to post your code so we don't have to guess at what is going on.
 

TheSnidr

Heavy metal viking dentist
GMC Elder
Smoothstep is a useful function similar to lerp, but allows for smoother movements.
Since you haven't posted any code, we can only guess how you made your thing bounce back and forth. So please post your code, we're not playing the guessing game.

For making something move smoothly from one place to another, I personally like setting the movement speed to a factor times the distance. This makes an object move fast in the beginning, then slower as it nears its target.
Something like this:
Code:
//Create
startValue = 0;
targetValue = 100;
speedFactor = 0.2;

//Step
startValue += speedFactor * (targetValue - startValue);

//Or alternatively like this:
startValue = lerp(startValue, targetValue, speedFactor);
 

samspade

Member
I'm asking lerp function analogues that works perfectly.
I'm not sure I understand. lerp does work perfectly. It does exactly what it is supposed to every time. If you're getting unexpected results then there is something in your code, which is why you should post it.

If you like how that looks but want something similar, look up easing and tweening. Those all function like lerp, they interpolate between one number and another, but the difference is in how they interpolate. There are several free tweening assets available in the marketplace as well.
 
Top