+2 votes

Hi there !

I have a problem that I never had before.

I want my player to make a footstep sound when he walks. So I simply made this in my process function, where my motion is managed:

    if motion.x != 0 or motion.y != 0:
    $grass_step.play()

the problem that I have is that the sound only plays when i release the key. It's really weird. At first i was thinking that maybe the file is too heavy and I need to preload it, but it plays exactly at the second i release the input...

Any advice ? thanks !!

Godot version 3.3
in Engine by (47 points)

2 Answers

+3 votes

Hello,

calling play on an AudioStreamPlayer resets it to the begining each time, so in your case the sound only plays at the moment you release the key because it is precisely the moment you stop asking it to rewind every frame.

You can get around this by checking if your AudioStreamPlayer is playing before re-ordering it to play the sound (using AudioStreamPlayer.playing).

by (736 points)

My friend, you just saved my game !

Thanks a lot :)

–2 votes

Is the beginning of your sound effect quiet?

Looking at the code, I'd expect it to begin playing your sound effect, from the start infinitely until motion isn't happening anymore (motion != Vector2.ZERO just btw ;) ), then play the sound clip all the way through.

Also as a side-note to @Lola's comment: you can get the current play position in an AudioStreamPlayer with get_playback_position()and pass the result as an argument to play(from_position). This probably isn't necessary for your use case, but is still a good-to-know :).

by (93 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.