Sound Effects/Music #Unity

Thomas Mauro
3 min readMay 28, 2022

--

The element of sound in a game is what could be called an immersive element. I can play a game without sound all day and just use my imagination, but very much prefer and 99.9% of the time play with the music and sound effects on. Sound is waves, they move through space & time and into your ears. You don’t just hear it you also feel it. It can have the effect of getting you more into the game. Therefore it's time to add sound to Galaxy Shooter. I won't bore you or take too much of your time. We will add some menu, background level music, and a laser sound effect.

Alright, thus far I have already implemented the in-game music so I will do that same process, and add background music to the Main Menu. The Main Camera already has the Audio Listener.

  • Create an Empty Object in the Hierarchy, name it Audio_Manager
  • Create an Empty Object on the Audio_Manager name it Background
  • Add Component of type Audio Source
  • Add in the correct audio clip (I opted to slow the game music down (Lower pitch .5) and use it as the main menu clip. In the Audio Source component, you can adjust settings and it won't mess with the clip attached to any other object)
  • Check the Loop Box and Play on Awake if not already checked
Create an Empty Object in the Hierarchy, name it Audio_Manager
Create an Empty Object on the Audio_Manager name it Background
Add Component of type Audio Source, set setting

Now on the Main Menu Scene, the music will simply play. Next, I will do the laser sound effect. That one will require some code to tell the laser sound when it should play the effect. (At the time of firing the laser/pressing the space key down)

  • Add Audio Source Component to the Player
  • // Make a Variable for the [Serialize] laserEffectClip; (The variable will let the script know there is an audio clip to access)
  • //Variable for audioSource;
  • Add Laser Effect Clip into the slot on the Player script component in Unity
  • // get the Component _audioSource; via void Start in the Player script (Null Check)
  • // else assign the laserEffectClip_audioSource
  • // Play the Laser sound effect after the FireLaser method/when the space key is pressed (Light is faster than sound)

A vital and very handy resource for this will be AudioSource.Play script reference.

Add Audio Source Component to the Player
Needed Variables on the Player script
get the Component _audioSource; via void Start in Player script (_audioSource = GetComponent<AudioSource>();)
// get the Component _audioSource; via void Start in the Player script (Null Check) // else assign the laserEffectClip_audioSource
// Play the Laser sound effect after the FireLaser method/when the space key is pressed (Light is faster than sound)
Laser effect adds

Every time I fire the laser the sound effect will now play. I will play with the pitch of the sound effect and maybe replace it eventually. From here I will add in the explosion effects where they need to be then move on from there to add more. Have a good weekend.

The Sound in action!

--

--