🔊
COIT20271 — Week 11

Audio in Unity

How to add background music, sound effects, and spatial audio to your game.

Fundamentals

How Audio Works in Unity

Unity's audio system has three core parts that work together:

🎵 AudioClip

The audio file itself
(.wav, .mp3, .ogg)

🔈 AudioSource

The speaker that plays
the clip in the scene

👂 AudioListener

The microphone that
hears (on Main Camera)

Key rule: Every scene needs exactly one AudioListener — Unity adds it to the Main Camera by default. Multiple AudioListeners cause warnings and overlapping sound.
Component Deep Dive

AudioSource — Key Properties

PropertyWhat It DoesTypical Use
AudioClipThe sound file to playAssign in Inspector or via script
Play On AwakeStarts playing when the scene loadsBackground music, ambient loops
LoopRepeats the clip continuouslyMusic, environmental sounds
VolumeLoudness (0 = silent, 1 = full)Balance music vs SFX levels
PitchSpeed/pitch (1 = normal)Slow-mo effects, variation
Spatial Blend0 = 2D (everywhere), 1 = 3D (positional)2D for music, 3D for world SFX
Scripting Audio

Three Ways to Play Sound

Play()

// Plays the assigned clip audioSource.Play(); audioSource.Stop();

Plays the clip set on the AudioSource. Calling Play() again restarts it. Good for music and single continuous sounds.

🔫 PlayOneShot()

// Fire-and-forget SFX audioSource.PlayOneShot(clip);

Plays a clip without interrupting anything already playing. Can overlap. Perfect for rapid SFX (gunshots, pickups, footsteps).

📍 PlayClipAtPoint()

// Sound at a world position AudioSource.PlayClipAtPoint( clip, transform.position);

Creates a temporary AudioSource at a position, plays the clip, then self-destructs. Use for explosions and destroyed objects.

Spatial Audio

2D vs 3D Sound

2D Spatial Blend = 0

Sound is heard at the same volume everywhere regardless of distance or direction. No stereo panning.

Use for:

  • Background music
  • UI sounds (button clicks)
  • Narrator / voiceover
  • Global announcements

3D Spatial Blend = 1

Sound gets louder as you approach and fades with distance. Pans left/right based on position.

Use for:

  • Enemy footsteps / growls
  • Environmental (waterfalls, fire)
  • Collectible hum / glow
  • Anything with a position in the world
3D Settings: When Spatial Blend is set to 1, configure Min Distance (full volume range) and Max Distance (silence beyond this). The Volume Rolloff curve controls how quickly sound fades between these distances.
Putting It Together

Common Audio Pattern

A typical script that plays different sounds for different game events:

public class PlayerAudio : MonoBehaviour { [SerializeField] private AudioClip pickupSound; [SerializeField] private AudioClip hurtSound; private AudioSource audioSource; void Awake() { audioSource = GetComponent<AudioSource>(); } void OnTriggerEnter(Collider other) { if (other.CompareTag("Coin")) audioSource.PlayOneShot(pickupSound); if (other.CompareTag("Enemy")) audioSource.PlayOneShot(hurtSound); } }
Guidelines

Audio Best Practices

Do

  • Keep one AudioListener per scene (on the camera)
  • Use PlayOneShot() for SFX that may overlap
  • Use separate AudioSources for music and SFX
  • Set Spatial Blend to 0 for music, 1 for world sounds
  • Use .wav for short SFX, .ogg for music (smaller file size)
  • Adjust volumes so music doesn't drown out SFX

Don't

  • Don't add multiple AudioListeners — causes doubled audio
  • Don't use Play() for rapid SFX — each call interrupts the last
  • Don't forget to assign AudioClips in the Inspector
  • Don't import huge uncompressed files for background music
  • Don't play sounds on destroyed objects — use PlayClipAtPoint()
Next up: In the tutorial, you'll build a complete game with background music, pickup SFX, spawn sounds, spatial audio, and a game-over jingle. Head to the Sound Arena tutorial!