0 votes

Hello there, I am working on a 2D platformer, and I wanted to add footsteps sound. Here is how I implemented it:

onready var footsteps = $AudioStreamPlayer
func _physics_process(delta):
    motion = move_and_slide(motion, Vector2.UP)

func walk_sound():
    if is_on_floor() and not footsteps.is_playing() and motion.x != 0:

Only the very first part of the audio plays and keeps looping. I double checked that the audio is not set to auto_play. I did set it to loop as I am using a 5 seconds long audio, so if player walks more than 5 seconds it'll just loop. What am I doing wrong?

Any help is appreciated thanks!

Godot version Godot 3.3.3 - Standard Version
in Engine by (96 points)

So is the walking sound intended to loop or not?

it is supposed to loop (once finished) as long as the player is actually walking

Two things:

  1. AudioStreamPlayer doesn't have an is_playing method, only a playing property.
  2. Do you see something odd if printing is_on_floor() every frame? This may be caused by your body not being on floor when you thought it was.

1- Yeah, I mixed stuff up. Thanks for that.
2- Yeah, I also thought about this, when I debugged it, isonfloor() is working perfectly, so It must be something else.

The only other option in the code you provided is ensuring motion.x actually contains the good value but I suspect you did it already.
As a side note, the correct way to compare floats is to use builtin is_equal_approx and is_zero_approx functions.

I can only speculate about this but you should also check if the stream player isn't being played from some other part of your code.

Please log in or register to answer this question.

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.