Falling damage?

I am assuming that my poor math skills are to blame, but I have never been able to understand how to implement a system that damages a player or NPC that falls too far in a game. The only thing I have ever come up with is a constant downwards ray cast that measures how far the object is from the ground and then measure the change over time. Surely there is a less complicated way!



Thanks in advance for any help! :slight_smile:

-Matt

Not really, you could also check for the time you spend in the air.

What is the best way to know when the object is airborne? That is the part I am having trouble with. If I had a reliable and efficient way to know when an object leaves the ground and when it lands, then it would be pretty easy to calculate the damage, but I am afraid of ending up with some really inefficient code to do something that should not happen all that often (if the player is reasonably smart anyway).



Thanks!

Matt

How about checking if the Y value of the current location of your character is different from the calculated height of the terrain at that same position?



not sure what the code is for it but something like this:



if char.Y > heightmap.Y

in the air

else

on the ground



Would that not maybe save you from doing a ray cast etc. and also if you subtract the 2 Y values when leaving the ground and again when touching the ground you could get the falling distance and multiply that by a damage value per unit or something.



Sorry I do not know how to code that but just an idea. I think there are helper functions for getting the height of the terrain at a given x,z pos?



Cheers

Ah thinking about it after posting, you probably need the direction when leaving the ground and again when touching the ground and take the amount of time spent in the air like Normen said and come up with a distance travelled somehow… grr this is hard :X

I’m glad I am not the only one who thinks it is difficult! :slight_smile: I like your idea of getting the Y position of the ground directly beneath the object , though you would have to account for an object moving down and forward as well.

Raycasting is the “normal” solution.

Isnt there a onGround() method in a rigidbody control?

I will have to deal with that problem, too, in the future. I thought i could write a customControl which checks if the player is onGround.If it is NOT i set a flag and store the actual y position of the player. As soon as the onGround method returns true again i check the reached y position and i thought about calculating the needed time for the average speed.

After that it would need only a speed based damage calculating…



Could that work and would that be performant?