unity填充量lerp返回错误值

Unity fill amount lerp returning wrong value

正在尝试检测健康栏的填充量。

有这个代码:

public float lerpSpeed = 2;
public void FillBar()
{
    bar.fillAmount = Mathf.Lerp(0f, 0.7f, Time.deltaTime * lerpSpeed);
    Debug.Log(emotion.fillAmount);
}


当函数运行时,在单击事件后,bar.fillAmount 仅变为 0.28

你的问题没有详细说明。但是你没有超过 0.28 的原因是因为 Mathf.Lerp 的第三个参数代表

The interpolation value between the two floats.

因此,为了获得正确的数量,您需要设置一个变量并在每次填充条形图时更新其中的值,最好是在协程或更新循环中。

public float lerpSpeed = 2;
private float t = 0;

public void FillBar()
{
    bar.fillAmount = Mathf.Lerp(0f, 0.7f, t);
    t += Time.deltaTime * lerpSpeed
}