1
\$\begingroup\$

Here is the setup:

> KinematicBody2D (Player)
- > Camera2D
- > Node2D (SoundFX)
- - > Timer (1 sec)
- - > AudioStreamPlayer2D (Beep)

It works fine at the beginning of the scene, but the sound fades out when the player moves away. Whyt is that and how to fix it?

I have tried to make the AudioStreamPlayer2D a child of a CanvasLayer node, without success.

\$\endgroup\$
2
\$\begingroup\$

To have sound without attenuation, the simple solution is to use AudioStreamPlayer instead of AudioStreamPlayer2D.

The AudioStreamPlayer2D node is a AudioStreamPlayer with a 2D position. The maximum distance and attenuation curve controls how strong it sounds based on its distance to the screen center. The horizontal offset from the center of the screen is used for panning.

And sadly, there isn't a Listener2D (no such node exist), that we could place on the Player. AudioStreamPlayer2D always work with the screen center.

Of course, given the scene tree, I'd expect the AudioStreamPlayer2D to stay on the center of the screen. However, if the Player is moving away from the center of the screen (which depends on how the Camera2D moves) or if the AudioStreamPlayer2D is moving relative to it, then you would have attenuation.

Thus, for a solution using AudioStreamPlayer2D, I'd suggest to make it child of the current Camera2D, so that it is centered on the screen (regardless of whether or not the player moves away from the center of the screen)… As long as you don't change current Camera2D, or move the AudioStreamPlayer2D relative to it.

For a more complex scenario, let us say you want the AudioStreamPlayer2D centered on the screen when the current Camera2D is the one on the Player, but centered on the Player when you are using another camera. To do that, we can put a script on the AudioStreamPlayer2D to move it. Give it the NodePath of the Camera2D and the Player. The script will check if the Camera2D is current, and if it is, set its own global position to match the Camera2D. If it isn't, then set its won global position to match the Player. Note that this requires to always unset the current Camera2D when you set a new one.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.