This site is currently in read-only mode during migration to a new platform.
You cannot post questions, answers or comments, as they would be lost during the migration otherwise.
+1 vote

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 (49 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 (288 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.