Question by
Paulsnijder · Sep 06, 2016 at 01:30 PM ·
lerpfloatintincreasespeed issues
How can I change the value of a Float smoothly?
Its might be kinda a silly, but I can't seem to get it to work. What I want is for my float to go to its new value over time, check:
var Speed : float = 0;
function Called(){
Speed = 100;
//I would like this to happen smoothly, however, I would also like to change back to say 40 if I call it later.
yield WaitForSeconds(2);
Speed = 40;
// So the first time its changed from 0 tot 100, and the second time from 100, 40, how can I make this smooooothhh?
}
Thanks!
Comment
Answer by doublemax · Sep 06, 2016 at 02:32 PM
IEnumerator ChangeSpeed( float v_start, float v_end, float duration )
{
float elapsed = 0.0f;
while (elapsed < duration )
{
speed = Mathf.Lerp( v_start, v_end, elapsed / duration );
elapsed += Time.deltaTime;
yield return null;
}
speed = v_end;
}
Call it with:
StartCoroutine( ChangeSpeed( 100f, 40f, 2f ) );
Answer by Jdean · Sep 25, 2020 at 05:21 PM
I know this is old, but if you just want to adjust the value of a float smoothly inside an Update function, you can do something like this:
float current = 0.0f;
void Update(){
float target = 1.0f;
float delta = target - current;
delta *= Time.deltaTime;
current += delta;
}
Your answer
Follow this Question
Related Questions
Decreasing a value over time, called by InvokeRepeating 0 Answers
How do I convert speed and heading to a vector? 3 Answers
Clamp01 does not work? 1 Answer
Cannot convert float to int? 1 Answer