problem with playing footsteps audio

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

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!

So is the walking sound intended to loop or not?

Ertain | 2021-09-17 09:22

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

JCNegar | 2021-09-18 18:13

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.

Lola | 2021-09-19 08:12

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

JCNegar | 2021-09-20 16:53

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.

Lola | 2021-09-20 22:43