Search code examples
c#unity-game-enginefilllerp

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


Solution

  • 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
    }