I am using the first person blueprint to create a game. I want my character to take fall damage depending on the height it is at. There are many platforms at different heights and I can’t seem to figure out an effective way to do this. I have tried many different blueprints and approaches, but none seem to work correctly. I also have a HUD that displays the health, which would lower the more damage is taken. Then when the health is to 0, the actor is destroyed and it would take to a widget that says GAME OVER Press f to respawn. If anyone has any thoughts and ideas, I would greatly appreciate them!
“depending on the height it is at”.
The easiest approach I can think of is to use the Z velocity magnitude (speed of fall) @ time of event ‘on land’ to determine amount of damage. Faster impact with the ground = more damage.
Don’t know if there is an easier way but…
You can read the “Is Falling” variable from the CharacterMovement component and then apply damage based on the Z velocity when the character stops falling.
You can change -500 to whatever you want the minimum fall velocity to be.
Remember that it’s not the fall that kills, it’s the sudden stop. So tracking time in air won’t be enough, as people said previously you have to track velocity changes. I personally suggest to track velocity magnitude change (i.e. all directions). This way damage will apply even if you get slammed against a wall.
One thing that may or may not be useful - be careful when doing your calculations on ragdolls. Their velocity is pretty ****ed up and can give wrong readings.
In the first person case your character will pretty much never have a high negative Z velocity while walking, so using the method I provided you can safely assume that velocity is right before the stop (so it’s sudden).
What TK said would work. Alternatively, you could use a looping timer and +1 to a fall integer every half second. If the fall integer is to big, then apply damage by multiplying against the fall integer.
This is on the event:
This is the function “Add to Fall” you see the timer calling:
This should give you more precise control over when and where your character is taking damage. It also takes into consideration things like flying, hence why it resets if your velocity goes above 0. You should still do a maximum velocity check, and this is by no means bug-proof.
In this example, falling any more than half a second will cause damage. Where the branch is, you can set the fall integer check bigger than 1.
In 4.8 there are some updates to how StartFalling works and On Walking Off Ledge. StartFalling no longer zeros out your Z velocity and Event On Walking off ledge gives you the Impact Normal and Location of where you walked off allowing you to easily calculate damage based off distance between last floor and current floor.
While not foolproof, the following script does take into consideration jetpacks.
Hah yup I saw that when I scrolled up and edited my post
As a point of fact, be aware that getting velocity via EventHit will not work, at least for falls on characters. When characters hit, their Z velocity is zeroed (so they don’t slide around on landing as a true physics object would), meaning the Z velocity you get in EventHit’s execution path will always be zero (at least it has been when I’ve tried this).
The solution is to use Event Tick to set the Z velocity component to some Float Var AS LONG AS it’s above some acceptable value (like 500 or whatever). When the player hits the ground, you use this float var for damage calcs, and then Set it back to 0. This way it remains at 0 (i.e. applying no damage) until the player again gains aerial speed in excess of the damage threshhold you chose ahove.
Another approach would be to save the player’s vector in a variable when the jump button is pressed, then when the player hits the ground, get his vector again, and compare it with the first vector to get the difference between them. Then use that value and do some math with it to come up with a nice damage model, result of that you subtract from players health and voila.
Anyway that’s how I did it with a project of mine some time ago.
This has far reaching consequences for allot of physics interactions. I’ve always found velocity to be wonky, and I try to avoid using it for little more than projectiles and basic <0>0 checks.
Everything while walking has been coded to be 2D, Z velocity is not stored and gravity is not applied. I disabled the all the points where Z velocity gets Zeroed out in a custom movement component, it has had some strange consequences.
Zeroing out Z velocity also has a lot of unwanted side effects as well like not being able to jump up ramps/hills or fly against angled surfaces and losing all momentum when you brush against a surface while falling
In short, velocity in UE4 is wonky
And it behaves differently between PIE, AMI, and the cooked version. It’s been getting better with each version of Unreal though, so let’s hope!
I’ve been trying to work with this, it’s simple and easy to work with. But if I don’t want it to print a string, instead, use a widget to display Game Over and destroy the actor, how would I do that? (I’m pretty new to UE). Thank you!
Even easier wasn’t aware there was a landed event.
You can spawn a UMG widget (Create Widget node) then Add to Viewport.
Thanks AlphaSierra216, but then how to handle “how many damage” to apply?
Event Any Damage for the actor, you will need some math to subtract the damage amount from your player health variable and Apply Damage from the True part of the Branch.
For the Damage amount you would probably want it to scale based off how fast the player is going, something like ABS(Velocity.Z-FallDamageThreshold)*1.5, just throwing numbers out
This is what I did.
That is what is good for me. My character has a shield of 70 points and a Health of 40 points and it will survive a 15 meter fall.(Bearly)
It will deal a max of 300 damage if your terminal velocity is set to default
Just change the Divide number to get a better result for yourself.