This is a part of game development that I have very little experience or knowledge about. I did have music tracks in my Roller Ball version but I just got them to play in the background on a 2 – 3 minute loop, and I did that just by dragging the music onto each scene. This worked okay and sufficed for my first game, but there was no real control over the music and I didn’t have sound FX. So for this part of the Space Shooter, I thought it wise to document.
As per usual I amusing Unity Engine 5.5.1f1 and Visual Studio to make this game and the original tutorial is available here: Space Shooter Tutorial. I imported all the assets from the Asset Store but if I have time I will endeavor to inject my personal design flair into it as well.
So lets begin another aspect in the wonderful world of game design……
There are three main components in Unity that handle the sound, the music and SFX:
Θ Audio Clips: contain the audio data used by Audio Sources and include .aif, .wav, .mp3, and .ogg. Other type formats can also be imported but are for more advanced users.
Θ Audio Sources: playback any Audio Clips and can be configured to play these as 2D, 3D, or as a mixture (SpatialBlend). The amount of speakers used to play can also be configured.
Θ Audio Listener: acts as a microphone It receives input from any given Audio Source in the scene and plays sounds through the amount of speakers chosen.
In Unity, in the Assets folder in the Project tabs are the audio clips for the game.
If you select an audio clip the following view is in the inspector.
The relevant audio clips must be allocated and placed in the right objects and assets to be played at the right time. We can do this by:
Dragging an audio clip onto a game object. Unity will automatically reference the audio clip dragged over. To attach an audio clip this way, you have to drag the audio clip onto the prefab you want to play this clip from. It is easier to do if you changed the view in the Project view to one column. You can set the explosions to the explosion prefabs but to add the player firing audio clip you have to drag ‘weapon_player’ onto the Player object in the scene. In the inspector
view you will see that the audio clip has been attached to the player object.
When we dragged the explosion audio clips onto the prefabs we left the settings for the audio clip in the inspector the same and did not change anything as the clips needed to be played upon ‘awake’. With the player weapon, we have to now deactivate the play on wake, as we want the audio clip to play only when we fire the lasers. Play on awake will mean that when the prefab is destroyed the explosion sound will play automatically.
To get the players weapon sound to activate when we fire the laser, we have to add code to our Player Controller script. The logical place where we place the code is in the player fire section of the Player Controller script, lines 28 to 36.
Once script is saved and scene in Unity is saved, run game and test and wallah we have sound. We have the ship firing sound, asteroid explosion sound and player explosion sound, excellent!
Now for the music.
To add the music, drag the music_background audio clip onto the Game Controller in the hierarchy. In the Inspector view make sure Play on Awake is checked and also check Loop, so that the audio clip plays continuously in the background.
So now once I save the scene and press play, the music plays in the background on a constant loop. The music and SFX still needs to be adjusted though, as all the clips are playing at full volume and don’t always mix well. Select the desired object that has the audio clip that you want to adjust. Once selected, in the Inspector view, in the Audio Source component, adjust the volume to the level you desire. For this game I adjusted the players laser fire volume to 0.5.Do the same with any audio clips you would like to adjust. Once done, save scene and run to test.
Wahoo!! We have sound, we have SFX, all running and playing when supposed to.
So there you have it. Sound and SFX added. There is still more to go before I complete this game but that is all for now for this post. Thank you so much for your company, I hope you got something out of it, until next time,
God’s blessings upon you in abundance.