Using directional sound in 3D
In this recipe, we will attach an AudioStreamPlayer3D node to a cylinder and create a player. When the player moves toward the cylinder, 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 on 3D to add a 3D scene. Click on Scene in the main menu next to Project, then select Save Scene As, and name the scene Audio3D
.
How to do it…
Let’s first start by making a ground, a post, a player, and a camera. After that, we will add the AudioStreamPlayer3D node to the post and AudioListener to the player:
- Left-click the three vertical dots to the left of the Transform view on the viewport toolbar.
- Add Sun and Environment to the scene by left-clicking the Add Environment to Scene button at the bottom while holding down the Shift key.
- Left-click on the Node3D node and then + in the Scene tab. In the Create...