menu_bookDocumentation
Playing Sounds - s&box Sound Documentation
Playing Sounds
s&box provides multiple ways to play audio. From simple one-shots to complex 3D spatial audio with full control.
SoundEvent
Create reusable sound events in the editor:
- Right-click Asset Browser → Create → Sound Event
- Configure sound properties
- Reference in code:
CSHARP
// Play the sound event
Sound.Play( "sounds/gunshot.sound" );Sound.Play
Basic Playback
CSHARP
// Simple playback
Sound.Play( "sounds/ui/click.mp3" );3D Positional Sound
CSHARP
// Play at a world position
Sound.Play( "sounds/explosion.mp3", explosionPosition );
// Play attached to a GameObject (moves with it)
Sound.Play( "sounds/engine.wav", transform.WorldPosition, gameObject );Fade In
CSHARP
// Fade in over 2 seconds
Sound.Play( "sounds/ambient.wav", volume: 0.5f, fadeIn: 2.0f );SoundHandle
Get a handle for controlling playback:
CSHARP
SoundHandle handle = Sound.Play( "sounds/music.mp3" );
// Key properties
handle.Volume = 0.8f;
handle.Pitch = 1.2f;
handle.Occlusion = 0.5f;Stopping a Sound
CSHARP
// Stop immediately
handle.Stop();
// Fade out over 1 second
handle.Stop( 1.0f );Stop Everything
CSHARP
// Stop all sounds on a specific GameObject
GameObject.StopSound();GameObject.PlaySound
Convenience method for playing sounds on a GameObject:
CSHARP
// Play attached to this GameObject
GameObject.PlaySound( "sounds/hit.wav" );Stopping All Sounds on a GameObject
CSHARP
// Stop all sounds playing on this GameObject
GameObject.StopSound();Audio Components
AudioListener
Usually on the player camera - determines where sounds are heard from:
CSHARP
// Camera should have AudioListener
var listener = camera.Components.Create<AudioListener>();3D Audio Features
- Distance attenuation (quieter when far)
- Occlusion (sounds through walls)
- Directionality (stereo positioning)
- Doppler effect (for moving sources)
Was this helpful?