In Unity 5
Good evening gents and ladies. This tutorial, same as the last, will be in my space adventure project, In Search of Simon. Today’s topic is audio and how to make it work in a game.
Unity handles a range of audio assets, though I suggest keeping it to either wav, mp3 or ogg for the purposes of keeping it simple. I believe Unity can handle others, but those are my favorite formats.
Once imported into assets, clicking on an audio file brings up its inspector, which is basically just standards for compression. In most cases, we won’t need to edit anything in the audio inspector.
Instead, to make audio work, we need to create an audio source component on a random game object. In the case of this game, I made a game object called BGM and made it a child of the main camera. There is a reason for this, which I will get to closer to the end. For now, just know that it’s usually a good idea for background music to be close to the camera.
A couple of notes for the audio source component…
- Audio clip is the attached sound file. Drag your audio file here to link it up.
- Play on awake determines whether or not the sound will play when the object is created. This useful for bullets or objects you want to cry or scream when they are created. Also, explosions follow this mode.
- Loop is another important option. Checking this box will cause the audio source to start over and continue playing after completion until the object is deleted. Very useful for background music or ambient sounds.
- Volume will adjust the strength of the audio source, but ordinarily, you shouldn’t need to modify this one. However, due to the nature of third party audio files, you may need to tweak this for certain sound effects.
- Pitch will not only alter the pitch of the file, but I believe it will speed it up or slow it down for higher and lower pitch respectively.
You will likely not need to modify any of the other settings.
Item of note…
Most sounds function in 3D space, which means they will be louder or softer depending on how close they are to the scene’s audio listener. What is the audio listener? By default, it’s on the main camera. Therefore, if you want to have background music, a good idea is to make a BGM audio source a child object of the main camera. That way, no matter how far the camera moves, it will always be the same distance from it.
That’s all I have today. Join me next time for an awesome tutorial!