Can I interpolate the value of a variable?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By MaaaxiKing

I have the float variable extension and want it to be interpolated. How? I only know that Tweens can interpolate properties of Nodes but I need an interpolation of a script’s variable.

:bust_in_silhouette: Reply From: djmick

You can use the lerp function. For example, you could have a value named x that is 0, and you could interpolate it to 100 using this:

x = lerp(x, 100, 1)

The first value in the lerp function is the starting value, so I just set it to what x is starting at. The second value in the ending value, so I put it too 100. The last value I believe is the percent of the way you want it to lerp every frame, but I’m not 100 percent sure on that. I do know that increasing it will make your lerp happen faster. In your case, you could do:

extension = lerp(extension, endValue, percentPerFrame)

Obviously but the values you need in, but I think you get the point. I hope this helps!

That’s good but when I call this neither in _process(delta) nor in _physics_process(delta), it jumps directly to the end value but I have things going on in the functions already and surely I want the lerp not to be called all the time, so what to do? Changing a specific variable when I want to call this and always check for this variable in _process(delta)? Meh

MaaaxiKing | 2021-02-15 20:57

I’m confused when you want to call this then. This would be no different that interpolating it, so you should call it when you would interpolate it. As for why it is jumping to the end, I have no idea. Just try and put the third value at something really low like 0.01 to test. Sorry for not being much help.

djmick | 2021-02-15 21:04