U
tab 04World & Game Loop7 lessons — tap to open
05 / 07 · 10 min
World & Game Loop

Sound and atmosphere

your project so far

Assets/

Scenes/

Arena.unity

Terrain

Scripts/

PropScatter.csGameAudioSettings.csFootstepPlayer.cs

Prefabs/

Knight prefab

Animations/

AC_Knight

Audio/

MainMixer

you already havethis lesson adds

Apart from the music and the click you added in tab 01, everything you have built so far is silent. The Knight walks without footsteps, casts skills without a sound, and the forest has no wind in it.

Turn the sound off in any game you like and it instantly feels cheap. Audio is half of atmosphere, and it is the half most beginners skip entirely.

animated diagram

Three layers, one mixer, and 3D falloff on anything that has a position in the world.

The three layers

  • Music — one looping track, 2D, never changes volume with distance. Set Spatial Blend to 0.

  • Ambience — wind, birds, a river. Also 2D for the global bed, 3D for a specific waterfall.

  • SFX — footsteps, hits, skill casts. Always 3D, always short, always pooled.

Import settings decide your memory budget

  1. 1

    Music and long ambience → Load Type = Streaming, Compression = Vorbis, Quality 60–70%.

  2. 2

    Short SFX → Load Type = Decompress On Load, Compression = PCM or ADPCM. They must fire instantly.

  3. 3

    Untick Preload Audio Data on anything that is not needed in the first second of the scene.

  4. 4

    Force To Mono on every 3D sound — stereo is pointless when Unity is going to position it anyway, and it halves the size.

The Audio Mixer — one volume slider per layer

  1. 1

    Window▸Audio▸Audio Mixer → create a mixer named MainMixer.

  2. 2

    Add three groups under Master: Music, Ambience, SFX.

  3. 3

    Right-click each group's Volume → Expose to script, then rename the parameter to MusicVol, AmbienceVol, SfxVol.

  4. 4

    Point every AudioSource's Output at the right group. Now one slider controls a whole category.

GameAudioSettings.cs — the settings menu talks to the mixer
using UnityEngine;
using UnityEngine.Audio;

public class GameAudioSettings : MonoBehaviour
{
    [SerializeField] private AudioMixer mixer;

    /// slider gives 0..1, the mixer wants decibels
    public void SetMusic(float value01)    => SetVolume("MusicVol", value01);
    public void SetAmbience(float value01) => SetVolume("AmbienceVol", value01);
    public void SetSfx(float value01)      => SetVolume("SfxVol", value01);

    void SetVolume(string param, float value01)
    {
        // -80 dB is silence. Log10 makes the slider feel linear to the ear.
        float db = value01 <= 0.0001f ? -80f : Mathf.Log10(value01) * 20f;
        mixer.SetFloat(param, db);
        PlayerPrefs.SetFloat(param, value01);
    }

    void Start()
    {
        SetMusic(PlayerPrefs.GetFloat("MusicVol", 0.6f));
        SetAmbience(PlayerPrefs.GetFloat("AmbienceVol", 0.8f));
        SetSfx(PlayerPrefs.GetFloat("SfxVol", 1f));
    }
}

3D sound that actually sounds 3D

  • Spatial Blend = 1 turns a source fully 3D. At 0 it is heard everywhere at the same volume.

  • Volume Rolloff = Linear is easier to tune than Logarithmic, especially for small levels.

  • Min Distance is the radius where it stays full volume; Max Distance is where it hits silence. Typical: 2 m and 20 m.

  • There must be exactly one AudioListener in the scene, and it belongs on the camera.

Footsteps that do not sound like a machine gun

FootstepPlayer.cs — on the Knight, driven by Animation Events on AC_Knight
using UnityEngine;

public class FootstepPlayer : MonoBehaviour
{
    [SerializeField] private AudioSource source;
    [SerializeField] private AudioClip[] clips;          // 4–6 variations

    private int last = -1;

    /// called from an Animation Event on the walk / run clips
    public void Step()
    {
        if (clips.Length == 0) return;

        // never play the same clip twice in a row
        int i = Random.Range(0, clips.Length);
        if (clips.Length > 1 && i == last) i = (i + 1) % clips.Length;
        last = i;

        source.pitch = Random.Range(0.92f, 1.08f);      // tiny pitch jitter
        source.PlayOneShot(clips[i], Random.Range(0.8f, 1f));
    }
}
  • Four clips plus random pitch is the difference between 'a game' and 'a prototype'. It costs ten minutes.

  • PlayOneShot lets sounds overlap; source.Play() cuts the previous one off.

  • Animation Events put the step exactly on the frame the foot lands — a timer never gets this right.

Where to get sound

  • freesound.org — huge, free, but check the licence on each file (CC0 is the safe one).

  • Unity Asset Store has free SFX packs; filter by price 0.

  • Record your own with a phone. Footsteps on gravel, a door, a keyboard — it works better than you expect.

  • Never ship a track you found on YouTube. Copyright strikes on a mobile store take the whole app down.