How to use easing inside a coroutine to change a float value?

724 Views Asked by At

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;
     }
  }
1

There are 1 best solutions below

1
On

Maybe something like this?

{
    time  = time + Time.deltaTime;
    value = Mathf.Lerp(start, 20, time / duration);
    yield return new WaitUntil(()=> value == 20);
    value = Mathf.Lerp(20, 70, time / duration);
    yield return new WaitUntil(()=> value == 70);
    value = Mathf.Lerp(70, 100, time / duration);
}