0 votes

I'm trying to add underwater sound effect and I need to detect this every frame.
But my sound effect only loops 0.1ms. How can ı fix that problem?

 func _process(delta):
        if transform.origin.y < -7.2:
            $"/root/World/UI/Effect/UnderWater".show()
            $UnderWater.play()
            vel.y += 0.1
            freeCamera = true
        else:
            $"/root/World/UI/Effect/UnderWater".hide()
            $UnderWater.stop()
            freeCamera = false
in Engine by (19 points)

1 Answer

+1 vote
Best answer

Looks like you're restarting the UnderWater sound every frame underwater.
Try:

if not $UnderWater.playing:
    $UnderWater.play()

Bonus: if you don't wanna start the sound at the beginning every time, record the playback position before you stop it:

var playback_pos = 0

if underwater:
    if not $UnderWater.playing:
        $UnderWater.play(playback_pos)
else:
    playback_pos = $UnderWater.get_playback_position()
    $UnderWater.stop()
by (4,237 points)
selected by

Thank you very much. It worked!

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.