terminalCode Example

Playing Sounds

calendar_today May 3, 2026 schedule ~1 min read person patrickjr verified 50

Playing Sounds in s&box

s&box provides both 2D and 3D sound playback through code and components.

Quick Sound Playback

CSHARP
// Play a 2D sound (UI, music, etc.)
Sound.Play("ui.click");

// Play a 3D positional sound
Sound.Play("weapons.shotgun.fire", WorldPosition);

// With volume control
Sound.Play("ambient.wind", position, volume: 0.5f);

Sound Handle

Store the handle to control playback:

CSHARP
SoundHandle music = Sound.Play("music.background");
music.Volume = 0.3f;
music.Stop();

Networked Sound (RPC)

CSHARP
[Rpc.Broadcast]
public static void PlaySoundAllClients(string soundName, Vector3 position)
{
    Sound.Play(soundName, position);
}

Audio Components

Add these to GameObjects for advanced audio:

ComponentPurpose
SoundPointComponentPositional 3D sound at a point
SoundBoxComponentSound within a box area
SoundscapeTriggerAmbient soundscapes
AudioListenerOverride hearing position
VoiceComponentVoice chat
LipSyncComponentLip sync from audio

SoundPointComponent Example

CSHARP
public class Weapon : Component
{
    [Property] public SoundEvent FireSound { get; set; }
    
    void Fire()
    {
        // Create sound point
        var soundPoint = Components.Create<SoundPointComponent>();
        soundPoint.SoundEvent = FireSound;
        soundPoint.Volume = 1.0f;
        soundPoint.Play();
        
        // Auto-destroy after sound finishes
        soundPoint.GameObject.DestroyAsync(FireSound.Duration);
    }
}

Sound Events

Define reusable sound events in the editor:

  1. Create .sound asset
  2. Configure: file path, volume, pitch variation, distance attenuation
  3. Reference in code: [Property] public SoundEvent MySound { get; set; }
Was this helpful?