Overview
Sound, alongside graphics, is a crucial element for game immersion. It serves many roles: BGM (background music) setting the mood, SE (sound effects) as feedback for player actions, and 3D spatial sound providing information about where footsteps or gunshots originate.
Unity's audio system consists of three main components:
- Audio Clip: The audio file asset itself (.wav, .mp3, .ogg, etc.).
- Audio Source: A "speaker" component that plays sound in the scene. Configures which
Audio Clipplays, volume, pitch, etc. - Audio Listener: A "microphone" component that "hears" sounds. Only one exists per scene, typically attached to the main camera. Sound perception changes based on distance and direction from
Audio Source.
This article covers basic methods for playing BGM and SE using these components.
Playing BGM (Background Music)
BGM typically starts when a scene begins and loops continuously.
- Create dedicated GameObject: Create an empty GameObject to manage BGM, named "BGMManager" or similar.
- Add Audio Source: Add an
Audio Sourcecomponent to this GameObject. - Configure Audio Source: Set these key properties in Inspector:
AudioClip: Drag and drop the BGM audio file.Play On Awake: Enable. Audio plays when scene starts.Loop: Enable. Audio automatically repeats when finished.Volume: Adjust BGM volume (0 to 1 range).
That's all—BGM automatically loops when the scene loads. Since BGM typically doesn't need positional information, set Spatial Blend to 2D (0).
Playing SE (Sound Effects)
SE typically plays once at specific events: button clicks, character jumps, attack hits, etc.
Method 1: Triggering Audio Source Playback
Add an Audio Source component to objects that play SE (buttons, characters) and trigger playback from script.
- Prepare Audio Source: Add
Audio Sourcecomponent to the SE-playing GameObject. - Configure Audio Source:
AudioClip: Can leave empty (specify from script).Play On Awake: Disable.Loop: Disable.
- Trigger from script: Use
Play()orPlayOneShot()methods.
using UnityEngine;
[RequireComponent(typeof(AudioSource))]
public class SoundEffectPlayer : MonoBehaviour
{
public AudioClip jumpSound;
public AudioClip hitSound;
private AudioSource audioSource;
void Start()
{
audioSource = GetComponent<AudioSource>();
}
public void PlayJumpSound()
{
// PlayOneShot plays new sound without stopping current playback—ideal for SE
// Second argument adjusts volume
audioSource.PlayOneShot(jumpSound, 0.8f);
}
public void PlayHitSound()
{
audioSource.PlayOneShot(hitSound);
}
}
PlayOneShot() is especially useful when multiple effects may play rapidly on the same Audio Source.
Method 2: Creating Temporary Audio Source
Another common approach creates a temporary Audio Source at the sound position, then destroys it after playback.
public static class AudioHelper
{
// Helper method to play clip once at specified position
public static void PlayClipAtPoint(AudioClip clip, Vector3 position, float volume = 1.0f)
{
if (clip == null) return;
GameObject go = new GameObject("OneShotAudio");
go.transform.position = position;
AudioSource source = go.AddComponent<AudioSource>();
source.clip = clip;
source.volume = volume;
source.spatialBlend = 1.0f; // Play as 3D sound
source.Play();
// Destroy GameObject after playback finishes
Destroy(go, clip.length);
}
}
// Usage example
// AudioHelper.PlayClipAtPoint(explosionSound, transform.position);
3D Sound and Spatial Blend
The Audio Source's Spatial Blend property adjusts how sound is heard between 2D and 3D.
0(2D): Sound plays equally from both speakers regardless ofAudio Listener(camera) position. Suitable for UI sounds and BGM.1(3D): Sound appears to originate from theAudio Source's position in the scene. Gets louder asAudio Listenerapproaches, quieter when distant, with automatic left/right panning. Suitable for positionally important sounds: footsteps, explosions, ambient sounds.
Edit the Volume Rolloff curve to customize distance-based attenuation.
Summary
Unity's audio system is intuitive and powerful.
Audio Listener(microphone) on main camera,Audio Source(speaker) on sound-producing objects.- For BGM, add
Audio Sourceto dedicated GameObject withPlay On AwakeandLoopenabled. - For SE, disable
Play On AwakeandLoop, trigger withPlayOneShot()from script. - Use
Spatial Blendto differentiate 2D sound (UI, BGM) from 3D sound (ambient, SE).
Good sound design dramatically enhances player experience. Master these basics to build rich soundscapes in your games.