0 votes

Hello everyone! I have a problem with my landing sound, and that is that I want that when my platform character lands a landing sound is reproduced but it does not work, it is repeated infinitely and I do not know how to solve it, please I ask for your help.

here is the code piece

func _physics_process(delta):
    if Velocity.x == 0 or Velocity.y == 0 && is_on_floor() && !is_on_wall():
        $Landing.play()
Godot version Godot_v3.4.3
in Engine by (36 points)

I would suggest a state machine, but that's a bit more than what's probably needed for this problem. Maybe you could have a variable which is assigned when the player lands on the floor. After that, check for whether the sound was played. It could look something like this:

func _physics_process(delta):
    if Velocity.length() == 0 and is_on_floor() and not is_on_wall() and not sound_has_played:
         sound_has_played = true
         $Landing.play()

Once the player jumps, assign false to sound_has_played.

1 Answer

0 votes

You could keep track of whether your player was on the floor in the previous frame, and then trigger the sound only when the player is on the floor but wasn't in the last frame. Something like this:

var was_on_floor: bool = true

func _physics_process(delta: float) -> void:
    if is_on_floor() && !was_on_floor:
        $Landing.play()
    was_on_floor = is_on_floor()
by (272 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 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 webmaster@godotengine.org with your username.