Can't get Image.fillAmount to lerp

I have a circular progress indicator. It works fine, and now I want to add lerping between values since it updates every second it “jumps” between value currently.

I’ve tried every combination I can think of, here is one of them:

IEnumerator Fill(TrainingMilitaryUnit trainingMilitaryUnit)
    {
        float start = progressFill.fillAmount;
        float current = percentage;

        while(progressFill.fillAmount < current)
        {
            progressFill.fillAmount = Mathf.Lerp(0f, 1f, current * Time.deltaTime);
            //progressFill.fillAmount = Mathf.Lerp(start, current, 1f * Time.deltaTime);
            yield return null;
        }
    }

Maybe I am misunderstanding Lerp and how it works. I want to lerp between my current value and the next value, ex:

current value = 0.3f
next value = 0.5f
duration = 1.0f

Here I want to lerp from current value to next value during duration .

@mrCharli3

I really don’t get what you mean by next value…

But in general your current lerp does this… if your deltaTime was 0.1, then you will get a value between 0 and 1 at 0.1, i.e. 0.1 See this page:

Edit. Here you would go from start value 0.25 to 0.5 in 4 seconds.

IEnumerator Fill()
{
    // these would be the values
    float current = 0.25f;
    float next = 0.5f;
    float duration = 4f;

    float start = current;
    float t = 0;

    while (t < 1f)
    {
        t += Time.deltaTime / duration;
        fillAmount = Mathf.Lerp(start, next, t);

        yield return null;
    }
}
1 Like

I can basically get this to work, so thanks! The one issue I have is that t += Time.deltaTime / duration does not seem to work properly for some reason, it always gets t = 1 in 1 second, while it should take 4. The math seems correct so not really sure why its doing this.

float start = progressFill.fillAmount;
        float duration = 3f;
        float t = 0;
       
        while (progressFill.fillAmount < 1)
        {
            t += Time.deltaTime / duration;
            progressFill.fillAmount = Mathf.Lerp(0f, 1f, t);
           
            yield return null;
        }

SOLVED:

float duration = 3f;
        float elapsedTime = 0;
       
        while (progressFill.fillAmount < 1f)
        {
            progressFill.fillAmount = Mathf.Lerp(0f, 1f, elapsedTime / duration);
            elapsedTime += Time.deltaTime;

            yield return null;
        }
3 Likes