1

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

Bruno Pigatto
  • 67
  • 2
  • 13

1 Answers1

3

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
}
Immorality
  • 2,164
  • 2
  • 12
  • 24
  • Using Time.deltaTime shouldnt have the same result? All the code samples Ive looked into were using `Time.deltaTime * lerpSpeed` as a Lerp parameter and their code was running smoothly – Bruno Pigatto Mar 06 '19 at 19:46
  • 1
    He used `Time.deltaTime` updating the t already? This is the correct way to lerp. – Ali Kanat Mar 06 '19 at 20:06