【Unity】Unity Audio: Implementing BGM, Sound Effects, and 3D Sound

Created: 2025-12-07

Sound enhances game immersion. Learn Unity's audio basics: playing BGM, triggering sound effects (SE) on button clicks or collisions, and using 3D spatial sound.

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:

  1. Audio Clip: The audio file asset itself (.wav, .mp3, .ogg, etc.).
  2. Audio Source: A "speaker" component that plays sound in the scene. Configures which Audio Clip plays, volume, pitch, etc.
  3. 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.

  1. Create dedicated GameObject: Create an empty GameObject to manage BGM, named "BGMManager" or similar.
  2. Add Audio Source: Add an Audio Source component to this GameObject.
  3. 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.

  1. Prepare Audio Source: Add Audio Source component to the SE-playing GameObject.
  2. Configure Audio Source:
    • AudioClip: Can leave empty (specify from script).
    • Play On Awake: Disable.
    • Loop: Disable.
  3. Trigger from script: Use Play() or PlayOneShot() 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 of Audio Listener (camera) position. Suitable for UI sounds and BGM.
  • 1 (3D): Sound appears to originate from the Audio Source's position in the scene. Gets louder as Audio Listener approaches, 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 Source to dedicated GameObject with Play On Awake and Loop enabled.
  • For SE, disable Play On Awake and Loop, trigger with PlayOneShot() from script.
  • Use Spatial Blend to 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.