AUDIO • CORE SOUND SYSTEM
UNITY AUDIOSOURCE & AUDIOLISTENER COMPLETE GUIDE
Master the foundational audio components of Unity: 3D spatial blend curves, logarithmic distance rolloff, PlayOneShot vs Play, and Audio Mixer grouping.
1. The AudioListener and AudioSource Relationship
Unity's audio engine operates on a microphone-and-speaker model:
AudioListener (The Microphone)
Acts as the player's ears in the 3D world. Attached to the Main Camera. There can only be ONE active AudioListener per scene.
AudioSource (The Speaker)
Attached to any GameObject (Gun, Vehicle, Explosion) that emits sound. Calculates stereo panning and volume attenuation based on distance to the AudioListener.
2. Spatial Blend: 2D vs 3D Sound
- Spatial Blend = 0.0 (2D Sound): Ignores the 3D world position entirely. The sound plays at constant volume in both headphone channels. Ideal for background music, menu clicks, and voiceover narration.
- Spatial Blend = 1.0 (3D Positional Sound): Attenuates volume based on distance and pans left/right as the emitter moves around the camera. Ideal for enemy gunshots, vehicle engines, and missile explosions.
3. PlayOneShot vs Play in C#
Calling audioSource.Play() restarts or overrides the current playing clip on that source. To play overlapping sound effects without cutting off previous audio (e.g. rapid machine gun fire), use PlayOneShot():
using UnityEngine;
[RequireComponent(typeof(AudioSource))]
public class WeaponAudioController : MonoBehaviour
{
[Header("Sound Clips")]
[SerializeField] private AudioClip fireSound;
[SerializeField] private AudioClip reloadSound;
private AudioSource audioSource;
private void Awake()
{
audioSource = GetComponent<AudioSource>();
audioSource.playOnAwake = false;
audioSource.spatialBlend = 1.0f; // 3D sound
}
public void FireWeapon()
{
// Add random pitch variation to prevent repetitive audio fatigue!
audioSource.pitch = Random.Range(0.92f, 1.08f);
// PlayOneShot allows rapid overlapping gunshot sounds on one AudioSource
audioSource.PlayOneShot(fireSound, 0.85f);
}
public void Reload()
{
audioSource.pitch = 1.0f;
audioSource.PlayOneShot(reloadSound, 1.0f);
}
}
Related Audio Guides & Error Fixes
Frequently Asked Questions
This warning occurs when two active cameras in the scene both have an
AudioListener component attached (e.g. your Main Camera and a newly instantiated Minimap Camera). Disable or remove the AudioListener on secondary cameras so only the primary camera has one.
Logarithmic Rolloff mimics real-world physics where volume drops steeply as distance increases initially, then tapers off gradually. Linear Rolloff provides an even decrease in volume until reaching
maxDistance, which is often preferable for arcade game mechanics where you want sounds to remain clearly audible within a specific zone.