首页 > 解决方案 > 如何在协程中使用缓动来更改浮点值?

问题描述

我想随着时间的推移增加浮动的价值,但增加了 In-Out 缓动。

在这里,我使用的是 Mathf.Lerp,我知道有 Mathf.Smoothstep,但在 SmoothStep 中,我似乎无法控制缓动本身的速度。我希望在我选择的特定时间(范围?)内从某个值开始缓动到另一个值。例如,如果浮点数从 0 变为 100,我希望缓动从 0 变为 20,然后再从 70 变为 100。

这是我正在使用的当前代码:

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

标签: c#unity3dcoroutineeasinglerp

解决方案


也许是这样的?

{
    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);
}

推荐阅读