Bird
Raised Fist0
Unityframework~10 mins

3D spatial audio in Unity - Step-by-Step Execution

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Concept Flow - 3D spatial audio
Start: Audio Source Setup
Attach AudioSource Component
Set AudioClip and Spatial Blend
Place AudioSource in 3D Space
Attach Audio Listener to Camera
Play Audio
Unity Calculates Audio Volume & Panning Based on Position
User Hears 3D Spatial Sound
End
The flow shows how Unity sets up and plays 3D spatial audio by positioning an audio source and listener, then calculating sound based on their positions.
Execution Sample
Unity
AudioSource audioSource = gameObject.AddComponent<AudioSource>();
audioSource.clip = myClip;
audioSource.spatialBlend = 1.0f; // 3D sound
audioSource.Play();
This code adds an AudioSource to a game object, sets a sound clip, enables 3D spatial audio, and plays the sound.
Execution Table
StepActionAudioSource PositionListener PositionSpatial BlendSound Volume & Panning
1Add AudioSource component(0,0,0)(0,0,0)0 (default)Sound is 2D, no spatial effect
2Assign AudioClip(0,0,0)(0,0,0)0Ready to play, still 2D sound
3Set spatialBlend = 1.0 (3D)(0,0,0)(0,0,0)1.0Sound will be spatialized
4Move AudioSource to (5,0,0)(5,0,0)(0,0,0)1.0Sound volume decreases, panning to right ear
5Play Audio(5,0,0)(0,0,0)1.0User hears sound from right side, volume based on distance
6Move Listener to (5,0,0)(5,0,0)(5,0,0)1.0Sound volume max, centered in ears
7Move AudioSource to (10,0,0)(10,0,0)(5,0,0)1.0Sound volume decreases, panning to right ear again
8Stop Audio(10,0,0)(5,0,0)1.0Sound stops playing
💡 Audio stops playing, ending spatial audio simulation.
Variable Tracker
VariableStartAfter Step 3After Step 4After Step 5After Step 6After Step 7Final
AudioSource Position(0,0,0)(0,0,0)(5,0,0)(5,0,0)(5,0,0)(10,0,0)(10,0,0)
Listener Position(0,0,0)(0,0,0)(0,0,0)(0,0,0)(5,0,0)(5,0,0)(5,0,0)
spatialBlend01.01.01.01.01.01.0
Sound PlayingNoNoNoYesYesYesNo
Key Moments - 3 Insights
Why does the sound volume decrease when the AudioSource moves away from the Listener?
Because Unity calculates volume based on distance between AudioSource and Listener, as shown in execution_table rows 4 and 7 where the AudioSource moves farther and volume decreases.
What does setting spatialBlend to 1.0 do?
It enables full 3D spatial audio, making sound position-dependent. This is shown in execution_table step 3 where spatialBlend changes from 0 to 1.0, enabling spatial effects.
Why does the sound become centered when the Listener moves to the AudioSource position?
Because the Listener and AudioSource are at the same position, so the sound is heard equally in both ears, as seen in execution_table step 6.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 4. What happens to the sound volume and panning when the AudioSource moves to (5,0,0)?
AVolume decreases and sound pans to the right ear
BVolume increases and sound pans to the left ear
CVolume stays the same and sound is centered
DSound stops playing
💡 Hint
Check the 'Sound Volume & Panning' column at step 4 in the execution_table.
At which step does the spatialBlend get set to enable 3D sound?
AStep 2
BStep 3
CStep 5
DStep 6
💡 Hint
Look at the 'spatialBlend' column in the variable_tracker and execution_table.
If the Listener moves to the AudioSource position, what happens to the sound according to the execution_table?
ASound volume decreases and pans left
BSound stops playing
CSound volume maxes out and is centered
DSound becomes 2D
💡 Hint
See step 6 in execution_table where Listener and AudioSource positions match.
Concept Snapshot
3D Spatial Audio in Unity:
- Add AudioSource component to GameObject
- Assign AudioClip to AudioSource
- Set spatialBlend = 1.0 for 3D sound
- Position AudioSource and AudioListener in scene
- Unity adjusts volume and panning based on distance and direction
- Play AudioSource to hear spatial sound
Full Transcript
This visual execution trace shows how 3D spatial audio works in Unity. First, an AudioSource component is added to a game object. Then, an audio clip is assigned to it. Setting spatialBlend to 1.0 enables 3D spatial sound. The AudioSource and AudioListener positions affect how the sound is heard. When the AudioSource moves away from the Listener, volume decreases and panning shifts. When both are at the same position, sound is centered and loudest. Playing and stopping audio shows the full cycle of spatial audio behavior.

Practice

(1/5)
1. What does setting spatialBlend to 1.0 on an AudioSource in Unity do?
easy
A. Disables the audio source completely
B. Makes the sound play only in stereo without 3D effects
C. Makes the sound fully 3D and affected by position in space
D. Loops the sound continuously

Solution

  1. Step 1: Understand spatialBlend property

    The spatialBlend property controls how much the sound is 3D or 2D. 0 means 2D (no spatial effects), 1 means fully 3D.
  2. Step 2: Effect of setting spatialBlend to 1.0

    Setting it to 1.0 makes the sound fully 3D, so it changes based on the listener's position and direction.
  3. Final Answer:

    Makes the sound fully 3D and affected by position in space -> Option C
  4. Quick Check:

    spatialBlend = 1.0 means full 3D sound [OK]
