Sound Component
Abstract base class for all vehicle sound effects. Handles multiple clips, spatial audio and master volume integration.
Features
- Multi-clip support - Random selection for audio variation
- Automatic AudioSource management - Creates and configures AudioSources during initialization
- Master volume integration - Per-component volume control with global master volume
- 3D spatial audio - Configurable falloff and doppler effects
- Audio Mixer support - Route through Unity Audio Mixer for advanced processing
- LOD-aware - Automatic enable/disable based on vehicle LOD state
Key Properties
| Property | Description |
|---|---|
baseVolume |
Base volume level (0-1) before master volume is applied. Final volume = baseVolume * masterVolume |
clips |
List of audio clips this component can use. Components supporting multiple clips choose randomly for variation. Single-clip components use only the first clip |
source |
The Unity AudioSource component. Created automatically during initialization |
Abstract Members (Override Required)
Derived classes must implement:
| Member | Description |
|---|---|
ContainerGO |
GameObject where the AudioSource is created |
AudioMixerGroup |
Audio Mixer Group for routing |
Priority |
AudioSource priority (0-256, lower = higher priority) |
Virtual Properties (Optional Override)
| Property | Description |
|---|---|
InitLoop |
Whether AudioSource should loop (default: false) |
InitClip |
Initial clip to assign (default: first clip in list) |
InitVolume |
Initial volume (default: baseVolume) |
InitSpatialBlend |
2D/3D blend, 0=2D, 1=3D (default: from SoundManager) |
InitDopplerLevel |
Doppler effect intensity (default: from SoundManager) |
InitPlayOnAwake |
Auto-play on initialization (default: false) |
InitializeWithNoClips |
Allow initialization without clips (default: false) |
Methods
Play / Stop
Play() // Play current clip
Play(int clipIndex) // Play specific clip by index
PlayRandomClip() // Play random clip from list
Stop() // Stop playback
Volume & Pitch
SetVolume(float volume) // Set volume (master volume applied automatically)
SetPitch(float pitch) // Set pitch (0-5 range)
Clip Management
AddDefaultClip(string clipName) // Load default clip from Resources
Usage Example
[Serializable]
public class MyEngineSound : SoundComponent
{
public override GameObject ContainerGO => vehicleController.soundManager.engineSourceGO;
public override AudioMixerGroup AudioMixerGroup => vehicleController.soundManager.engineMixerGroup;
public override int Priority => 50;
public override bool InitLoop => true;
protected override void VC_Update()
{
if (!Active) return;
float engineRPM = vehicleController.powertrain.engine.RPM;
SetPitch(engineRPM / 1000f);
SetVolume(baseVolume * Mathf.Clamp01(engineRPM / 5000f));
}
}
Built-in Sound Components
Vehicle Physics 2 includes these SoundComponent implementations:
- EngineRunningComponent
- EngineStartStopComponent
- EngineFanComponent
- ExhaustPopComponent
- TransmissionWhineComponent
- GearChangeComponent
- TurboFlutterComponent
- ForcedInductionComponent
- HornComponent
- BlinkerComponent
- CrashComponent
- AirBrakeComponent
- ReverseBeepComponent
- SuspensionBumpComponent
- WheelSkidComponent
- WheelTireNoiseComponent
See SoundManager for configuration and usage of these components.
Notes
- Always use
SetVolume()instead of directly modifyingsource.volumeto ensure master volume is applied - AudioSources are automatically created in
VC_Initialize() - Components are LOD-aware via VehicleComponent inheritance
- Uses VehicleComponent lifecycle:
VC_Initialize→VC_Enable/VC_Disable→VC_Update/VC_FixedUpdate
Inherits from VehicleComponent.