Unity Guide
Using sound effects in Unity
A quick guide about how to use sound effects in Unity
Objective: Implement sound effects in a space shooter game in Unity.
In the last post I covered how to start using audio in Unity, and now it’s time to implement sound effects for the space shooter game.
Laser sound effect
In order to implement a laser sound effect every time the player spaceship shoots, we need to add an Audio Source component in the player gameobject. Let’s make sure that the Play on Awake property is disabled so that it only plays the sound whenever it shoots:
Then, let’s open the Player script and create 2 new variables:
- _audioClips
This array will store the references to the different audio files that the player will use in the game. For instance, we’ll only use it to store the laser sound effect.
- _audioSource
This variable will store a reference to the Audio Source component that the player contains to input the respective audio file in the game.
Now let’s drag the laser sound effect file from the project folders into the inspector:
Then, in the Start method, let’s assign the Audio Source component in the new variable:
In order to play different sound effects, let’s create a new function that will receive the index of the sound file in the audio clips array as a parameter:
Then, let’s assign the respective audio file into the clip property of the Audio Source to play the correct sound effect with the Play method:
If you want to know more about this method, you can visit the Unity docs:
Now, in the function that instantiates the laser shoot, we can call the PlayAudio function with the index of the sound effect to play it:
And now, every time the player shoots, the laser sound effect will be played.
Power-up sound effect
Another audio that needs to be implemented is the power-up item collection sound effect. There are 3 different power-ups in the space shooter game:
In order to implement the sound when the player collects each power-up item, let’s open the PowerUp script and let’s create a variable to store the reference to the collection sound effect:
Note: we’ll use a different method to play the sound effect.
Now, let’s select the 3 different prefabs of the power-up items and let’s drag the collection sound effect into the inspector:
And finally, to play the sound effect when the power-up is collected, let’s use the PlayClipAtPoint method from the AudioSource class with the respective audio clip and the position in which we’ll play the sound in the scene.
We’re using this method because when the player collects the power-up item we destroy the gameobject immediately, so the sound won’t play. PlayClipAtPoint provides a way to instantiate an Audio Source component that will play a sound in the game and then, when the sound is over, it gets disposed from the scene.
If you want to know more about the PlayClipAtPoint method, you can visit the Unity docs:
Now, every time that the player collects a power-up in the game, the instance created by the PlayClipAtPoint method will play the collection sound effect.
And that’s it, you can implement sound effects for your game in Unity! :D. I’ll see you in the next post, where I’ll be showing daily features added to my space shooter game in Unity.