简体   繁体   中英

Unity fill amount lerp returning wrong value

Trying to lerp the fill amount of a health bar.

Have this code:

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


When the function runs, after a click event, the bar.fillAmount goes only to 0.28

Your question is a little sparce on the details. But the reason you are not getting past 0.28 is because the third parameter of Mathf.Lerp represents

The interpolation value between the two floats.

So to get the right amount, you need to set a variable and update the value in it each time you fill the bar, preferably in something like a coroutine or in the update loop.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM