Create Immersion by adding Sound in Unity
From the iconic sounds of light sabers to the infamous shark in Jaws, sounds can drastically change the mood of a scene. It should go without saying that adding sound can be vital to the overall feel of your game.
Unity provides you with some simple means to do this whether it’s for some background music or the sound of your blaster.
The first step is to set up a game object to control the music. Then you can add the “AudioSource” component:
Once this is in place, you can simply drag any audio file that you have into the “Audio Clip” component:
From here, you have access to custom adjustments you may want like volume, looping and playing on Awake.
Creating sound when a gun fires can be done through script. We need access to the clip we want to play and then we can decide when it should start:
Finally, we drag the sound we want into the audio clip slot:
One thing to note is that if you are destroying a game object and want a sound to play before that same object is destroyed, you can use this method:
This plays the sound at a specified location independent of the game object being destroyed