• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by DubstepDragon · Aug 30, 2016 at 01:41 PM · barfillprogressover timegradually

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 ^__^

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Owen-Reynolds · Aug 30, 2016 at 01:51 PM 0
Share

$$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 programming/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.

avatar image Owen-Reynolds · Aug 31, 2016 at 04:23 PM 0
Share

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.

1 Reply

· Add your reply
  • Sort: 
avatar image
2

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);
Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image DubstepDragon · Aug 30, 2016 at 07:14 PM 0
Share

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 :)

avatar image Owen-Reynolds DubstepDragon · Aug 30, 2016 at 08:08 PM 0
Share

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)?

avatar image DubstepDragon Owen-Reynolds · Aug 31, 2016 at 04:09 PM 0
Share

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 ^__^

Show more comments
avatar image Stealthygolem · Nov 06, 2017 at 04:55 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

56 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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

Calculate Radial Fillamount 2 Answers

Circular Bar Transparent gradient 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges