Framework 1: Level Refinement
Adding Sound Effects & Background Music
One of the next few things that we need to add to our game is sound effects. Sound effects are the small details that enhances the game play and provides a level of depth to the game itself. Sounds such as collecting the coins/collectables or climbing up the ladder or slamming on the ledge all add a layer of feedback to the player.
Prepping for Sound effects:
Everyone has their sources when gathering sound effects, I won’t tell you where to get them, but there are online sources *ahem*.
Anyhow, once you have your sources, import them into Unity and place them within its own dictated folder. This will help with organization later. For the sound effects, there will be two method that can trigger the sound effect to play. First is using the Audio Source and allow the audio source to play the sound effect. This is great for background music or any type of audio clip that can be looped. The second method is using Playing Audio Clip on point, which works wonderfully if the audio clip belongs to an collectable object since you can still play the Audio effect even after when the object has been destroyed.
To add the background music simply create a empty game object and attach an Audio Source element. Drag and drop the audio clip into the slot and check “Loop” and “Play on Awake”. There are some audio settings which you can adjust such as volume and pitch. This music will play automatically once the scene has loaded so no scripting will be required.
For the collectable sound effects, we want to trigger the sound clip as the player makes contact with the object. Therefore we are limited to use the Play Clip on point function, the beauty of this method is we won’t need an Audio source.
In the Collectable Behaviors script create a new local Audio Clip variable with a serialized field tag. We can also add in an private float value which will be our volume.
In the section where we have our Trigger Enter event, we are going to add in the Audio Play Clip at point function. Since the play clip at point function as Spatial Audio ability, the further away the object is, the quieter it will be. Therefore we are going to play the audio clip right at the Camera position by adding the “Camera.main.transform.position” as our set location.
For the ladder climbing we want to loop that sound since the player duration on the ladder could be longer than the length of the audio clip. Therefore we are going to be using an Audio Source to house our audio clip.
On the script we are going to create an serialized field to house our private Audio Source variable.
Then we are going to play the sound during the On trigger enter event.
But we need to stop the looping once we exit the ladder, so during the On Trigger Exit method, we are going to Pause the sound effect.
Time to add in the Ledge slam when the character lands on the platform ledges. For this, since we don’t need an looping aspect, and since the sound effect only trigger once, we can use the Play at Point function. Create an serialized field to house the audio clip.
Then at the Trigger Enter we are going to instantiate the sound effect at the main camera when the player makes contact.