// Apply damage to player AFTER completing the fall
if(falling == false && fall > fallFromHeight)
{
health = Mathf.Round(health - fall * damage);
fall = 0;
}
Yup, that's how easy it is to calculate falling damage and round it to the nearest integer in Unity!
"Falling" is a boolean that is set to true whenever the player is off of the ground. "Fall" is a variable that should be pretty obvious what it contains.
After the player has finished falling and if the fall was higher than, say, 8 feet, the following line executes -
health = Mathf.Round(health - fall * damage)
- which determines the height of the fall, then subtracts only a small percentage of that value to the player's health, thus resulting in damage, which is different depending on how far the player has fallen.
A few issues come up with the balancing it all out, and I'm sure there's better ways to do this, but this was the easiest I could find and think of. One problem is that the damage seems to have set values; for example, if I fall for two feet, instead of 8 damage I get 4, but if I fall for four feet then I get 8 damage, and if I fall for eight feet then I get 16 damage... this isn't really a problem, as it is random numbers in-between, but more often than not it seems to be exact doubles.
"The ships hung in the air in much the same way that bricks don't."
~ Douglas Adams