【问题标题】:Mathf.PingPong from 1 to 0Mathf.PingPong 从 1 到 0
【发布时间】:2020-08-19 12:27:45
【问题描述】:

我已经创建了一个从 1 到 0 的 PingPong 助手,但我很难将其反转,因此值从 1 变为 0。

您可以在下面看到代码,我目前正在使用。我也不确定这是否可能。

_lerpPulse = PingPong(Time.time * 0.5f, 0f, 1f);

还有帮手

float PingPong(float aValue, float aMin, float aMax)
{
    return Mathf.PingPong(aValue, aMax - aMin) + aMin;
}

提前致谢!

【问题讨论】:

  • 为什么不直接使用 sin / cos 函数呢?顺便说一句,+ aMin 不是必需的
  • 我最初使用的是 Sin,但不知道如何从 1 变为 0 _lerpPulse = (Mathf.Sin(Time.time * 2f) + 1f) / 2f 这是正在使用的和适用于 0 到 1

标签: c# unity3d lerp math-functions


【解决方案1】:

Mathf.PingPong 创建这样的图表 因此,要从峰值开始,您可以将沿 x 轴到峰值的距离添加到时间参数中,即 sqrt(2)/2 * 长度,或 0.707 * 长度

float PingPong1To0(float aValue, float aMin, float aMax)
{
    float offset 0.707f *  (aMax - aMin); // sqrt(2)/2
    return Mathf.PingPong(aValue +offset, aMax - aMin) + aMin;
}

应该击中那个三角形的下降边

【讨论】:

  • answer here 更好地解释 mathf.PingPong 的工作原理
  • 这确实是我想要的!我唯一想知道的是,我怎样才能改变 PingPong 的速度?最初我使用 Time.time * 0.5f,但由于某种原因这不适用于偏移量。
  • Mathf.PingPong((Time.time * 0.5f) + offset, aMax...?没用?
猜你喜欢
  • 2019-10-03
  • 2011-03-12
  • 2010-12-01
  • 2018-09-26
  • 1970-01-01
  • 1970-01-01
  • 2023-04-03
  • 2018-11-30
  • 2017-11-28
相关资源
最近更新 更多