- Home /
Fall Damage
What is the best way to find fall damage? I have an idea that I wrote, but I'm not sure if it's the best way. Any guidance?
var v = rigidbody.velocity.y;
var d = 1;
speed = v;
if(grounded == true)
{
speed = 0;
if(v < -1.0)
{
health += v;
}
}
Answer by sacredgeometry · Aug 11, 2019 at 08:38 PM
Something like this? It works in all directions but you could easily reduce it just to the Y axis.
public Vector3 Velocity;
public Rigidbody RidgedBody;
public bool IsAlive = true;
private double _decelerationTolerance = 12.0;
void Start()
{
RidgedBody = GetComponent<Rigidbody>();
}
void FixedUpdate()
{
if(IsAlive)
{
IsAlive = Vector3.Distance(RidgedBody.velocity, Velocity) < _decelerationTolerance;
Velocity = RidgedBody.velocity;
}
}
Because you answered my silly question for 6 years ago (I have no idea why) I will reward you with some internet karma XD
Haha no way! I cant even remember what that was.
Note that Vector3.Distance(RidgedBody.velocity, Velocity) gives you the magnitude of the deceleration so you can also use it to deter$$anonymous$$e how much damage to deal.
I would just use the magnitude member function in Vector3 class. Using the distance method does exactly the same thing, but is less concise.
I tried adding this to my code but it doesn't seem to work. Could you please explain which part triggers the fall damage?
Answer by hristo991 · Jul 07, 2014 at 07:31 PM
For character controller I would use a variable that calculates the air time and then take from the player's health when touching the ground:
var minSurviveFall : float = 2f; //the time that the player can spend in the air without taking damage
var damageForSeconds : float = 1f; //damage taken for 1 second in air (for airTime = 1)
private var _controller : CharacterController;
private var airTime : float = 0;
var playerHealth : float = 10; //you can use your own variable here, or make a reference to a variable in other script
function Start () {
_controller = GetComponent(CharacterController);
}
function Update() {
if(!_controller .isGrounded)
{
airTime += Time.deltaTime;
}
if(_controller .isGrounded)
{
if(airTime > minSurviveFall)
{
playerHealth -= damageForSeconds * airTime;
}
airTime = 0;
}
}
It is checked for errors, but it's not playtested
Velocity is more realistic and compatible, if you have a flying mechanic it makes more sense to use velocity, other wise you would get hurt for hovering right above the ground for a while.
Answer by NateLaw1988 · Aug 19, 2019 at 01:45 PM
Hi I know this is an old post but incase someone new comes along like I did I have posted a video with a project download link to a heavily extended first person controller
Your answer
Follow this Question
Related Questions
Make object fall with constant speed! 4 Answers
Kinematic how to know rigidbody velocity vector? 1 Answer
How do I make my character's speed stay consistent? 0 Answers
Pushing rigidbodies around 1 Answer
Rigidbody load problem 1 Answer