menu_bookDocumentation
Playing Sounds: SoundEvent, SoundHandle, and GameObject Audio in s&box
s&box provides several ways to play sounds in code: fire-and-forget at a world position, control via SoundHandle, attach to a GameObject, or use scene components.
SoundEvent Assets
Most sounds are defined as SoundEvent assets (.sound files) bundling audio files with volume, pitch, 3D distance, and occlusion settings.
CSHARP
[Property] public SoundEvent FootstepSound { get; set; }
var sound = ResourceLibrary.Get<SoundEvent>( "sounds/footsteps/concrete.sound" );Sound.Play
Primary static API returning a SoundHandle:
CSHARP
Sound.Play( FootstepSound );
Sound.Play( "sounds/explosion.sound" );
Sound.Play( ExplosionSound, WorldPosition ); // 3D positional
Sound.Play( AmbientSound, WorldPosition, fadeInTime: 2.0f ); // fade inSoundHandle Properties
| Property | Description |
|---|---|
| Position | World-space position |
| Volume | Volume multiplier (default 1.0) |
| Pitch | Pitch multiplier (default 1.0, 2.0 = one octave up) |
| Distance | Max audible distance (default 15000) |
| SpacialBlend | 0 = 2D, 1 = 3D (default 1.0) |
| Occlusion | Whether geometry blocks the sound |
| Paused | Pause/resume without stopping |
CSHARP
SoundHandle _engine = Sound.Play( EngineLoop, WorldPosition );
_engine.Volume = 0.8f;
_engine.Pitch = 1.2f;
_engine.Stop( 1.5f ); // fade outGameObject.PlaySound
Plays a sound that follows the GameObject's position every frame:
CSHARP
var handle = GameObject.PlaySound( FootstepSound );
var handle = GameObject.PlaySound( FootstepSound, Vector3.Up * 64f ); // local offset
GameObject.StopAllSounds( fadeOutTime: 0.5f );Audio Components
| Component | Description |
|---|---|
| SoundPointComponent | Plays at a fixed world point with auto-play and looping |
| SoundBoxComponent | Source position constrained to a box region |
| SoundscapeTrigger | Blends ambient soundscape when listener enters trigger |
| AudioListener | Overrides listening position (default is camera) |
Was this helpful?