2D Game Dev Journey — Day 11: Game Audio

Gabe Gomez
3 min readMay 7, 2021

Welcome to 2D Game Dev Journey — Day 11: Game Audio. Today I will be adding in some audio to help with the immersion aspect of our game.

Starting Point & Setup

Today we will be starting right where we left off in 2D Game Dev Journey — Day 10: Post Processing.

Background Music

When it comes to setting up audio, one of the key components we will be working with is the Audio Source component. In this step, we will be making the background music, which will be the easiest audio implementation. All I had to do for this step was create an empty GameObject which I named “background music” and gave it the Audio Source component. I then checked the Loop property to true and attached to it my audio clip for the background music. The final setup is as shown below:

Laser Shot SFX

To get my laser shot sound effects working, I had to get a bit more involved with the code. To start, I gave my “Player” GameObject an audio source component, where that audio source component will play the laser shot sound. In code, I had to grab the audio source attached to our player, and then through that audio source I played the laser shot effect every time my laser was shot with the following line:

With this, I was able to get my laser shot sound effects working as expected.

Explosion SFX

Similar to how I had done things for my laser shot sound effect, the only difference being I had played the explosion sound effect upon the enemy and asteroid being destroyed. My enemy and asteroid GameObjects are set up in the following ways when it comes to the audio components:

Asteroid audio setup
Enemy audio setup

As you can see, I do not have any audio clips attached directly to their audio source components. Instead of that, I am providing my scripts a reference to the audio clip I want to be played when these objects are meant to explode, and I utilize the PlayOneShot() to get the result I want.

Powerup SFX

Lastly, I will be adding some sound effects to my powerups. I accomplished this simply by playing the sound effect through my player when my PowerupCollected() method is called. I, unfortunately, can not play sound for you through GIFs, but with everything set up, including the previous article on post-processing, I get the following final result (just imagine some sound in the background :) ).

Ending for Day 11

Conclusion

Thanks for checking in and reading today’s article. Tomorrow, I will be deploying the game. As always, comments and critiques are welcome. If I have made any mistakes, please let me know so I can correct them for future readers. If you would like a more in-depth tutorial about anything I went over today, let me know and I will take the time to write something up.

--

--