如何在协程中使用缓动来更改浮点值?

问题描述

我想随着时间的推移增加浮点数的值,但要增加 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;
     }
  }

解决方法

也许是这样?

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

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...