- Home /
How to use easing inside coroutine to change a float value?
I would like to increase the value of a float over time but with added In-Out easing. Here I'm using Mathf.Lerp and I know there is Mathf.Smoothstep but in SmoothStep, I can't seem to control the speed of the easing itself. I would like the easing to start from a certain value to another value over a specific time(range?) I choose. For example, if the float is changing from 0 to 100, I would like the easing to be from 0 to 20 and again from 70 to 100.
Here is the current code I'm using:
float minValue = 0;
float maxValue = 100;
float duration = 10;
float value;
void Start()
{
StartCoroutine(IncreaseSpeed(minValue, maxValue, duration));
}
private IEnumerator IncreaseSpeed(float start, float end, float duration)
{
float time = 0;
while (time <= duration)
{
time = time + Time.deltaTime;
value = Mathf.Lerp(start, end, time / duration);
yield return null;
}
}
Comment
Answer by andzq · Apr 30, 2021 at 12:30 PM
hi (: try this:
float minValue = 1;
float maxValue = 2;
float duration = 3;
float value;
void Start()
{
StartCoroutine(IncreaseSpeed(minValue, maxValue, duration));
}
private IEnumerator IncreaseSpeed(float start, float end, float duration)
{
float percent = 0;
float timeFactor = 1 / duration;
while (percent < 1)
{
percent += Time.deltaTime * timeFactor;
value = Mathf.Lerp(start, end, Mathf.SmoothStep(0,1,percent));
yield return null;
}
}
Thank you for your help. This does have an "Easing" effect. But how do I control it? how do I make it slower or faster?