Progress bar (i.e. Health, Mana, etc...) filling up gradually...
This question isn't a duplicate; my method is different to any I've found online thus far, so I've decided to ask my own question. My values are all integers, no floats are involved apart from the value to be drawn on the bars, which is just for appearance. What I want to do is have it smoothly change from one value to the next if adding or subtracting. Here is my code related to the bars filling:
private void HandleBar() {
healthBar.fillAmount = Map(CurHealth, 0, MaxHealth, 0, 1);
manaBar.fillAmount = Map(CurMana, 0, MaxMana, 0, 1);
xpBar.fillAmount = Map(CurrentXP, 0, MaximumXP, 0, 1);
EnemyHealthBarFill.fillAmount = Map(PlayerMovement.EnemyCurHealth, 0, PlayerMovement.EnemyMaxHealth, 0, 1);
}
private float Map(float value, float inMin, float inMax, float outMin, float outMax) {
return(value - inMin) * (outMax - outMin) / (inMax - inMin) + outMin;
}
I then run HandleBar() in Update() so it constantly keeps track of the bars and their progress. I've been playing around with it for quite a while trying to see how I could achieve this but to no avail... Thanks in advance ^__^
$$anonymous$$oved to HelpRoom.
It looks like the real Q is how to convert any integer range, like 40-60, into any other float range, like 0-1 or even 5-9. And especially to make sure the top number, like 60, is exactly 1 and not 0.99 (?) That's a straight-up program$$anonymous$$g/math Q. But it's fine for the HelpRoom area.
It might help if you wrote what the problem was. The equation in $$anonymous$$ap looks like roughly the right sort of thing.
Try Searching "unity change value over time." Lots of hits, many confusing. I think $$anonymous$$oveTowards is the best way, but some like a 0-1 lerp (the only below is a "small percent towards" lerp, which is different.)
The tricky part is you have two ranges. Suppose health goes from 60 to 55. That means fillAmount maybe goes from 0.8 to 0.73. To do it slowly, you have just remember that target value of 0.73 (maybe save it in healthTargetFill, or maybe recompute it each frame, from 55.) Then use change-over-time math to move the actual 0.8 fill amount towards that saved 0.73.
Answer by etaxi341 · Aug 30, 2016 at 02:45 PM
Well you could just Lerp between the values. So if the Value used to be 1 and is now 2 you can make this:
speed = 10f //Defines how fast it switches between the 2 values
progressbarvalue = Mathf.Lerp(progressbarvalue, newValue, Time.deltaTime * speed);
I haven't really touched Lerping at all, would it be okay if you edited my code above to include that? That way it'll help me understand and make it easier for me to get it going... thanks :)
The equation above is the semi-standard "fast then slow" trick. It won't solve any of your math problems. It's just a common Unity way to make a number track a target value over several frames. Several frames is the key thing.
Is that what you wanted? If you smash an enemy's health to 50% in one hit, do you want the health bar to drift down over a second or two? If so, do you want it to be smooth, or fast at first then slower as it gets close (which is what that formula does)?
I want it to deplete/increase at the same rate; i.e. a fixed speed, but having it appear to transition smoothly between one value and the other at a s$$anonymous$$dy rate, at the same constant speed; taking 5 damage should deplete twice as fast as taking 10 damage, in other words it is not based on the amount missing. I just want it to move smoothly between the values ins$$anonymous$$d of directly setting the length of the GUI slider to reflect the amount of HP remaining.
Hope that makes sense ^__^
Using this code granted me success in creating a gradual bar. Now I have a static health bar, and a smooth red health bar to indicate the chunk of damage that was just lost. Using this smoothing worked like a charm!
Your answer
Follow this Question
Related Questions
Make the health bar fill smoothly over time 0 Answers
Image fill with two colors 1 Answer
Black bars on top and bottom in aspect ratio? 1 Answer
Defining Rectangles on the fly 1 Answer
Circular Bar Transparent gradient 0 Answers