Audio Basics

Big Ideas:

  • Play a single sound effect with "AudioSource.PlayClipAtPoint(clip, location, volume)"
  • Play ongoing background sound using an "AudioSource" component on a GameObject
  • Tons of available free sounds online:
    • freesound.org - A huge library of free sounds you can use. You must make an account, but there are tons of awesome sound effects here. Definitely check it out.
    • Audacity - A free sound editing app to record and edit your own sound effects.
    • "Royalty free music" - Just google this term to find tons of free music you can use in your games.

Playing a Single Sound Effect


1. Find a sound effect. I downloaded a WAV file from freesound.org that went by the title "Laser Shot Silenced".

Unity supports these four formats: AIF, WAV, MP3, OGG


2. Drag the file into the Project Window in your Unity Editor. Similarly you could go to the Top Menu and select "Assets --> Import New Assett ..." and locate the file on your computer.


3. Write a script that has:

  • public AudioClip variable - in Unity, drag the audio clip file from the project window onto this public variable
  • Use the "AudioSource.PlayClipAtPoint(clip, location, volume)" command


Important Notes & Common Mistakes

1. The PlayClipAtPoint() function actually creates a GameObject to play the sound and destroys it when it is done playing. This is not efficient. Better is to have an AudioSource on your GameObject that selects which sounds to play and never Creates/Destroys any GameObjects.

2. Destroy - If you destroy a game object before this sound effect command, it will never get called. When you destroy the GameObject it doesn't run any commands after the Destroy() command.

Playing Background Music


1. As above, find an audio clip and bring it into Unity. I find it really easy to search for "royalty free music" and find some great background music. There are tons of sites hosting these, musicians creating these, and all sorts of genres of music.


2. On the Main Camera in your scene, add an "AudioSource" component. This is like a speaker that outputs sound.

Having it on the camera is best because it is in the same location as the one single "AudioListener" in your scene (like a virtual microphone recording sound input). This means you won't have your background music on the left or right side of the microphone, messing up the stereo or surround sound. The background music also won't ever be distant from the microphone and grow louder/quieter (unless you use scripting to tell it to grow louder/quieter).


3. Drag the audio clip into the slot "AudioClip" at the top. Then check both of circled boxes. "Play on Awake" just makes sure the background music starts when the scene opens, but you could unselect this and use the ___.Play() command to start it at a later time. The loop checkbox is simply that, loop the music so it doesn't play once and go silent.

Better Way to Play Sound Effects (No Create/Destroy)


As mentioned above, using "AudioSource.PlayClipAtPoint()" creates a temporary GameObject to play your sound and then destroys the GameObject when the sound is finished playing. This gets really expensive when there are lots of GameObjects in your scene creating lots of sound effects. There is a much more efficient way.

Better:

1. Create one Audio Source on each GameObject that will play a sound

2. Publicly link to multiple AudioClips

3. Use "PlayOneShot(clip, volume)"


Play() vs PlayOneShot()

  • Play() does not take an argument. It plays the AudioClip currently loaded in the AudioSource. It also interrupts the clip currently playing, and starts from the beginning of the new clip. If you have the same AudioClip still loaded, it will restart it from the beginning.
  • PlatOneShot() takes in an AudioClip as an argument and does not interrupt sounds currently playing. This is great if you want to use a variety of quick sounds like jumping, turning, running, colliding and you don't want the clips to interrupt one another.

Some Great Resources:

Free Assets

  • freesound.org - A huge library of free sounds you can use. You must make an account, but there are tons of awesome sound effects here. Definitely check it out.
  • Audacity - A free sound editing app to record and edit your own sound effects.
  • "Royalty free music" - Just google this term to find tons of free music you can use in your games.


Unity Official Materials:

YouTube Videos

"How to make Sound Effects for Games - Easy Tutorial" (9min, Mar 2019) by BlackThornProd. This video demos how to record your own sound effects and use some simple filters on the free Audacity program. Really quick and gets you started.

"How to make Music for your Game - BOSCA CEOIL" (11min, July 2017) by Brackeys. This video shows you how to make some super simple background music based on notes and chords in a super simple program "BOSCA CEOIL". You don't NEED musical background for this, but it would very much help.

"How to play background music in Unity" (3min, Feb 2017) by Tony Morelli. Literally just a super quick walkthrough of the background music instructions above, in case you like following along visually in a video.


Website Tutorials

"Unity Audio Tutorial: Getting Started" by Anthony Uccello (Sep 2019). This is a great tutorial that goes over some of the material above, but then more specifically goes through the features of an "Audio Source" component. He has you play around in a single simple scene. Great explanations.