c# - 如何在协程中使用缓动来更改浮点值?
问题描述
我想随着时间的推移增加浮动的价值,但增加了 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);
}
推荐阅读
- azure - 如何防止删除 blob 容器?
- ruby-on-rails - NoMethodError:nil 的未定义方法“[]”:NilClass
- react-native - React Native:忽略 CodePush 更新中的文件
- php - 函数目录::store() 的参数太少,通过了 1 个,预期正好有 2 个
- ios - 添加 AutoLayout Constraints 时 UITextView 消失
- ruby-on-rails - 未初始化的常量 ExecJS::Runtimes::RubyRacerRuntime
- java - iText image.setRotationDegrees() 没有保持一致的原点
- swagger - 使用 Swagger 编辑 OpenApi 3.0 的问题
- angular - 从 ReplaySubject 退订返回 asObservable();
- java - Eclipse 编辑器特别不加载 POM.XML (Linux/Raspbian)