# Fall damage of rigid body

Hi all, i want to make a fall damage of player which is rigid body. For example fall damage will start if the player falls from more than 5 units and every unit after 5 would give more damage for example 5 units = 1 damage but 10 = 5 and so on so on.

Can anyone help with this :? should i use raycast, rigid body velocity.y or any other idea how to make it work ?

Thanks !

I would use velocity. For even more dynamic performance you can use the realitive velocity from the collision data.

so something like this :?
if (GetComponent.().velocity.y > 5)
{
Debug.Log(“you fell”);
}

but when i tried this out it doesnt actually calculate fell distance its just velocity so even when i jump and velocity is >5 it says you fell so if i added damage it will add damage while im in air and i want it to add damage when you fell on ground or other collision

Edit : I have used transform.position.y instead and it works

A method I used before was to have a ‘timer’ that starts ticking off while in air.

``````float timeInAir = 5;

void Update()
{
//Here we decrease the time in air from 5
//Grounded is used by Rigidbody controller
if(!grounded)
timeInAir -= 1 * Time.deltaTime;

//Increase the time in air to reach 5 each time we're on ground
if(grounded && timeInAir < 5 && timeInAir > 0)
timeInAir = 5;

//Making the player die when it reaches 0
if(timeInAir <= 0)
Debug.Log("You Died!");

//Or just damage player on a "checkpoint", in this case I use 3
if(timeInAir == 3 && grounded)
PlayerHealth -= 30;

}
``````

You can also use a switch case to check for those “checkpoints” I was talking about!
You can find more about switch here: https://unity3d.com/learn/tutorials/modules/beginner/scripting/switch

Dude I was working with the Steam VR plug in so I didn’t have the “isGrounded” variable and I was going crazy trying to find a solution for fall damage. Yours is classy af and works perfectly. I would just coment that instead of if (GetComponent.().velocity.y > 5) you shoud change the sign to if (GetComponent.().velocity.y < 5).

Thanks!