- Home /
How to smooth a single value over time?
Hey guys,
I need to smooth a single variable that changes its value overtime. I know that the lerp function should go for this, but therefor you need to have a start value and a target value, I only have one.
Got someone have an idea? Thanks!
newValue += newValue * Time.deltaTime, put this in the Update code.
If you want the effect of newValue be reflected on something in past, put Valeu = newValue, in LateUpdate()
How can you "smooth" the change in a variable, unless you know what it's changing to?
He wants to smooth input, hence to do that you have the split the input between 2 update loops, in order to predict. All input will be handled in LateUpdate, and the new value will be lerped in Update.Because LateUpdate happens after Update.
@Starwalker - but that's not what the code in your first comment does... all that does is exponentially increase the value of newValue!
Answer by designico · Nov 15, 2013 at 09:22 PM
Now I tried this one:
void Update ()
{
velocityX1 = velocityX;
}
void LateUpdate ()
{
velocityX2 = velocityX;
finalVelocityX = Mathf.Lerp(velocityX1, velocityX2, (0.0002f* Time.time));
/// Rotate by using arms
playerTurning.Rotate(0.0f,(finalVelocityX* -(rotateForceInput) * Time.deltaTime),0.0f );
}
No error, but it has no effect as well :-(
void Update ()
{
// x2 is old rotation times new input.
velocityX2 = velocityX1 * -(rotateForceInput) * Time.deltaTime; // New velocity, New $$anonymous$$AX
}
void LateUpdate ()
{
// Lerps new value of X2, with old value of X1, over 0.002 f secs
finalVelocityX = $$anonymous$$athf.Lerp(velocityX1, velocityX2, (0.0002f* Time.time));
velocityX1 = finalVelocityX; // Store final velocity for next frame.
/// Rotate by using arms,
//set new rotation here.
playerTurning.Rotate(0.0f,finalVelocityX ,0.0f );
}
Try above code, it might work.
Thank you I'll check it as soon as possible and than I'll give feedback.
Thanks man! I had to change the code a little bit but now it works:
void Update (){
// x2 is old rotation times new input.
velocityX2 = velocityX; // New velocity, New $$anonymous$$AX
}
void LateUpdate ()
{
// Lerps new value of X2, with old value of X1, over 0.002 f secs
finalVelocityX = $$anonymous$$athf.Lerp(velocityX1, velocityX2, (0.002f* Time.time));
velocityX1 = finalVelocityX; // Store final velocity for next frame.
/// Rotate by using arms
playerTurning.Rotate(0.0f,(finalVelocityX* -(rotateForceInput) * Time.deltaTime),0.0f );
}