Skip to content

Tips and recipes

Recipes

A distant explosion that arrives late

Turn on enable_sound_delay and start the sound with play_with_delay(). It waits distance / speed_of_sound seconds before it plays, so a blast 340 m away is heard a second after it's seen.

@onready var explosion: SpatialAudioPlayer3D = $Explosion

func detonate(at: Vector3):
    explosion.global_position = at
    explosion.enable_sound_delay = true
    explosion.play_with_delay()

Lower speed_of_sound (343 m/s by default) to make the delay easier to notice. A plain play() always plays straight away.

A looping ambience that fades in

Turn on autoplay and leave autoplay_fade_in on. The sound starts silent and fades up once the first scan of the room is done, so it doesn't blast unfiltered for a frame when the scene loads. Lower autoplay_fade_in_speed (6 by default) for a slower fade.

A muffled subtitle

Show a hint when a voice goes behind a wall:

func _ready():
    $NPCVoice.audio_occluded.connect(_on_voice_occluded)
    $NPCVoice.audio_unoccluded.connect(_on_voice_clear)

func _on_voice_occluded(_listener, wall_count):
    $Subtitles.show_muffled(wall_count)

func _on_voice_clear(_listener):
    $Subtitles.hide_muffled()

A sound you can stand inside

For a big speaker, a waterfall or anything the player can walk into, set inner_radius_panning_strength to 0. Inside the inner radius the sound is centred, and it pans normally once you walk out. Set it above 1 to exaggerate the panning instead.

Your own falloff curve

Pick USER_DEFINED and give it a Curve. X is distance through the falloff zone (0 at the inner radius, 1 at the outer edge) and Y is volume (1 to 0).

var curve := Curve.new()
curve.add_point(Vector2(0.0, 1.0))
curve.add_point(Vector2(0.5, 0.9))  # stays loud until halfway
curve.add_point(Vector2(0.8, 0.3))  # then drops fast
curve.add_point(Vector2(1.0, 0.0))

$Radio.attenuation_function = SpatialAudioPlayer3D.AttenuationFunction.USER_DEFINED
$Radio.user_attenuation_curve = curve

A performance or accessibility setting

func _on_spatial_audio_toggled(enabled: bool):
    SpatialAudioPlayer3D.set_global_effects_disabled(not enabled)

Gameplay that reacts to the room

$Radio.reverb_zone_changed.connect(func(room_size, wetness):
    if room_size > 0.75:
        print("Big open space")
    elif room_size < 0.15:
        print("Tight corridor")
)

Sound around a corner

Put the player under an agent and it works with no setup. To react when the sound loses its route:

```gdscript
@onready var agent: SpatialReflectionNavigationAgent3D = $SoundAgent

func _ready():
    agent.path_failed.connect(func(_from, _to): print("Sound is shut in"))

func _process(_delta):
    if agent.get_route_state() == SpatialReflectionNavigationAgent3D.RouteState.ROUTED:
        %Hint.text = "Around %d corners" % (agent.get_current_path_world().size() - 2)

See Sound around corners.

Keep it fast

  • Update static sounds less often. Each player re-scans every update_frequency seconds (0.2 by default). For a hum or a fountain that never moves, 0.5 to 1.0 is plenty. Players loaded together spread their scans over different frames.
  • Skip idle sounds. Turn on update_only_while_playing for sounds that spend most of their time stopped, like one-shots and pooled players. They scan straight away when they start playing.
  • Use Classic rays for simple rooms. CLASSIC casts 10 fixed rays and is the cheapest. Use FIBONACCI_SPHERE with 8 to 16 rays only where rooms are oddly shaped.
  • Turn off what a sound doesn't need. Quick footsteps rarely need reverb or air absorption.
  • Split the collision layers. Keep room rays on walls, floors and ceilings with reverb_collision_mask, and add props only to occlusion_collision_mask.
$Footsteps.room_size_reverb = false
$Footsteps.enable_air_absorption = false

Things to know

  • volume_db is set every frame. Use max_db to cap the volume and the Attenuation group to shape the fade.
  • Match max_distance and max_raycast_distance. The first is Godot's hearing range, the second is how far the plugin's rays reach. Set both to the furthest you want the sound heard.
  • The player ignores your own body. ignore_listener_body leaves out the first CharacterBody3D above the camera. If your player is a RigidBody3D or a custom rig, turn it off and keep the player's body off the occlusion layer instead.
  • The delay only applies to the start. enable_sound_delay waits before playback begins. It doesn't change a sound that's already playing, so use it for one-shots like gunshots, explosions and thunder.
  • The plugin makes its own audio bus. Each player adds a bus with a reverb and a lowpass filter, named SpatialBus# plus a number, and sends it to the bus you set. You can change bus while it plays: the player keeps its own bus and sends it to the new one. get_spatial_bus_name() returns the name of its own bus.
  • Pooling and reparenting work. The bus is removed when the player leaves the tree and made again when it comes back.