- Home /
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;
}
}
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);
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
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.
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.
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);
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!
Your answer