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

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

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

Similarly, other sound effects were added in the game.