Graphics are always top-of-mind when you’re creating games, largely because we’re visual creatures. That said, graphics can only take a game so far.

Sound gives depth to the visuals and can trigger powerful emotions. For example, graphics can adeptly depict a large cavernous structure, but echoing footsteps and the sound of dripping water make it an immersive experience.

In this chapter, you’ll take Bobblehead Wars from good to great, through the power of Unity’s audio engine!

Note: This tutorial is part of a collection that teaches Unity development from the ground up. You can read the entire series over here. This series is free and does not require any account creation. All assets are provided. If you find it useful, feel free to buy me a coffee.

Getting started

Open your Bobblehead Wars project that’s in progress, or start fresh with the starter project from the resources for this chapter.

https://drive.google.com/file/d/1inoaQnMiiuSKp69fz2zjxez_Vvv2Wvfn/view?usp=drive_link

The first thing you need to hear sounds in Unity is the Audio Listener. This is a component that you attach to a GameObject so that the engine knows where the listener is. This is important because some audio depends on location — for example, if you’re near a humming generator, the noise will be loud, but if you move far away you might not even be able to hear it.

By default, every new scene includes an audio listener that’s attached to the Main Camera. Click on the camera to see it for yourself:

This screenshot shows the main camera with the Audio Listener component added to it.

Note: This default listener can trigger an error when you import a first-person Character Controller. You’ll receive a message that states:

“There are 2 audio listeners in the scene. Please ensure there is always exactly one listener in the scene.”

Unity’s a bit like the Highlander when it comes to listeners: “There can be only one.” Add the AudioListener to the single GameObject that most accurately represents the listener.

The second thing that you need in order to play sounds in Unity is the Audio Source. As you might guess, this is an object that emits a sound, such as a sound effect or background music.

You already have an Audio Listener set up, so add your first Audio Source — background music in the game. As Corinne Bailey Rae would say, “It’s time to put your records on!”

Playing background music

Select Main Camera in the Hierarchy. Click the Add Component button and select Audio Source in the Audio category.

This screenshot shows the Audio Source compenent

You’re adding the Audio Source for the background music to the camera, since that’s the same place the audio listener is. This way, the volume of the background music will always stay the same; think of it like carrying around your own boom box!

As you can see, you have an array of options. Here’s the breakdown:

  • AudioClip: The actual sound file. You create a clip by dragging a sound file into Unity, and then the engine converts it to an audio clip. In your case, you imported the sounds when you imported the models in Chapter 1, “Hello, Unity.”

Pro Tip: Avoid formats such as MP3 because Unity sometimes re-encodes compressed files to a lower quality when it exports the game. To avoid these, we have provided the audio as OGG files.

  • Output: This represents the Audio Mixer to use. You’ll learn what this means later in this chapter.
  • Mute: Check this if you’d like to mute the audio. This can be useful to programmatically mute sounds mid-game or to disable sounds while testing.
  • Bypass Effects/Listener Effects/Reverb Zones: Turns off any additional effects that you might add to a sound. Effects can add depth and increase realism. For instance, consider the difference between footsteps in that cavernous structure with and without an echo.
  • Play on Awake: Plays the sound as soon as the GameObject is activated. Note that this is checked, which is great: It means that the background music will start playing automatically as soon as the game begins.
  • Sound Sliders: Adjusts the current sound’s properties, such as volume and pan location.
  • 3D Sound Settings: Changes how a sound plays in 3D space. For instance, you can adjust the doppler level here. That can be used to create the pitch and frequency of the sound for a fast-moving object, such as a bullet whizzing past your player.

It’s important to understand that Unity doesn’t factor in level geometry when playing sounds. Here’s a case in which level geometry matters: Imagine that two players, each with a large ray gun, stand on opposite sides of a two-meter thick concrete wall. If player one fires, in Unity player two would hear the shot as if player one were right next to him (rather than it being muffled, as you might expect).

Assigning an audio clip

Look at the audio clip for the background music. In the Project window, open the Sounds folder and select bw_bg. The Inspector shows you all the properties for this audio clip.

This screenshot shows the Audio Clip in the inspector.

In here, you can decide whether or not to go with stereo or mono, load the clip in a background thread or normalize.

You also have the ability to change how the audio clip plays, which can have a ripple effect on performance. By default, audio clips are decompressed in memory when loaded. OGG files expand ten times when decompressed, so a 1 MB file requires 10 MB when it’s loaded. It’s not ideal.

From the Load Type option, select Compressed in Memory. Instead of hogging memory space, the audio will decompress as it plays. This setting does increase CPU usage, but it’s a fair trade-off in this case.

Note that you have the option to stream the file too, but you can try that out another time.

This screenshot shows an Audio Clip set to be Compressed in Memory

Click Apply to save changes.

Next, select the Main Camera and drag bw_bg from the Project browser to the AudioClip property on the Main Camera’s audio source. Check the Loop checkbox to make it repeat continuously.

This screenshot shows adding the background music to the Audio Resource.

Play your game and listen to those sweet, sweet tunes. Don’t worry about volume for the moment. You’ve got a sound manager to build.

Where to go from here

At this point you have some background music, but the game features more than a “banging” soundtrack. Your game needs sound effects and lots of them.

You need to construct a sound manager that will play sound effects on demand. You’ll do this in the next tutorial.


Discover more from Jezner Books

Subscribe to get the latest posts sent to your email.

By Brian Moakley

Brian Moakley is a writer and editor who lives amongst the quiet hills in New England. When not reading tales of high adventure, he is often telling such stories to all who will listen.

Related Post

Leave a Reply

Jezner Books
Privacy Overview

This website uses cookies so that we can provide you with the best user experience possible. Cookie information is stored in your browser and performs functions such as recognising you when you return to our website and helping our team to understand which sections of the website you find most interesting and useful.