- Home /
Script to detect a death fall
Hi there here is my problem. I imported a skecth up map in unity. This map is one big component. That map has sevral big bloc from different heights. I want to script in c# something that would detect if my hero fall from more than 10 meters he dies instantly. I really don't know where to strart of and how to do it.
Thanx
Depending on the situation, its often a good idea to place either 1 large invisible box, that when collided with will cause 'death', or place several boxes in addition. Otherwise you will need to keep track of all possible fall locations and calculate distance - 10 meters, which is not a catch-all solution.
Yeah thats the point I just cant keep track of all possible fall locations. $$anonymous$$y map is kind of an open world.
Answer by jessee03 · Feb 17, 2012 at 02:48 AM
once your character is falling start a timer and if it exceeds that time and is still falling then activate death.
He asked from a specified height, not for falling for so long of a time. While you can use a function of max distance, gravity and time, it is not always guaranteed to be accurate.
Well I was trying to say calculate how long in would take to fall 10 meters... Then if it exceeds that then kill the player.
I know. But it can be inaccurate if there are extra forces involved (especially drag from the 'air').
Answer by rabbitfang · Feb 17, 2012 at 03:34 AM
An easier thing to do would be to use a maximum fall velocity threshold. Every frame you would check to see if the velocity would be over this and then if it is, set a flag that tells the script to 'kill' the object on collision with the ground.
If you did want to do it with distance, every frame, check the Y (vertical) velocity and if it is less than (moving down) a really small negative value (to account for inaccuracies with floating point values) (say < -0.01f), set the current Y value in a starting height variable. When the collision Y value (where they hit the ground) is greater than the allowed fall distance (10 meters in this case), kill the hero.
I would recommend using acceleration (change of velocity between frames) as that gives the most realistic simulation of impact damage (from a fall, for instance) (it is the stopping that kills you, not the fall).
There are issues involved with using a time or distance approach because in theory, if you fall 100 meters in 24 hours (falling over that entire interval but moving really slowly), both of those methods would most likely kill you but it would be incredibly unrealistic. If you ever have upward forces applied to your hero (for example, from knockback from gunfire, or drag from the 'air'), then you should not use time or distance methods.
P.S. Sketch models are known to not be very efficient when it comes to using them as a mesh for a model. They are not optimized when exported.
Your answer
![](https://koobas.hobune.stream/wayback/20220613064147im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Character disappears when dies. 3 Answers
AI Awareness 2 Answers
Death script problem 2 Answers
Death when a character hits the ground from a certain height 0 Answers
Death on impact with water 1 Answer