0 votes

I am trying to make my character die from fall damage. So I use this code

    velocity.y += gravity * delta
fall_strength += 1

But there is one big issue. It is that as long as I am not on the floor, I take damage. I was thinking of storing the y position of the player in a variable after a jump was made and subtracting the y position after the character hits the floor. (the y position will be stored in another variable when the character hits the floor)
Since I just started with Godot, I do not know how to do this.

Also, another concern is with the negative numbers since if my character drops from a negative number, e.g. drop from y = -145 and lands on a negative number when hits the floor, e.g. y = -230, it will give me a positive number. And when my character drops from a positive number, e.g. y = 500, and lands on the ground that is a positive number, e.g. 100, means I cannot put a fixed height limit that my character can fall

How should I tackle this issue?

Godot version v3.4.4
in Engine by (31 points)

2 Answers

0 votes

https://youtu.be/Ip5SlTlF2iQ

This video should help you.

You should be able to just use the velocity.y to determine if the player should take fall damage rather than storing an extra fall_strength variable and distance fallen. velocity.y will constantly increase based on your gravity.

if velocity.y is positive it means your player is falling, if it's above a particular threshold then you can apply fall damage, you could also set different thresholds of instant death vs damage.

by (53 points)
edited by

Does this work for a kinematic body 2d???

Yes, you don't have to do the full state system in the video.

Have you got the code that detects when the player lands on the ground?

share your full code for func physicsprocess(delta): if you like and I can give better advise.

Edited this response as i got my positive and negative mixed up, velocity.y will be a positive number when falling.

physicprocess(delta) function:

func _physics_process(delta):
    get_input()
    shrink()
    if is_on_floor():
        jumps_made = 0 #double jump
        fall_strength = 0
    velocity.y += gravity * delta
    fall_strength += 1
    fall_damage_taken()
    jump()
    velocity = move_and_slide(velocity, Vector2.UP)
    print(fall_strength)

detection when on floor:

func fall_damage_taken():
    if fall_strength >= 90:
        will_die = true
    if fall_strength < 90 and not is_on_floor():
        will_die = false
    if will_die == true and is_on_floor() and PlayerGlobal.reached_checkpoint != true:
        pass #play death animation
        move_last_checkpoint()
        print("fall damage taken")
        will_die = false
    elif will_die == true and is_on_floor() and PlayerGlobal.reached_checkpoint== true:
        move_last_checkpoint()
        print("fall damage taken")
        will_die = false

have a look at the code i provided in my other answer. It should give you everything you need.

Probably something like:

func _physics_process(delta):
    get_input()
    shrink()

    velocity.y += gravity * delta

    if is_on_floor():
        jumps_made = 0 #double jump
        if fall_strength > 0:
            fall_damage_taken()
        fall_strength = 0
    else:
        fall_strength = velocity.y
    jump()
    velocity = move_and_slide(velocity, Vector2.UP)

func fall_damage_taken():
    if fall_strength >= 90:
        will_die = true
    if will_die == true:
        if PlayerGlobal.reached_checkpoint == true:
            move_last_checkpoint()
        else:
            print("move back to start?")
        will_die = false

Wow! Thanks so much for the help :D

No worries, if you plan on playing a death animation you will need to move the "movelastcheckpoint()" to inside an animation finished event.

0 votes

add a new var falling_speed = 0

    velocity.y += GRAVITY * delta


    if is_on_floor():
        on_ground = true
        if falling_speed > 300:
            print("instant death")
        elif falling_speed > 230:
            print("take damage")
        falling_speed = 0
    else:
        on_ground = false
        falling_speed = velocity.y
        if velocity.y < 0:
            $AnimatedSprite.play("jump")
        else:
            $AnimatedSprite.play("fall")


    velocity = move_and_slide(velocity, FLOOR,true)
by (53 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.