OneDayGame, update #4 – Sounds and visual effects. All done with reusable components only.
How to
Health and taking damage
Both a zombie and the player have a Health component attached that defines how much life they have.
When zombie is hit by a bullet, the Damage component attached to the bullet will decrease health value from the zombie’s Health component.
ApplyDamage(RaycastHit) method of the damage component is called when bullet hits something. Collision detection is performed by the CollisionFromRaycast component.
Bullet hit sounds
Whenever bullet hits something, the CollisionFromRaycast component calls Activate(RaycastHit) method on the OnCollisionActivate component.
Here different bullet hit effects are activated. One of them is MetalHitEffect game object.
MetalHitEffect game object consists of the AudioPlayer component and child game objects with audio clips.
A single audio clip will be played when MetalHitEffect game object gets enabled.
Highlighting body parts on bullet hit
When you shoot a zombie, the body part that is hit will become brighter for a moment. The ChangeTargetColor game object responsible for the effect is enabled when CollisionFromRaycast component detects a collision.
ChangeTargetColor game object has a RendererUpdate component attached.
RendererUpdate runs a coroutine that changes the target material color.
Zombie idle sounds
Zombie has a IdleSounds game objects that is enabled by default. IdleSounds has a Randomizer component that triggers the PlayRandomClip method of the AudioPlayer component in random intervals.
Gaining health
Player can gain health from touching red blood cells coming sometimes from the shot zombie. How often the blood cells appear is controlled by the RandomizerAwake component.
When a blood cell touches the player, the particle system will call OnParticleCollision message. This message is handled by the ParticleCollision component attached to the blood cell which then applied health value to the player.
Similarly, ParticleCollision component is placed on the player to instantiate the hearts particle system on collision with blood cell.
State manager
In idle state zombie will moan in random intervals. However if attacked, zombie will stop moaning to make hurt sounds. The current state is controlled by the StateManager extension.
StateManager holds references to State instances. Here, each state is defined within a separate game object.
When a state is activate by the StateManager all callbacks defined in the state are executed. They’re ment to change the world state without the world ever know about state change. In the Attacked state above, the callback will disable the Randomizer component so that the idle moaning sounds won’t be played.
Parenting and destroying
All game objects responsible for creating effects that last more than a frame (like sounds, particle systems, etc.) must not be destroyed before completing its task. Currently, many effects is parented to the bullet game object. When bullet hits something it’s immediately destroyed but before that happenes the effect game objects are moved somewhere else to complete its task and then destroy themselves.