Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by Borzi · Jun 23, 2013 at 10:45 AM · floatintmathfroundinghealth

Rounding to the nearest int as a Result of a Function?

Hello, I a have a problem with a small Health Regeneration. The part that I'm having problems with works on the following variables:

 //This will be set and equal to InitialHealth (but modified) 
 var health : float;
 
 //Amount of Health added to Regenarte (determines the regeneration speed)
 var regenSpeed : float = 0.5;
 //Time till Regeneration
 var TimeToRegenerate : int = 10;
 //The timer to get the whole thing to restart
 private var regenTimer : float = 0.0;

And this is the function for the regeneration: function Regenerate ()

 function Regenerate ()
 { 
 //And the player has lost some Health
 if(health < InitialHealth)
     {
         //If the timer is bigger then the Time to Regenerate,
         //Start with the Regeneration process
         if(regenTimer < TimeToRegenerate)
             { 
             //Start counting up to surpass the Time to Regernate
             regenTimer += Time.deltaTime; 
             }
         else
             {
             //Regenerate 
             health += regenSpeed;        
             }
             }
         else
              {
              //Reset the Timer so you can Regenerate next time the
              //Player looses Health
              regenTimer = 0.0; 
              }
 }    

This will give me a Decimal, but is it possible to round the result of health after this function takes place/while it takes place so that the health float is always equal to the nearest int?

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
2

Answer by AlucardJay · Jun 23, 2013 at 10:50 AM

  • http://docs.unity3d.com/Documentation/ScriptReference/Mathf.FloorToInt.html

  • http://docs.unity3d.com/Documentation/ScriptReference/Mathf.CeilToInt.html

  • http://docs.unity3d.com/Documentation/ScriptReference/Mathf.RoundToInt.html

  • parseInt( floatValue );

Edit :

Here is a little demo script to show what each command does. Create a new scene, create an empty gameObject and attach the below script.

Hit Play.

Now in the Inspector, change the value of myFloat. Watch the output in each GUI Box.

  • Mathf.FloorToInt : Returns the largest integer smaller to or equal to float.

  • Mathf.CeilToInt : Returns the smallest integer greater to or equal to float.

  • Mathf.RoundToInt : Returns float rounded to the nearest integer.

All of the work is done in Update, I added the GUI to give a visual display instead of debug logging an output. So this is how to use one of the commands :

 myFloatFloor = Mathf.FloorToInt( myFloat );

now the demo script :

 #pragma strict
 
 var myFloat : float = 0.0;
 
 private var myFloatFloor : float = 0.0;
 private var myFloatCeiling : float = 0.0;
 private var myFloatRound : float = 0.0;
 
 
 function Update() 
 {
     // Floor to Int
     myFloatFloor = Mathf.FloorToInt( myFloat );
     
     // Ceiling to Int
     myFloatCeiling = Mathf.CeilToInt( myFloat );
     
     // Round to Int
     myFloatRound = Mathf.RoundToInt( myFloat );
 }
 
 
 function OnGUI() 
 {
     // Floor to Int
     GUI.Box( Rect( 10, 10, 100, 30 ), "Floor to Int" );
     GUI.Box( Rect( 10, 45, 100, 30 ), "" + myFloatFloor.ToString() );
     
     // Ceiling to Int
     GUI.Box( Rect( ( Screen.width * 0.5 ) - 50, 10, 100, 30 ), "Ceiling to Int" );
     GUI.Box( Rect( ( Screen.width * 0.5 ) - 50, 45, 100, 30 ), "" + myFloatCeiling.ToString() );
     
     // Round to Int
     GUI.Box( Rect( Screen.width - 110, 10, 100, 30 ), "Round to Int" );
     GUI.Box( Rect( Screen.width - 110, 45, 100, 30 ), "" + myFloatRound.ToString() );
 }
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 Borzi · Jun 23, 2013 at 01:10 PM 0
Share

I have looked at these but I have no real experience with $$anonymous$$athf...can you tell me how I would have to apply them on this script for example or what type of function I would need to add?

avatar image AlucardJay · Jun 23, 2013 at 01:29 PM 0
Share

I have updated the answer with some more information and a demo script. Play around with it, and you should see how each command modifies the float.

avatar image Borzi · Jun 23, 2013 at 09:16 PM 0
Share

Okay thanks, but it looks like this does not work for this example as the float variable "health" is not an actual value and merely becomes "Initial Health" on startup. I guess i'll just have to think of an alternative solution.

avatar image AlucardJay · Jun 24, 2013 at 01:06 AM 0
Share

Your comment makes no sense. the float variable "health" is not an actual value : what does this even mean?

The question was : This will give me a Decimal, but is it possible to round the result of health after this function takes place/while it takes place so that the health float is always equal to the nearest int?

 round the result of health = $$anonymous$$athf.FloorToInt( the result of health );



avatar image Borzi · Jun 26, 2013 at 10:33 AM 0
Share

Hmmm...well this doesn't work for me. Sorry for the late reply I did not see that you commented. Anyway, whatever I try, it does not work...this is how I did it (please be sure to point out any mistakes I made):

I added the Following Variable

 //This is used to Round "health"
 private var healthRound : float;

I tried to make Health Equal to Health (I think this is where the mistake lies)

 function Update ()
 //Set that healthRound always equals health
 healthRound = health;

And then I added the line of Code that I was supposed to add into the update function

 healthRound = $$anonymous$$athf.RoundToInt( health );

It would be awesome if you could help me...sorry, I am still learning how to program.

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

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 to round or convert doubles? 2 Answers

Periodic fraction gets converted to int 1 Answer

Why is this Int lagging when its subtracting something over a period of time? 1 Answer

How to submit float score as time to Game Center? 1 Answer

Turn float to int 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