Mobile Game Progression
Adding Sound effects to our game (part 2)
Continuing on from the previous article where we added background music to our game along with adding UI sound effects to our buttons. In this article we will focus on adding Sound effects to our player and enemies. There are several things that we will go over:
- Adding sound effects when our Player jump/walk/attack/gets hurt/dies
- Adding sound effects for our enemies gets hurts/dies
- Adding sound effects when the player collects the Diamonds.
Adding the Sound Effects:
What we need to first within our Audio Manager we are going to create new empty game objects containing new Audio source components for each sound effect. This method is organized but isn’t optimized.
For every sound effect other than the background music, we want the “Play on Awake” to be unchecked.
Inside the player script we are going to create new handles for all the Audio source components.
Death and Damage Sound:
Starting with the damage and death sound effects, we are going to locate the “Damage” method and insert the damage Sound whenever the player gets his health deducted.
In the Death sequence within the Damage method, we are going to insert the Death sound effect.
Sword Swing Sound:
In the void Attack method within our player script we are going to insert the sword sound effect.
Diamond Collection Sound:
In the Add Gems method we are also going to add in our Diamond sound effect.
Foot Steps Sound Effect:
For the foot step sound effect, I am going to introduce another method to trigger audio clips. We are going to store several audio clips within an array and have the audio clip play a random clip.
First we are going to get a reference to an Audio Source that doesn’t contain any audio clip yet. Then we are going to create an Audio Clip Array and name it footstep.
In the start or awake method we are going to assign the audio source from the audio source component on the player.
Then during the movement method we are going to select the audio clip from the Array with an randomizer and assign it to the audio source. Then play the audio clip.
I also created a boolean to track whenever the player is moving, since I only want the footstep sound to occur whenever the player moves. I track whenever my velocity.x doesn’t equal to zero I set the isMoving bool to true, else it should be false.
Back in Unity we are going to make sure our footstep array has correct amount of elements. In my case, I only have 2 audio clips so I set the count to 2, then drag in the two audio clips into the slots.
Now when testing your game, you should hear the proper sound effects.