why landing sound not working?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By MegamanXfangamer

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()

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.

Ertain | 2022-05-09 23:35

:bust_in_silhouette: Reply From: cm

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()