menu_bookDocumentation
Playing Sounds: SoundHandle, GameObject.PlaySound, and Audio Components
s&box provides several ways to play sounds: fire-and-forget at a position, controlled via SoundHandle, attached to a GameObject, or via scene components.
SoundEvent Assets
Sounds are defined as .sound files bundling audio with settings (volume range, pitch range, 3D distance, occlusion). Reference from a component:
CSHARP
[Property] public SoundEvent FootstepSound { get; set; }Or load at runtime:
CSHARP
var sound = ResourceLibrary.Get<SoundEvent>( "sounds/footsteps/concrete.sound" );Sound.Play (Static API)
CSHARP
// By reference or path
Sound.Play( FootstepSound );
Sound.Play( "sounds/explosion.sound" );
// 3D positional
Sound.Play( ExplosionSound, WorldPosition );
// With fade in
Sound.Play( AmbientSound, WorldPosition, fadeInTime: 2.0f );SoundHandle Control
Every Sound.Play returns a SoundHandle:
CSHARP
SoundHandle _engineLoop;
void StartEngine()
{
_engineLoop = Sound.Play( EngineLoopSound, WorldPosition );
_engineLoop.Volume = 0.8f;
_engineLoop.Pitch = 1.2f;
}
// Stop with optional fade
_engineLoop.Stop( 1.5f );
// Stop all sounds
Sound.StopAll( fade: 0.5f );Key properties: Position, Volume, Pitch, Distance, SpacialBlend (0=2D, 1=3D), Occlusion, IsPlaying, Paused, Time, TargetMixer.
GameObject.PlaySound (Following Sounds)
Plays a sound that follows the GameObject's position — recommended for moving objects:
CSHARP
var handle = GameObject.PlaySound( FootstepSound );
var handle = GameObject.PlaySound( FootstepSound, Vector3.Up * 64f ); // local offset
GameObject.StopAllSounds( fadeOutTime: 0.5f );Internally sets SoundHandle.Parent and FollowParent = true.
Audio Components
| Component | Description |
|---|---|
| SoundPointComponent | Fixed world point, auto-play, looping |
| SoundBoxComponent | Source constrained to a box region |
| SoundscapeTrigger | Ambient soundscape on listener enter |
| AudioListener | Overrides listening position (default is camera) |
Was this helpful?