0 votes

I have a simple script:

if Input.is_action_just_pressed("left_mouse_button"):
    $ShootSound.play()

The only problem is, the sound sometimes doesn't play. Seems like it didn't load and after spamming LMB, the sound is working but if you don't use it for sometime, it doesn't play anymore until you try to use it again a few times.

Godot version 3.3.2
in Engine by (55 points)

1 Answer

0 votes

The reason for this could be that the sound is already playing. If left click/"fire" button is pressed repeatedly, the sound will be played from the beginning each time. What may have to be checked is whether the sound is playing:

if Input.is_action_just_pressed("left_mouse_button"):
    if not $ShootSound.is_playing():
        $ShootSound.play()

It may also be a good idea to put in a small "cool down" for the weapon, e.g. 0.2 or 0.3 seconds.

by (2,080 points)

Remember that to play a sound multiple times, you need multiple AudioStreamPlayer nodes. This can be done easily by using an autoload such as this one: https://github.com/Calinou/escape-space/blob/master/autoload/sound.gd

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 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 webmaster@godotengine.org with your username.