Skip to content

Audio

Play sound effects and music through Unity AudioSource components. Place an AudioSource on an object (assign its clip in the editor or leave it empty and set one), expose it as a public field or GetComponent<AudioSource>(), then drive it.

Audio plays locally

Sound is rendered on each player's client. A script running on everyone's machine plays the sound for everyone; spatial AudioSources are positioned in 3D so players hear them from the right place. This is the right model for world SFX. (Player voice and NPC speech are separate systems — see Voice.)

Play a sound

public AudioSource sfx;       // drag an AudioSource in, or GetComponent<AudioSource>()

void OnTriggerEnter(Collider other)
{
    if (Player.IdOf(other.gameObject) != "")
        sfx.Play();                       // play its assigned clip
}
Control
Play() · Stop() · Pause() · UnPause() transport
PlayOneShot(clip) layer a one‑off without interrupting the current clip
PlayClipAtPoint(clip, position) fire‑and‑forget a clip at a world point
clip the AudioClip to play (get/set)
isPlaying currently playing?
time playback position in seconds

Tune the sound

sfx.volume = 0.6f;          // 0..1
sfx.pitch = 1.2f;           // speed/pitch; randomize a touch for variety
sfx.loop = true;            // background music / ambience
sfx.spatialBlend = 1f;      // 0 = 2D (UI sfx), 1 = full 3D positional
sfx.minDistance = 2f;       // full volume within this radius
sfx.maxDistance = 30f;      // inaudible beyond this
sfx.mute = false;

2D vs 3D

Set spatialBlend = 0 for UI/menu sounds that should play at constant volume regardless of where the object is, and spatialBlend = 1 for world sounds that should fall off with distance. minDistance/maxDistance shape that falloff.

A simple footstep / pitch‑varied SFX

public AudioSource step;
public AudioClip[] footsteps;     // a few clips, picked at random

public void PlayFootstep()
{
    step.pitch = Random.Range(0.9f, 1.1f);     // subtle variation
    step.PlayOneShot(footsteps[Random.Range(0, footsteps.Length)]);
}

AudioClip & AudioListener

AudioClip (read‑only info): length, name, channels, frequency, samples — handy for syncing events to a clip's duration.

AudioListener is the player's "ears" (one per scene, on the camera). You can globally volume (master level, 0..1) and pause all audio:

AudioListener.set_volume(0.5f);   // master volume
AudioListener.set_pause(true);    // mute everything (e.g. a pause menu)

Quick reference

Group Calls
Transport Play · Stop · Pause · UnPause · PlayOneShot · PlayClipAtPoint
Tuning volume · pitch · loop · spatialBlend · minDistance · maxDistance · mute · time · clip
Clip info AudioClip.length · name · channels · frequency · samples
Master AudioListener.volume · AudioListener.pause

→ For talking characters see Voice; to trigger sounds from events see World events.