Add Sounds and Effects
๐ฏ Goal
Play a sound when something is hit and spawn an effect when it dies.
๐งฐ What You Need
HealthSound GeneratorSpawnable EffectSelfDestructon 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
| Field | What it does | Set it to |
|---|---|---|
| Audio Source | Source to play through. Uses this object’s AudioSource if empty | Leave empty with an AudioSource on the object |
| Sounds | Clips to pick from at random | Two or more variations |
| Pitch Range | Random pitch range (min, max) | 0.9, 1.1 for subtle variation |
| Play On Enable | Play when enabled | On for effect prefabs, off when played from events |
๐ Inspector Settings: Spawnable Effect
| Field | What it does | Set it to |
|---|---|---|
| Effect | Prefab to spawn | Your particle effect |
๐ Inspector Settings: SelfDestruct
| Field | What it does | Set it to |
|---|---|---|
| Lifetime | Seconds before the effect is destroyed | The 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 afterKill(). UseSpawn()for deaths that don’t come from a hit.Health.Spawn(GameObject)spawns any prefab at the object straight from an event, without aSpawnable Effect.- Use one
Spawnable Effectper effect: one for hit sparks on On Hit, another for the explosion on On Death.