Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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 Darksang · Sep 29, 2015 at 07:30 PM · uibarfill

Make the health bar fill smoothly over time

I want to make my health bar gradually fill (not the chunk of the bar corresponding to the recovered health just increasing the bar in an instant) as the health is regenerated, currently, a base player has 1 health per second of regeneration, so I would like to make the bar increase the portion corresponding to 1 point of health (or the current health regen) over a second.

That's the code I have right now to fill the bar:

 public class UIHealthBar : MonoBehaviour {
 
     public PlayerStats stats;
     
     public RectTransform healthBarArea;
     public RectTransform healthBarFill;
     
     public Text healthBarValues;
     
     void Update () {
         float healthPercent = Mathf.Clamp((float)stats.health.StatCurrentValue /                                    (float)stats.health.StatBaseValue, 0f, 1f);
         
         float newRightOffset = -healthBarArea.rect.width + healthBarArea.rect.width * healthPercent;
         
         healthBarFill.offsetMax = new Vector2 (newRightOffset, healthBarFill.offsetMax.y);
         
         healthBarValues.text = string.Format("{0} / {1}", 
                                              stats.health.StatCurrentValue, 
                                              stats.health.StatBaseValue);
     }
 }


I have thought and tried a lot of things, but I can't make it work as I want, what would you recommend?

Comment
Add comment · Show 3
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 bubzy · Sep 29, 2015 at 07:45 PM 1
Share

look up mathf.lerp

avatar image Darksang bubzy · Sep 29, 2015 at 09:39 PM 0
Share

What should I lerp and where?

avatar image Darksang Darksang · Sep 29, 2015 at 10:11 PM 0
Share
 healthBarFill.offset$$anonymous$$ax = new Vector2($$anonymous$$athf.Lerp(healthBarArea.offset$$anonymous$$ax.x, newRightOffset, Time.time), healthBarFill.offset$$anonymous$$ax.y);

Tried to lerp between the current x offset and the new one, but does not work, what am I doing wrong?

0 Replies

· Add your reply
  • Sort: 

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.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Progress bar (i.e. Health, Mana, etc...) filling up gradually... 1 Answer

Managing multiple images fillamount by one function? 1 Answer

Image fill with two colors 1 Answer

C# Creating a bar that decrements but can be incremented when pieces are matched 1 Answer

Instantiate panel background not showing 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges