Exploring directional sound in 2D
In this recipe, we will attach AudioStreamPlayer2D to a block and create a player. When the player moves toward the block, the sound file will play and get louder as the player gets closer.
Getting ready
For this recipe, open Godot 4 and start a new project called Chapter 7
. In the Scene tab, click 2D to add a 2D scene. Click on Scene in the main menu next to Project, then select Save Scene As, and name the scene Audio2D
.
How to do it…
We start by creating the background, the box with the AudioStreamPlayer2D node, and the player:
- Left-click on the Node2D node and then + in the Scene tab. In the Create New Node window, type
mesh
in the Search box and then select MeshInstance2D to create the node in the Scene tab. Left-click the Create button. - Right-click the MeshInstance2D node in the Scene tab and rename it
Background
. - In the Inspector window, click on <empty> to the right of Mesh and select New BoxMesh. ...