• 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 SpeedTutor · Aug 17, 2013 at 11:36 AM · javascripthealthbarrounding

Rounding values for GUI

Hello, This is my crude code for managing a set of values based on certain things that the player might do. Now as you can see from the screenshot my values are within single floating point decimal places from the Time.deltaTime value, is there anyway to round these to the nearest whole number?

alt text

 var currentHealth = 100;
 var maxHealth = 100.00;
 
 var currentMana : float = 100.0;
 var maxMana : int = 100;
 
 var currentStamina : float = 100.0;
 var maxStamina : int = 100;
 
 var healthBarLength = 0.0;
 
 private var chMotor : CharacterMotor;
 
 function Start()
 {
     healthBarLength = Screen.width / 6;
     chMotor = GetComponent(CharacterMotor);
 }
 function Update()
 {
     AdjustCurrentHealth(0);
     AdjustCurrentMana(0);
     
     //Normal Mana regeneration
     
     if(currentMana >= 0)
     {
         currentMana += Time.deltaTime;
     }
     
     if(currentMana >= maxMana)
     {
         currentMana = maxMana;
     }
     
     if(currentMana <= 0)
     {
         currentMana = 0;
     }
     
     if(Input.GetKeyDown("f"))
     {
         AdjustCurrentMana(-20);
     }
     
     var controller : CharacterController = GetComponent(CharacterController);
     
     if(controller.velocity.magnitude > 0 && Input.GetKey(KeyCode.LeftShift))
     {
         currentStamina -= Time.deltaTime;
         chMotor.movement.maxForwardSpeed = 10;
         chMotor.movement.maxSidewaysSpeed = 10;
     }
     
     else
     {
         chMotor.movement.maxForwardSpeed = 6;
         chMotor.movement.maxSidewaysSpeed = 6;
     }
     
     if(controller.velocity.magnitude > 0 && Input.GetKey(KeyCode.LeftShift) && currentStamina == 0)
     {
         chMotor.movement.maxForwardSpeed = 6;
         chMotor.movement.maxSidewaysSpeed = 6;
     }
     
         //Stamina Regeneration
     if(controller.velocity.magnitude == 0 && (currentStamina >= 0))
     {
         currentStamina += Time.deltaTime;
     }
     
     
     if(currentStamina >= maxStamina)
     {
         currentStamina = maxStamina;
     }
 }
 function OnGUI ()
 {
     //Icons for GUI draw
     GUI.Box(new Rect(5, 30, 40, 20), "HP");
     GUI.Box(new Rect(5, 50, 40, 20), "Mana");
     GUI.Box(new Rect(5, 70, 40, 20), "Stam");
     
     //Health / Mana / Stamina Bars
     GUI.Box(new Rect(45, 30, healthBarLength, 20), currentHealth + "/" + maxHealth);
     GUI.Box(new Rect(45, 50, healthBarLength, 20), currentMana + "/" + maxMana);
     GUI.Box(new Rect(45, 70, healthBarLength, 20), currentStamina + "/" + maxStamina);
 }
 
 function AdjustCurrentHealth(adj)
 {
     currentHealth += adj;
         
     healthBarLength = (Screen.width / 6) * (currentHealth / maxHealth);
 }
 
 function AdjustCurrentMana(adj)
 {
     currentMana += adj;
 }
script.jpg (295.2 kB)
Comment
Add comment
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

1 Reply

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

Answer by Bunny83 · Aug 17, 2013 at 11:42 AM

Just use ToString with a format string

Something like this:

     currentHealth.ToString("0")

or

     currentHealth.ToString("000")

Comment
Add comment · Show 3 · 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 SpeedTutor · Aug 17, 2013 at 11:52 AM 0
Share

Sounds cool, so just like:

 function Start ()
 {
 currentHealth.ToString("0")
 }

It doesn't seem to make any difference. $$anonymous$$aybe I'm not meant to put it in start?

avatar image Bunny83 · Aug 17, 2013 at 12:02 PM 0
Share

No, The function name "ToString" should give you a hint... The function converts the value into a string and returns that string.

     GUI.Box(new Rect(45, 30, healthBarLength, 20), currentHealth.ToString("0") + "/" + maxHealth.ToString("0"));
avatar image SpeedTutor · Aug 17, 2013 at 12:07 PM 0
Share

I see, I'll remember this! Very helpful, thanks very much for your time.

All the best.

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

16 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

Related Questions

How Do I Make A Health Bar 6 Answers

Health Bar 0 Answers

My healtbar is not change when i look another enemy 0 Answers

C# to Java UI Health Bar 1 Answer

Setting Scroll View Width GUILayout 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