What is the best way to smoothly update a float?

Hello,

I’m trying to create a function which will take as inputs: NewValue and Time.
The goal of the function is to update a private float variable to NewValue, and it will smoothly update itself in Time seconds.

For example, the variable is set to 0 and I call the function with NewValue = 1 and Time = 1

So the variable will have to have the value “0.1” in 0.1 second, “0.5” in 0.5 second, and “1” in 1 second.

I wanted to use Lerp but the problem is that I can’t set how long it has to update it, and the smooth is not the one I want, because the value will be 0.1, 0.6, 0.9, 0.95, 0.99, 0.999… and I want 0.1, 0.2, 0.3…, 0.9, 1.

So I did a math function but I’m sure that’s not the best way to do it:

http://puu.sh/axls7/022751e6f5.jpg

(The first node branch is connected to Event Tick).
As you can see it is very messy… Is there a way to use a Timeline or something like that to do what I want?

Thanks!

Have you tried FInterp To node?

Yes I already tried but actually I think I’m doing something wrong with it.
Here’s my code:

http://puu.sh/axmp5/04be96abfe.png

The problem is that the Test variable will increase but never reach 1. It will go to 0.9999…

I’m probably doing it wrong because it gives quite the same value as Lerp…

Thats normal, because Interp never reaches target value, just move value closer to target value and less distance i moves less closer, thats why you go this smooth effect out of it. You need to detect when value is 0.9999999 and correct it to 1 yourself. This function was made mainly for animation where 0.9999 values are irrelevent.

Okay, thanks.
So there is no way but the one I posted on my question to have a linear increase, like this line:

http://puu.sh/axrk5/d45582758d.png

And not like this one:

http://puu.sh/axrl0/576bbb0660.png

Perhaps write your own function to do this?

ie Create a variable (float) named updateRate and another (float) targetValue.
Create a function SetNewTargetValue (with two inputs NewValue(float) and Time(float)). Within the function, set

targetValue = newValue

updateRate = (NewValue-Currentvalue)/Time

In the Tick event, do this:
CurrentValue = min(CurrentValue+(updateRate*deltaDeconds), targetValue)

You will do the above only if (updateRate != 0 AND CurerntValue != targetValue)

I think this will give you a linear increase.

1 Like