#Health bar not moving

1 messages · Page 1 of 1 (latest)

fallen tendon
#

I'm trying to fix a health bar and I forgot where it came from. The bar is not sliding in the unity editor. When damage is taken a red background bar should follow the front bar with some delay to cause a shrink effect, and when healed the back bar should set to what the new health is going to be and then the front bar slide up to the backbar (also, should be green when adding health)

    {
        float fillF = frontHungerBar.fillAmount;
        float fillB = backHungerBar.fillAmount;
        float hFraction = Hunger / 100;
        if (fillB > hFraction)
        {
            frontHungerBar.fillAmount = hFraction;
            backHungerBar.color = Color.red;
            HunlerpTimer += Time.deltaTime;
            float percentComplete = HunlerpTimer / chipSpeed;
            percentComplete = percentComplete * percentComplete;
            backHungerBar.fillAmount = Mathf.Lerp(fillB, hFraction, percentComplete);
        }

        if (fillF < hFraction)
        {
            backHungerBar.color = Color.green;
            backHungerBar.fillAmount = hFraction;
            HunlerpTimer += Time.deltaTime;
            float percentComplete = HunlerpTimer / chipSpeed;
            percentComplete = percentComplete * percentComplete;
            frontHungerBar.fillAmount = Mathf.Lerp(fillF, backHungerBar.fillAmount, percentComplete);
        }
    }```
oblique cave
fallen tendon
#

at percentComplete?

fossil egret
oblique cave
#

I assumed Hunger / 100 would be the problem, didnt really read all of the code but thats very common problem when people do division to get number between 0 and 1

fossil egret
#

ah I did not see that line

#

Yes if Hunger is an int that would certainly yield unexpected results

fallen tendon
#

Hunger is a float

#

I believe the problem lies in the if statements but I honestly have no clue what fillB> hFraction and FillF < hFraction does

fossil egret
#

Did you not write this code?

fallen tendon
#

No I did not

#

It was a few years back so I have no idea where I got it from

fossil egret
#

I see. Well, from the looks of the code, it appears to interpolate the fillAmount up or down based on the current fill and the desired fill

#

or rather, I should say, two different bars. front and back, whose current values are fillF and fillB respectively

fallen tendon
#

I assume that fillF < hFraction is when it should be going down?

fossil egret
#

I'm not sure why one is < and the other is >

fallen tendon
#

And what about the Mathf.Lerp, what do the first 2 variables do? Im not sure when fillB, fillF, or hFraction should be used

fossil egret
#

Well Lerp is just a formula