Hint: Remember: 0 = 2D sound, 1 = full 3D sound [OK]
Common Mistakes:
  • Confusing 0 and 1 values for spatialBlend
  • Thinking spatialBlend controls volume
  • Assuming spatialBlend disables sound
2. Which of the following is the correct way to set an AudioSource to full 3D spatial sound in C# script in Unity?
easy
A. audioSource.spatialBlend = 0;
B. audioSource.spatialBlend = 1.0f;
C. audioSource.spatialBlend = "1";
D. audioSource.spatialBlend = true;

Solution

  1. Step 1: Check correct data type for spatialBlend

    The spatialBlend property expects a float value between 0 and 1, so it must be assigned a float like 1.0f.
  2. Step 2: Identify correct syntax

    audioSource.spatialBlend = 1.0f; uses 1.0f which is a float literal in C#. Setting to 0 produces 2D sound, assigning a string is invalid, and assigning a boolean is invalid.
  3. Final Answer:

    audioSource.spatialBlend = 1.0f; -> Option B
  4. Quick Check:

    Use float value 1.0f for spatialBlend [OK]
Hint: Use float (1.0f) for spatialBlend, not string or bool [OK]
Common Mistakes:
  • Assigning string or boolean instead of float
  • Using 0 instead of 1.0f for full 3D
  • Forgetting the 'f' suffix for float literals
3. Consider this Unity C# code snippet:
AudioSource audioSource = gameObject.AddComponent<AudioSource>();
audioSource.spatialBlend = 1.0f;
audioSource.minDistance = 1f;
audioSource.maxDistance = 10f;

// Listener is 5 units away from audioSource
float volume = audioSource.GetOutputData(0, 0);
What is true about the sound volume heard by the listener at 5 units distance?
medium
A. Volume will be between max and min volume due to distance attenuation
B. Volume will be zero because listener is beyond maxDistance
C. Volume will be unaffected by distance because spatialBlend is 0
D. Volume will be at maximum because listener is within minDistance

Solution

  1. Step 1: Understand minDistance and maxDistance roles

    minDistance is where sound is at full volume. maxDistance is where sound fades to zero. Between these, volume decreases gradually.
  2. Step 2: Analyze listener distance

    The listener is 5 units away, which is between minDistance (1) and maxDistance (10), so volume is partially attenuated (reduced), not full or zero.
  3. Final Answer:

    Volume will be between max and min volume due to distance attenuation -> Option A
  4. Quick Check:

    Distance between min and max means volume fades [OK]
Hint: Volume fades between minDistance and maxDistance [OK]
Common Mistakes:
  • Assuming volume is max at any distance
  • Confusing minDistance and maxDistance roles
  • Ignoring spatialBlend value
4. You wrote this code to make a sound 3D but it still sounds flat (2D):
AudioSource audioSource = gameObject.AddComponent<AudioSource>();
audioSource.spatialBlend = 0;
audioSource.Play();
What is the error and how to fix it?
medium
A. Set spatialBlend to 1.0f to enable 3D sound
B. Call audioSource.Stop() before Play()
C. Add an AudioListener component to the game object
D. Set volume to 0.5f for 3D effect

Solution

  1. Step 1: Identify spatialBlend value effect

    Setting spatialBlend to 0 means the sound is 2D (flat), so no 3D spatial effect.
  2. Step 2: Correct the spatialBlend value

    To make sound 3D, set spatialBlend to 1.0f so Unity applies spatial audio processing.
  3. Final Answer:

    Set spatialBlend to 1.0f to enable 3D sound -> Option A
  4. Quick Check:

    spatialBlend = 1.0f enables 3D sound [OK]
Hint: Use spatialBlend = 1.0f for 3D sound, not 0 [OK]
Common Mistakes:
  • Leaving spatialBlend at 0 for 3D sound
  • Thinking volume controls 3D effect
  • Forgetting AudioListener is needed but not the cause here
5. You want to create a game where a sound fades smoothly as the player moves away, but stays loud when close. Which combination of AudioSource settings achieves this best?
hard
A. Set spatialBlend = 0, minDistance = 0, maxDistance = 0
B. Set spatialBlend = 0.5f, minDistance = 10, maxDistance = 10
C. Set spatialBlend = 1.0f, minDistance = 15, maxDistance = 2
D. Set spatialBlend = 1.0f, minDistance = 2, maxDistance = 15

Solution

  1. Step 1: Choose spatialBlend for full 3D effect

    Setting spatialBlend to 1.0f ensures the sound is fully 3D and affected by distance.
  2. Step 2: Set minDistance and maxDistance correctly

    minDistance should be smaller than maxDistance so sound is loud close (within 2 units) and fades out smoothly by 15 units.
  3. Step 3: Eliminate incorrect options

    Setting spatialBlend = 0, minDistance = 0, maxDistance = 0 disables 3D sound, reversing min and max distances (min > max) is invalid, setting min and max equal causes no fade.
  4. Final Answer:

    Set spatialBlend = 1.0f, minDistance = 2, maxDistance = 15 -> Option D
  5. Quick Check:

    Full 3D + proper min/max distances = smooth fade [OK]
Hint: minDistance < maxDistance with spatialBlend = 1 for smooth fade [OK]
Common Mistakes:
  • Swapping minDistance and maxDistance values
  • Using spatialBlend less than 1 for full 3D
  • Setting minDistance equal to maxDistance