+2 votes

i just started out with godot. The code is for testing and learning the engine.
I struggle to figure out how to trigger an event if the player lands on the ground.
The first thing i want is to play a landing sound.
I did some research and tried the suggestions, but the result is always the same, the sound is played at the beginning of the jump or is repeatedly played while the player is on the ground.

I also tried to make a solution with connection of animation finished:

if is_on_gound() and animation_finished("jump"):

but it also didn't work

extends KinematicBody2D

export (int) var walk_speed = 150
export (int) var run_speed = 300
export (int) var jump_speed = -400
export (int) var gravity = 1200

var velocity = Vector2()

enum {LEFT, RIGHT}
var facing
var state
var anim
var new_anim
var jumping = false
var walking = false
var running = false
var current_speed

func change_state(new_state):
    state = new_state
    match state:
            new_anim = 'Idle'
            new_anim = 'IdleLeft'
            new_anim = 'Walk'
            new_anim = 'RunningRight'

func get_input():
    velocity.x = 0
    var left = Input.is_action_pressed("ui_left")
    var right = Input.is_action_pressed("ui_right")
    var jump = Input.is_action_pressed("ui_select")
    var run = Input.is_action_pressed("ui_run")

    if left or right:
        current_speed = walk_speed
        walking = true
        walking = false
    if (left or right) and run:
        current_speed = run_speed
        running = true
        running = false

    if left:
        $AnimatedSprite.flip_h = true
        velocity.x -= current_speed
        if is_on_floor():
            if walking:
            if running:
        facing = LEFT
    if right:
        $AnimatedSprite.flip_h = false
        velocity.x += current_speed
        if is_on_floor():
            if walking:
            if running:
        facing = RIGHT

    if velocity.x == 0 and is_on_floor():
        if facing == RIGHT:
    if running:
        if jump and is_on_floor():
            jumping = true
            velocity.y = jump_speed

func _process(delta):
    if position.y > 1000:
        position.y = 0

func _physics_process(delta):
    velocity.y += gravity * delta
    if jumping and is_on_floor():
        jumping = false

    velocity = move_and_slide(velocity, Vector2(0, -1))
in Engine by (14 points)

Fixed a problem because of this question. THANK YOU!

1 Answer

+3 votes

Here's a work around I've used before. It basically a bool that checks if it's only just landed.

var landing : bool

if is_on_floor():
    if landing:
        landing = false
    if !landing:
        landing = true
by (3,251 points)

Just to add for anyone who might not understand the else part.

else: # In the air 
    if !landing: # In the air and haven't landed
        landing = true
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.