• 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 /
avatar image
0
Question by superventure · Jul 02, 2013 at 07:46 PM · guihealthbar

Gui box size calculations 0-1

Hello. I have a gui box im using as a health bar. A gui box's size is only 0.0 to 1.0, but of course the characters health is going to be some higher integer. How do you formulate the health to be represented for the health bar? I have no idea on how to calculate health/totalHealth, ie: 231/450 to fit inside 0.0 - 1.0

bardisplay is a float, so if it equals .5, the health bar is half full

 GUI.Box ( Rect (screenPosition.x   , Screen.height - screenPosition.y - size.y , size.x * barDisplay, size.y),"");
Comment
Add comment · Show 1
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 superventure · Jul 02, 2013 at 08:20 PM 0
Share

I just saw a mistake- a gui box isn't capped at 1, but I do need it at 1.

3 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Eric5h5 · Jul 02, 2013 at 08:39 PM

Use InverseLerp, which returns 0.0 to 1.0.

Comment
Add comment · Show 4 · 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 superventure · Jul 02, 2013 at 10:03 PM 0
Share

Thank you! Can I ask you one more question? The total width, size.x * barDisplay is not one single bar, but has the ends of a new bar on each side when full. How do I stop that?

avatar image Eric5h5 · Jul 02, 2013 at 10:31 PM 0
Share

Try changing the border values etc. for the box style, or maybe use GUI.DrawTexture ins$$anonymous$$d.

avatar image superventure · Jul 03, 2013 at 01:58 AM 0
Share

after testing, I found out that the texture can only be so small before flipping over, which causes the ends to reverse. So when the health is equals zero, the box can't accomodate. Testing out some more, I tried out gui.groups, which worked because it doesn't scale the box, it crops it. I just would like to ask one thing of you- How do you reverse this so that is loads from right to left?

GUI.BeginGroup ( Rect (screenPosition.x , Screen.height - screenPosition.y - size.y , size.x * barDisplay , size.y),""); GUI.Box (Rect (0,0, size.x, size.y),"");
GUI.EndGroup ();

avatar image superventure · Jul 03, 2013 at 02:44 AM 0
Share

Figured it out. I just had to switch up the inverse lerp. Thank you for all of help. I appreciate it.

avatar image
0
Wiki

Answer by ebogguess · Jul 02, 2013 at 08:45 PM

You need to store the maxHealth for your character, as well as their current health. (float)currentHealth/(float)maxHealth will give you a value between 0.0 and 1.0 (assuming that you never let currentHealth go below zero or above maxHealth)

Comment
Add comment · 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
0

Answer by Noztradamuz · Jul 02, 2013 at 09:08 PM

Use something like this:

 GUI.Box ( Rect (screenPosition.x   , Screen.height - screenPosition.y - size.y , actualLifeAmount/maxLifeAmount, size.y),"");

of course you need to have those two variables declared and filled, you can update the actualLifeAmount in the update, or every time the character recieves damage, to avoid unnecesary loads.

Comment
Add comment · 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

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

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

18 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

Related Questions

Adding a floating health bar 2 Answers

How To Make A Floating Health Bar 1 Answer

HealthBar for 5 Objects 3 Answers

Creating A GUI health bar 0 Answers

to draw a health bar 2 Answers


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