3D Audio and AudioMixer
AudioSource, AudioListener, 3D attenuation, and routing through the mixer.
Sound in a 3D game isn’t just an mp3 in the background. It’s spatial audio: footsteps on the left, a gunshot behind you, a whisper through a wall. Unity provides a built-in 3D audio engine that’s sufficient for most tasks.
Core components
- AudioListener — the “ears” in the scene. One per scene. By default it sits on the main Camera.
- AudioSource — a sound source. Attached to a GameObject. Plays an AudioClip.
- AudioClip — an asset containing an imported wav/mp3/ogg.
public class Footsteps : MonoBehaviour
{
[SerializeField] private AudioSource source;
[SerializeField] private AudioClip[] clips;
// Called from an Animation Event
public void Footstep_AnimEvent() {
var clip = clips[Random.Range(0, clips.Length)];
source.pitch = Random.Range(0.95f, 1.05f);
source.PlayOneShot(clip);
}
}
PlayOneShot is great because it doesn’t interrupt an already-playing sound — several footsteps can overlap.
A plain source.Play() would reset the current playback.
3D vs 2D sound
AudioSource has a Spatial Blend slider: 0 = 2D (plays the same wherever the listener is), 1 = 3D (depends on distance and direction). Music and UI are usually 2D, diegetic sounds (gunshots, footsteps) are 3D.
For 3D mode you configure:
- Min Distance / Max Distance — the distance at which the volume is at its maximum and where it drops to zero.
- Volume Rolloff — the attenuation curve: Linear, Logarithmic, Custom.
- Spread — how widely the source is “spread out” in stereo.
- Doppler Level — how strongly the Doppler effect is applied during movement.
Sound in reality falls off with the inverse square of distance — that’s close to Logarithmic. Linear is easier to control, but it sounds fake. For important sounds (gunshots), use Logarithmic with fine-tuned Min/Max.
AudioMixer — routing and effects
AudioMixer is a separate asset (Create → Audio Mixer) that describes channels (groups). The standard
structure is Master → Music + SFX + Voice. Each channel has a volume that can be exposed to code:
[SerializeField] private AudioMixer mixer;
public void SetMusicVolume(float linear) {
// Convert linear volume to dB
float db = linear > 0.0001f ? Mathf.Log10(linear) * 20f : -80f;
mixer.SetFloat("MusicVolume", db);
}
Mixer parameters (“MusicVolume”) must be manually “Expose to Script” — right-click on the group’s slider → Expose.
You can attach effects to a channel: low-pass filter, reverb, compressor. Useful for:
- Underwater effect (low-pass on Master when the player is underwater).
- Room reverberation (Reverb Zone + reverb effect in the mixer).
- Ducking SFX when dialogue is playing (sidechain via duck volume).
AudioSource.PlayClipAtPoint — a one-shot source
A handy helper when you need to play a sound at a point and forget about it:
AudioSource.PlayClipAtPoint(_explosionClip, transform.position, volume: 1f);
Unity creates a temporary GameObject with an AudioSource, plays it, and destroys it. Not suitable for controlling the sound (you can’t stop it or change the volume on the fly), but it’s ideal for short effects.
AudioMixer Snapshots — mixer states
A Snapshot is a named state of all of the mixer’s sliders and effects. Through the AudioMixer you can smoothly blend between snapshots — this is the main path for “ducking” music during dialogue, changing the atmosphere when entering a room, or ducking SFX in a paused state.
In the Audio Mixer window:
- Create an AudioMixer (if you don’t have one yet) → right-click on it → Add Snapshot.
- Save several snapshots:
Default,Underwater,Paused,Combat. - In code, call
snapshot.TransitionTo(duration):
[SerializeField] private AudioMixerSnapshot defaultSnap;
[SerializeField] private AudioMixerSnapshot underwaterSnap;
public void OnEnterWater() {
underwaterSnap.TransitionTo(0.5f); // 0.5 seconds of smooth transition
}
public void OnExitWater() {
defaultSnap.TransitionTo(0.5f);
}
A snapshot doesn’t just change the volume — it switches all exposed parameters and effect states (enabling/disabling the low-pass, EQ curves, etc.). This is faster and cleaner than changing parameters one at a time from code.
Pitfalls
- One AudioListener. If two accidentally appear (for example, in both scenes during additive loading), you’ll get a warning and the sound may play oddly. Check in the editor.
- Compression on mobile. mp3/ogg gets decompressed on the fly. For short sounds, set
Load Type: Decompress On Load(decompressed once, stored as PCM in memory). For long ones —Streaming. - Forced To Mono. If a 3D sound is imported as stereo, Unity may “lose” the spatial effect — a specific stereo channel always plays from one side. Enable Force To Mono for 3D SFX.
In the next chapter — UI: HUD, menus, dialogs, and how they even exist in a 3D scene.