Adding Sound Effects

Let’s sound it out!!

Daniel Jennings
3 min readMay 15, 2021

Background Music

To add background music lets add an AudioSource to our Main Camera game object. Then drag the music_background asset to the AudioClip slot. When adding background music it is ok to attach it to a permanent object like the camera.

Unfortunately, gifs don’t share audio. Next up is the Laser sound.

Laser

In this case, the Player will need access to the laser sound.

We will add an Audio Source to the player but not set the AudioClip.

We populate the AudioSource in the Start method. When we fire our laser we set the sound clip on the AudioSource and the call the Play method.

Doing it this way shows us to reuse the AudioSource for other sounds if the Player ever needs to play a different sound.

Explosion

For the explosion I will create an empty GameObject named AudioManager. In this case the ExplosionSound GameObject will have an AudioSource with the explosion_sound AudioClip.

For the Enemy script we will have a private variable and do a Find to locate the AudioSource.

For the Asteroid we will add an AudioSource and set the explosion_sound as the clip. The sound will play in the Start method of the Explosion. This makes sense since the explosion is an animation, add a sound to the animation at the same time.

PowerUp

We have seen multiple methods of adding Audio to components. Adding sound to the powerups allows us to demonstrate one of the cool features of Unity.

Selecting multiple GameObjects in the hierarchy shows like items in the Inspector. This allows you to add or delete items to all selected items at the same time.

--

--