0
0
Unityframework~10 mins

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

Choose your learning style9 modes available
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.