OneDayGame Update #4 – Sounds and Visual effects

OneDayGame, update #4 – Sounds and visual effects. All done with reusable components only.

OneDayGame main project page

How to

Health and taking damage

Both a zombie and the player have a Health component attached that defines how much life they have.

ODG_Update4_Health
Callback field allows execute callbacks on every health change.

When zombie is hit by a bullet, the Damage component attached to the bullet will decrease health value from the zombie’s Health component.

ODG_Update4_Damage
Damage component will search for the Health component to decrease its health value.

ApplyDamage(RaycastHit) method of the damage component is called when bullet hits something. Collision detection is performed by the CollisionFromRaycast component.

ODG_Update4_CollisionFromRaycast
When CollisionFromRaycast component detects a collision, it executes defined callbacks passing in the RaycastHit info.

Bullet hit sounds

Whenever bullet hits something, the CollisionFromRaycast component calls Activate(RaycastHit) method on the OnCollisionActivate component.

ODG_Update4_event
Reference field stores a reference to a game object that has the OnCollisionActivate component attached.
ODG_Update4_OnCollisionActivate
OnCollisionActivate allows you to activate game object based on data received by RaycastHit info. After all elements on the lists are executed, the UnityEvent callback is called.

Here different bullet hit effects are activated. One of them is MetalHitEffect game object.

ODG_Update4_MetalHitEffect
Children of the MetalHitEffect are enabled by default.

MetalHitEffect game object consists of the AudioPlayer component and child game objects with audio clips.

ODG_Update4_AudioPlayer
The more times a clip is assigned to the AudioPlayer, the bigger is the probability it’ll be played.

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.

ODG_Update4_ChangeTargetColor_Hierarchy
CollisionFromRaycast component calls the UpdateRenderer(RaycastHit) method on the RendererUpdate component.

ChangeTargetColor game object has a RendererUpdate component attached.

ODG_Update4_RendererUpdate
RendederUpdate gets target renderer through method argument.

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.

ODG_Update4_Randomizer_IdleSounds
Randomizer will call the PlayRandomClip method 5 sec. after being enabled and the after each 11 to 20 seconds.
ODG_Update4_AudioPlayer_IdleSounds
Zombie has four different idle moans that are played in random order.

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.

ODG_Update4_blood_cells
Approximately once every 1/10 bullet hit a blood cell will get out of the zombie.
ODG_Update4_RandomizerAwake
When RandomizerAwake component gets enabled, it’ll execute callbacks with random probability.

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.

ODG_Update4_ParticleCollision_BloodCell
Particle collision listenes to the MonoBehaviour OnParticleCollision message and add health to the player’s Health component.

Similarly, ParticleCollision component is placed on the player to instantiate the hearts particle system on collision with blood cell.

ODG_Update4_hearts_ps
Instantiate hearts particle system on collision with a 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.

ODG_Update4_StateManager
StateManager holds references to different states and also allows setting the current state.

StateManager holds references to State instances. Here, each state is defined within a separate game object.

ODG_Update4_State
Each state defines callbacks that are executed when the state is activated.

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.

ChangeParent_component
ChangeParent component will change game object’s parent in Awake().
DestroyAfterTime_component
DestroyAfterTime component will destroy referenced game object after specified time.

Leave a comment