2D Galaxy Shooter — EXPLOSIVE SOUND EFFECTS!

Marcus Cohee
3 min readAug 31, 2021

Hello all!

Today, I’m going to go over how to add sound effects to the game! Effects like background music, firing a laser, explosions on death, and getting powerups!

I tried to embed the video but that was not working at all, I made a link to it below that displays the sound I added to the game.

Adding the Sound Source for the Background Music

  1. Add an empty game object, name it Sound_Manager, and drag it somewhere off screen, it has an annoying icon like Post Processing.
  2. Add another empty, name it Background, then make it a child of the Sound_Manager.
  3. Add a component to the Background called Audio Source.
  4. Find a .WAV Audio file you want for your background music, then drag it onto the box by Audio Clip.
  5. Hit the loop button, you always want music to play!
  6. Test it.

Adding the Laser Sound

This will mostly happen in the Player script.

  1. Select the Player
  2. Add the Audio Source component, then uncheck the Play On Wake box.
    This will stop the sound from playing when the game starts.
  3. Head to the Player script.
  4. Add variables:
    [SerializeField] private AudioSource _audioSource;
    [SerializeField] private AudioClip _laserSound;
  5. Reference these in the Player script component. You will need to find a laser sound, and link it to the _laserSound, and link the Audio Source component to _audioSource.
  6. Head down to Start()

Get the Audio Source component and NULL check it.

7. Now head down to the FireLaser() method.

When you use “_audioSource = _laserSound”, it will set the Audio Clip in the Audio Source component to your laser sound. Then since _audioSource is a laser sound, it will play once.

8. Test it!

Adding the Explosion Sound

Similar to the laser, we just need a new variable. When done here, do the same to the Asteroid and Enemy scripts for when they die.

  1. Back to the Player script.
  2. Add variable: [SerializeField] private AudioClip _explosionSound;
  3. Reference an Explosion sound into the Player script component.
  4. Under the Damage() method.

Make the Audio Clip into the explosion clip, then play it before you die!

5. Test it! Listen to them sweet explosions!

Adding the Powerup Sound

  1. In the Player script.
  2. Add variable: [SerializeField] private AudioClip _powerupSound;
  3. Reference a Powerup sound to the Player script component.
  4. You will add the “_audioSource = _powerupSound” and “_audioSource.Play()” under three places.

This is saying that whenever you collect any of the three Powerups, then play the powerup sound!

Check out the video at the top to see all the awesome things you can do with sound!

This has been how to add sounds in Unity! Next, I will go over how to build the game for the end user! This means that I can make the game playable to anyone that can open the file!

Thanks you for your time!

--

--

Marcus Cohee

I am starting Unity and learning the ropes as I go! Let’s take this journey together!