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 · Aug 02, 2013 at 11:25 AM · multiplayerfpsdamageround

Rounding up Damage to an Int

Hey, I have a health script which allows the player to take Damage via RPC. I also have an Armor system, but this system sometimes spits out decimals as a result. I was wondering if I could round Health so that no decimals are created? (Note: Just ignore MyPlayer. if its just confusing you. It doesn't really mean anything special.)

 //If this function is triggered, subtract the amount of damage
 //Recieved from health and armor 
 if(MyPlayer.DamageDone > 0)
 {
     if(MyPlayer.armor > 0)
     {
         //Half of Damage is Subtracted off Health
         MyPlayer.health -= Damage/2;
         //Full Damage is subracted from the Armor 
         MyPlayer.armor -= Damage;
         //Tell us that the player has recieved damage
         Debug.Log("Damage Recieved");
         //Reset the Damage Notification
         MyPlayer.DamageDone --;
         //Set Regen Timer back to "0" so that the player cant
         //Regenerate while being hit
         MyPlayer.RegenTimerDuration = 0.0f;
     }
 }
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

2 Replies

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

Answer by CHPedersen · Aug 02, 2013 at 11:31 AM

The function Mathf.CeilToInt accomplishes exactly that. You pass whatever you want rounded to it, and it returns the number rounded up to the nearest integer, like so:

     float damage = 0.5f;
     int damageRoundedUp = Mathf.CeilToInt(damage);
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 Borzi · Aug 02, 2013 at 12:40 PM 0
Share

Hmmm...yes but Damage is Random and sent in. Could you show me how you mean that? Wait, here is the function so you get an idea of the whole thing:

 [RPC]
         void Client_TakeDamage ( float Damage )
 {
     //If this function is triggered, subtract the amount of damage
     //Recieved from health and armor 
     if($$anonymous$$yPlayer.DamageDone > 0)
     {
         if($$anonymous$$yPlayer.armor > 0)
         {
            //Half of Damage is Subtracted off Health
            $$anonymous$$yPlayer.health -= Damage/2;
            //Full Damage is subracted from the Armor 
            $$anonymous$$yPlayer.armor -= Damage;
            //Tell us that the player has recieved damage
            Debug.Log("Damage Recieved");
            //Reset the Damage Notification
            $$anonymous$$yPlayer.DamageDone --;
            //Set Regen Timer back to "0" so that the player cant
            //Regenerate while being hit
            $$anonymous$$yPlayer.RegenTimerDuration = 0.0f;
         }
 }
 

Where and how would I use $$anonymous$$athf.CeilToInt? Note: in the end I want health and armor to be int not Damage

avatar image CHPedersen · Aug 02, 2013 at 01:15 PM 0
Share

Similar to that other question about clamping values, you would replace the lines that deal with health and armor with code that does the rounding. Your full function would become this:

 void Client_TakeDamage ( float Damage )
 {
     //If this function is triggered, subtract the amount of damage
     //Recieved from health and armor 
     if($$anonymous$$yPlayer.DamageDone > 0)
     {
         if ($$anonymous$$yPlayer.armor > 0)
         {
             //Half of Damage is Subtracted off Health
             $$anonymous$$yPlayer.health -= $$anonymous$$athf.CeilToInt(Damage / 2);
             //Full Damage is subracted from the Armor 
             $$anonymous$$yPlayer.armor -= $$anonymous$$athf.CeilToInt(Damage);
             //Tell us that the player has recieved damage
             Debug.Log("Damage Recieved");
             //Reset the Damage Notification
             $$anonymous$$yPlayer.DamageDone--;
             //Set Regen Timer back to "0" so that the player cant
             //Regenerate while being hit
             $$anonymous$$yPlayer.RegenTimerDuration = 0.0f;
         }
      }
 }

Don't just copy paste this as is. Think about what it does and how it works. In the previous question, we discussed that "A+=B" means "A=A+B", i.e. addition first, then re-assigning. It's the same with "-=" for subtraction. So,

 $$anonymous$$yPlayer.health -= $$anonymous$$athf.CeilToInt(Damage / 2);

In english, means, "Divide damage by two, then round the resulting value up to an integer. Then subtract it from $$anonymous$$yPlayer.health, and finally, reassign the result back to $$anonymous$$yPlayer.health".

Note that the rounding is always UP to nearest integer, not just to closest integer. So if Damage is 2.2, then Damage / 2 = 1.1, which then gets rounded back up to clean 2.

avatar image Borzi · Aug 02, 2013 at 01:20 PM 0
Share

Oh okay now I see....this makes sense! I oversaw that at first sorry, but it works, thank you very much. I appreciate your help and taking the time to explain everything. Had quite some trouble figuring this out.

avatar image
1

Answer by amphoterik · Aug 02, 2013 at 12:45 PM

If you just want to remove decimals without any particular rounding type, you can just cast to an int:

 MyPlayer.health -= (int)(Damage/2);
 
 MyPlayer.armor -= (int)Damage;

This will turn the value to an int and remove any decimals. If you want to round up, do like CHPederson said and use the math ceil function:

 MyPlayer.health -= Mathf.CeilToInt(Damage/2);
     
 MyPlayer.armor -= Mathf.CeilToInt(Damage);

Comment
Add comment · Show 2 · 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 · Aug 02, 2013 at 01:21 PM 0
Share

Thank you, this is also the correct answer and also shows how to do it in 2 methods. I would like to mark your answer as correct as well but CHPederson already answered it correctly..but, thumbs up!

avatar image amphoterik · Aug 02, 2013 at 03:09 PM 0
Share

No problem. Glad I could help.

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

Cant Kill Multiplayer Player In Unity, Help! 0 Answers

For my multiplayer FPS, the 3rd person character is not moving 0 Answers

yield WaitForSeconds c# 4 Answers

[Closed]multiplayer camera script 2 Answers

Calling the Experts! Problems with Networking: Client/Server Architecture 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