Sounds: Another crucial part of our games. Whether they are just effects or music, sounds have the force to carry our games to another level. Before beginning writing this article, I have thought about some games that really touched me with its music/effects.
I never forget Fifa 98 because of its sountrack, especially Blur — Song2. First Grand Theft Auto game has also surprised me (well I was about 7–8 years old at that time, maybe it was easy to surprise me :) ) with soundtracks on the radio — it really felt like I was driving (hey! I know I am not a bird).
The list may continue to infinity, but I want to emphasis on Super Mario Bros lastly. Probably all of us spent hundreds of hours playing Super Mario Bros. I never remember decreasing or muting its sound. You know, a simple music looping at the background, basic sound effects for jumping, collecting coins and so on.
So how do we use sounds in Unity? Well, it’s not much different from the real life (again!). Just like a computer, Unity plays the sounds from an “Audio Source” and gathers it from an “Audio Listener”. In a way, you may think the audio source as a speaker and audio listener as a mic.
Audio listener is provided in the main camera as a component by default.
In order to create a background music, we can create an Audio Manager empty game object, in order to keep our game tidy.
The basic components and their duties are provided below:
AudioClip : The clip that the audio source is going to play
Output : Audio listener is the default output. Unity provides us the opportunity to use Audio Mixer as an output if we want to.
Play on Awake : Unity will start playing the sound as the game object is on the screen.
Loop : Check if you want to loop the clip once it’s finished.
Priority : For ranking the audio sources within each other.
Pitch: Determines the playbackspeed.
For more detailed information, you can check here for Unity documentation. I will be getting into more detail for the 3D sound settings in the upcoming articles.
How do we Use Audio Sources?
As I’ve mentioned earlier, in order to make a game object play a sound, we need to add the “Audio Source” component to the relevant game object.
Let’s begin with adding music to our scene. If you have a clip that will play at the background constantly, you can actually make Unity play the audio without scripting. To play the background music, simply drag and drop the music clip that you want to be played to the audio source of the game component that you have created for music purposes. Make sure that ‘Play On Awake’ and ‘Loop’ is checked so that audio will not stop playing during the gameplay.
Certainly, most of the sounds in the games are played according to the progress/events that occur during the game. We hear the shooting sound once we hit ‘Fire’ button, we hear the crash voice if we crash the car and so on. You can actually guess we cannot play these effects just like a background music. Despite the core mechanics of adding an audio source and clip stands, we need the help of scripting in order to play these effects.
In the Space Shooter game, we want our ship to play shooting effects on every laser it fires. We do not want the effect to be played once the object is alive, so we uncheck the ‘Play on Awake’ property.
So how do we make the clip to be played at the correct moment?
If you look at the scripting API, you can observe that to play an audioclip, we simply use “AudioSource.Play()” command. In order to use a function from a component, we need to reach to the component first.
private AudioSource _audioSource;
AudioSource.Play() method, simply plays the clip attached to the audio source. With the updates made to the player script above, we can easily observe that now our game plays a sound as we shoot a laser.
Playing Different Clips From a Single Audio Source
So far so good. But we want to play an effect once we die, or a different sound with a different laser power-up. How are we going to handle it? Does it really makes sense to add multiple audio source component to the same game object and try to get out of this complexity? Certainly not.
I was actually surprised that there’s such an easy way to do it. Simply, we declare multiple audioclip variables (or an array, I preferred creating multiple in order to distinguish them easier) and change the clip to be played by the audio source within the script.
In the space shooter game, I have used an “ammo out” clip and a “death” clip, apart from the shooting.
To be able to change the clips, we need to access the Audio Source component, which we have already cached for shooting purposes. So, only thing to do here is to change the clip to the one that we want to be played according to the occurred event.
For the death:
_audioSource.clip = _deathSound;_audioSource.Play();Destroy(this.gameObject);
Playing the Clip after Destroying an Object
To be able to use the audio source component within a game object, we need the object to be in the scene (at least, not destroyed completely) so that Unity can reach the object and the necessary component to play the sound; just as the player above.
Now, the enemy case. If we shoot an enemy, we want it to be destroyed and play a sound of destruction. That’s where PlayClipAtPoint() method comes to help.
PlayClipAtPoint is a static method within the AudioSource class. It’s a function that creates a new audio source as it’s called and destroyes the audio source once it finishes playing the clip once. By using this method, we can play an audio clip and make it continue playing until it’s end even if the game object that we have called the method is destroyed.
The method simply requires us to denote the clip we want to be played, and the position that we want it to be played. As we want the position to be the same as the game object that is going to be destroyed, it will be transform.position.
In order to inform Unity regarding the clip we want to be played with the PlayClipAtPoint method:
[SerializeField] private AudioClip _audioclip;
and at the place that we want the clip to be played — which is an explosion sound effect here:
And that’s all. Wish you all the best :).