Add Sounds and Effects

Add Sounds and Effects

๐ŸŽฏ Goal

Play a sound when something is hit and spawn an effect when it dies.

๐Ÿงฐ What You Need

  • Health
  • Sound Generator
  • Spawnable Effect
  • SelfDestruct on the effect prefab

๐Ÿชœ Steps

Play a sound on hit

Add an Audio Source and Add Component > Glowing Rifts > Misc > Sound Generator to the object. Fill Sounds with a few clips and turn Play On Enable off. Then hook a hitbox’s On Hit (or the Health’s On Damage Data Received) to SoundGenerator.Play. Each hit plays a random clip at a random pitch.

Spawn an effect on death

Add Add Component > Glowing Rifts > Misc > Spawnable Effect, assign your effect prefab to Effect, and hook On Death to SpawnableEffect.Spawn. Pick the dynamic Spawn (DamageData) to spawn it where the killing blow landed, or the static Spawn () to spawn it at the object.

Clean up the effect

Add Add Component > Glowing Rifts > Misc > SelfDestruct to the root of the effect prefab and set Lifetime to how long the effect plays. For a death sound, add a Sound Generator with Play On Enable on to the same prefab, so the sound plays from the effect and isn’t cut off when the object is destroyed.

๐Ÿ“ Inspector Settings: Sound Generator

FieldWhat it doesSet it to
Audio SourceSource to play through. Uses this object’s AudioSource if emptyLeave empty with an AudioSource on the object
SoundsClips to pick from at randomTwo or more variations
Pitch RangeRandom pitch range (min, max)0.9, 1.1 for subtle variation
Play On EnablePlay when enabledOn for effect prefabs, off when played from events

๐Ÿ“ Inspector Settings: Spawnable Effect

FieldWhat it doesSet it to
EffectPrefab to spawnYour particle effect

๐Ÿ“ Inspector Settings: SelfDestruct

FieldWhat it doesSet it to
LifetimeSeconds before the effect is destroyedThe effect’s duration

๐Ÿงช Example

DS_Duck hooks On Death to a Spawnable Effect that spawns DS_DuckDeathEffect. That prefab is a water splash with a Sound Generator (pitch 0.6 to 1.2, Play On Enable on) for a random squeak, and a SelfDestruct that removes it after 2 seconds. DS_Bullet_Impact is built the same way.

๐Ÿ’ก Tips

  • Spawn(DamageData) uses the hit point, which is empty after Kill(). Use Spawn() for deaths that don’t come from a hit.
  • Health.Spawn(GameObject) spawns any prefab at the object straight from an event, without a Spawnable Effect.
  • Use one Spawnable Effect per effect: one for hit sparks on On Hit, another for the explosion on On Death.

๐Ÿ”— Related