Audio listener & audio source
In any game, sounds are the most important components. No one can imagine the game without sounds. In Unity, we can add sound component to the GameObject.
Adding Background Music
Our background music will played throughout the game since it started. So, the background sound was added in the Main Camera. There are two main components required:
Audio Listener
Audio Source
AUDIO LISTENER: An Audio Listener component in the Unity enables us to listen to the music. It is like the microphone or headset. It grabs the audio from Audio Source and plays it through the camera speakers.
- Note: Audio Listener is, by default, added to the Main Camera.
AUDIO SOURCE: An Audio Source component stores the Audio Clip that is desired to be played in the scene. The Audio Clip can be played using Audio Listener. The component also has an option to cache the output through audio mixer, if available. It contains the properties that can provide either 2D or 3D sound effects. The details of Properties of Audio Source can be found at Unity Documentation.
3D Sound Settings of Audio Source Component
In my game, to add the background music, I simply added the Audio Source component to the Main Camera.
Then, the sound I chose for background music was dragged and dropped in the Audio Clip variable.
In my game, to add the background music, I simply added the Audio Source component to the Main Camera.
Then, the sound I chose for background music was dragged and dropped in the Audio Clip variable.
I adjusted the volume from the Audio Source properties, lowering the volume can help player hear other sound effects clearly.
Finally, Loop checkbox was checked off because it is going to be played throughout the game.
ADDING SOUND EFFECTS
Sounds effects are triggered from the script if any events occurred, such as collision, fire, treasures found, and so on. Thus, in order to add sound effects to the Game Objects, we need to declare a variable. To guess it correctly, the variable should obviously be of AudioClip type.
I will just discuss on how I added the sound effect on shooting laser. Other sound effects that I have in the game, such as explosion of asteroids, destruction of spaceship and such will follow the similar procedure.
SOUND EFFECTS: Laser Beam
First, I declared a AudioClip variable in the PlayerController script.
Back in the Unity, in the Player game object, there will be a new variable, laserBeamSound. Here, I dragged and dropped the desired sound into the field.
Now that we have a audio clip stored for laser shooting. We also need an AudioSource to play the audio clip that we have. To do this, we first need to add component "Audio Source" to the player object and declare a variable of AudioSource type in the script attached to it ("PlayerController.cs").
Then, the variable should cache the component "Audio Source" in the script during the Start() of the game.
Once we have the audio source component cached, we can easily used the properties that AudioSource have in the desired event inside our script.
Once we have the audio source component cached, we can easily used the properties that AudioSource have in the desired event inside our script. In this game, I want the laser sound to play once when player presses Space and laser beams are instantiated. So inside the decision statement of instantiating laser beam, I added the PlayOneShot method. Inside the method there are two parameter. The first one is the sound variable that desired audio clip is attached to and the second one is the level of volume.
Similarly, other sound effects were added in the game.