Reader small image

You're reading from  Unity Cookbook - Fifth Edition

Product typeBook
Published inNov 2023
Reading LevelIntermediate
PublisherPackt
ISBN-139781805123026
Edition5th Edition
Languages
Tools
Right arrow
Authors (3):
Shaun Ferns
Shaun Ferns
author image
Shaun Ferns

Shaun is a lecturer at Technological University Dublin. He is currently teaching on the BA (Hons) in Creative Digital Media where he is lead in the delivery of the Multimedia Stream. He is currently exploring serious games for construction-related training as well as the opportunities transmedia provides in improving user experience and engagement in cultural archive artifacts. His educational research is currently driven by his interest in self-determined learning (heutagogy), rhizomatic learning theory, micro-credentialing /digital badging, and curriculum development.
Read more about Shaun Ferns

Sinéad Murphy
Sinéad Murphy
author image
Sinéad Murphy

Sinead Murphy is currently Data Analytics Manager for the Irish NGO Trocaire. She has over 25 years of computing experience, including freelance IT training and database consulting, university lecturing in mathematics, IT skills and programming at TU Dublin (Ireland) and Middlesex University (London). She is a published academic, with undergraduate and postgraduate degrees in mathematics, computing and data science. She is passionate about the use of IT for understanding and visualising data, and using that understanding to make meaningful differences in the world. She is currently exploring the use of Python and Unity for data analytics and interactive visualisations.
Read more about Sinéad Murphy

View More author details
Right arrow

Playing and Manipulating Sounds

Sound is a very important part of the gaming experience. In fact, we can’t stress enough how crucial sound is to a player’s immersion in a virtual environment. Just think of the engine running in your favorite racing game, the distant urban buzz in a simulator game, or the creeping noises in horror games. Think of how these sounds transport you into the game.

Before we get into the recipes, first, let’s review how different sound features work in Unity. A project with audio needs one or more audio files. These are called AudioClips in Unity, and they sit in your Project folders. At the time of writing, Unity supports four audio file formats: .wav, .ogg, .mp3, and .aif. Files of these types are re-encoded when Unity builds for a target platform (PC, Mac, Linux Standalone, Android, or WebGL, for example). It also supports tracker modules in four formats: .xm, .mod, .it, and .s3m.

A scene (or prefab) GameObject can have an...

Setting up the Third Person Character Controller project

Unity provides a great Playground scene with a Third Person robot character set up and ready to use. We’ll get those assets and set up a project with this scene in this recipe, making it really easy to work with 3D sounds later this chapter.

How to do it...

To set up the Third Person Character Controller project, perform the following steps:

  1. Create a new Unity 3D project.
  2. Open a web browser tab to Unity Asset Store, by choosing Menu: Window | Asset Store.
  3. Search for the free Starter Assets Third Person Character Controller.
  4. Select the assets, and when viewing their details, click the button Add to My Assets.

Figure 4.1: Adding Third-Person Starter to your assets on the Unity Asset Store website

  1. In the Unity editor, open the Package Manager to list your asset store assets by choosing the menu Window | Package Manager. Dock the Package Manager panel alongside...

Playing sound when a scene begins

Often, we want a sound to play as soon as a scene is loaded; this may be to indicate to a player that the new scene is ready, or perhaps it’s some background music or audio sound effect for the scene. As you’ll see in this recipe, Unity makes it very easy for us to play as soon as a scene is loaded.

Before we start using the 3D scene created in the first recipe of this chapter, we’ll first explore some core audio features in a 2D project in this recipe.

Getting ready

Try out this recipe using any audio clips you might have on your computer. We have included a number of classic Pacman game sound clips inside the 04_02 folder.

How to do it...

To play a sound when the scene is loaded, perform the following steps:

  1. Create a new Unity 2D project. There is a single sample scene, containing a Main Camera GameObject that has an Audio Listener component.
  2. Import the sound clip files.
  3. Select the...

Removing redundant AudioListener components

There are times when we may end up with more than one AudioListener in a scene, so need to remove or deactivate one to avoid problems. We’ll learn how to do so in this recipe.

Getting ready

You can find several Pacman game sound clips inside the 04_02 folder.

How to do it...

To remove AudioListener components, perform the following steps:

  1. Create a new Unity 2D project. There is a single sample scene, containing a Main Camera GameObject that has an Audio Listener component.
  2. Import the sound clip files.
  3. Select the Main Camera GameObject in the Hierarchy, and then from the Project panel, drag the Pacman Opening Song audio clip into the Inspector.
  4. Save and play the scene. You should hear the Pacman song play once as soon as the scene begins.
  5. Now, add a second camera to the scene, choosing GameObject | Camera. Rename this GameObject Camera2.
  6. Save and play the scene. You should hear...

Enabling and customizing 3D sound effects

The default spatial setting for an AudioSource component is to be fully 2D. However, the Spatial Blend between 2D and 3D interpretation of a sound playing from a GameObject’s AudioSource is easily changed. Once the 3D location of an AudioSource is brought into the equation, many interesting audio effects are possible to enhance the player’s experience. In this recipe, we’ll move the spatial blend of an AudioSource fully to 3D, and customize the Volume Rolloff, which determines how a sound becomes quieter the further away it is located from the active AudioListener in a scene.

There are scenes where the best location of an AudioListener is not on a camera GameObject. This is often the case for third-person games, where the camera is behind/above the player’s character, and we want the player to hear the sounds that their animated character should be hearing (not where the observing camera is located). In this...

Adding effects with Audio Reverb Zones

One way to make the sound in a game more realistic is to apply effects about how sound behaves in different physical environments. The way sounds echo changes when they bounce off surfaces made of different materials, such as in bathrooms, sewers, hallways, or outdoors in a city or quarry, and so on. Audio Reverb Zones are a Unity component that can add such effects to AudioSources in spherical zones in a scene, which we’ll learn to create and customize in this recipe.

Getting ready

This recipe follows from the previous one, so make a copy of that project and work on the copy.

How to do it...

To add effects with Audio Reverb Zones, perform the following steps:

  1. Open the copy you made of the previous recipe.
  2. Open the Playground scene customized in the previous recipe.
  3. In the Hierarchy panel, double-click the Sphere-music GameObject – this should select the object and align the Scene panel to...

Playing different one-off sound effects with a single AudioSource component

As we’ve seen in the recipes at the beginning of this chapter, the basics of playing a sound are very straightforward in Unity – by adding an AudioSource component to a GameObject and linking it to an AudioClip sound file. For simple sound effects, such as short, one-off plays of pickup confirmation noises, it’s useful to have a single AudioSource component that you can then reuse to play different sound effects. That is what we’ll do in this recipe.

Getting ready

Try out this recipe using any short audio clip that is less than one second in duration. You can also find several Pacman game sound clips inside the 04_02 folder.

How to do it...

To play multiple sounds using the same AudioSource component, perform the following steps:

  1. Create a new Unity 2D project and import the sound clip files.
  2. Create a C# script class, called SoundPlayer, in a new...

Playing and controlling different sounds, each with its own AudioSource component

The approach in the previous recipe (using PlayOneShot(...) with a single AudioSource component) is fine for one-off sound effects. When further control is required over a playing sound, each sound will need to be played in its own AudioSource component. In this recipe, we’ll create two separate AudioSource components and pause/resume each of them with different arrow keys.

Getting ready

Try out this recipe with two audio clips that are several seconds long. We have included two free music clips inside the 04_02 folder.

How to do it...

To play different sounds with their own AudioSouce components, perform the following steps:

  1. Create a new Unity 2D project and import the sound clip files.
  2. Create a new empty GameObject in the scene named Music medieval, containing an AudioSource component that is linked to the 186772__dafawe__medieval AudioClip. You can do this in...

Creating just-in-time AudioSource components at runtime through C# scripting

In the previous recipe, for each sound clip that we wanted to manage in the scene, we had to manually create GameObjects with AudioSource components at design time. However, using C# scripting, we can create our own GameObjects that contain AudioSources at runtime – just when they are needed.

This method is similar to the built-in AudioSource PlayClipAtPoint() method, but the created AudioSource component is completely under our programmatic control.

This code was inspired by some of the code posted, in 2011, in the online Unity Answers forum, by user Bunny83. Unity has a great online community, with users helping each other and posting interesting ways of adding features to games. You can find out more about that post at http://answers.unity3d.com/questions/123772/playoneshot-returns-false-for-isplaying.html.

Getting ready

This recipe adapts the previous one. So, make a...

A button to play a sound with no scripting

Unity can send messages to GameObjects, including built-in components such as AudioSource and components that are instances of C# script classes we write ourselves. In this recipe, we’ll create a scene with a button that sends a Play() message to the AudioSource component of a GameObject – to illustrate how some messages can be sent with no custom scripting at all. Then, in the following recipe, we’ll write a custom C# script class that will refine the scene’s behavior, preventing the AudioSource from being restarted if it is already playing.

Getting ready

Try out this recipe with any audio clip that is one second or longer in duration. We have included the engineSound audio clip inside the 04_09_engine_sound/Sounds/ folder.

How to do it...

To create a button to play a sound with no scripting, perform the following steps:

  1. Create a new Unity 2D project and import the sound clip file.
  2. ...

Preventing an audio clip from restarting if it is already playing

In a game, there might be several different events that cause a particular sound effect to start playing. If the sound is already playing, then in almost all cases, we won’t want to restart the sound. A limitation of the previous recipe was that if we repeatedly clicked the button, the AudioClip would keep being interrupted and restart from the beginning with each button click.

This recipe involves a custom C# script class that includes a test so that an AudioSource component is only sent a Play() message if it is currently not playing.

Getting ready

This recipe adapts the previous one. So, make a copy of the project from the previous recipe, and work from this new copy.

How to do it...

To prevent an audio clip from restarting, perform the following steps:

  1. Open the project, which is a copy of the previous recipe.
  2. Create a second UI button named Button-play-not-interrupt by...

Waiting for the audio to finish playing before auto-destructing an object

An event might occur (such as an object pickup or the killing of an enemy) that we wish to notify the player of by playing an audio clip and an associated visual object (such as an explosion particle system or a temporary object in the location of the event). However, as soon as the clip has finished playing, we will want the visual object to be removed from the scene. This recipe provides a simple way in which to link the end of an audio clip playing with the automatic destruction of its containing object.

Getting ready

Try out this recipe with any audio clip that is one second or more in duration. We have included the engineSound audio clip inside the 04_09_engine_sound/Sounds/ folder.

How to do it...

To wait for audio to finish playing before destroying its parent GameObject, perform the following steps:

  1. Create a new Unity 2D project, and import the sound clip file.
  2. Create...

Creating audio visualization from sample spectral data

The Unity audio systems allow us to access music data via the AudioSource.GetSpectrumData(...) method. This gives us the opportunity to use that data to present a runtime visualization of the overall sound being heard (from the AudioListener) or the individual sound being played by individual AudioSource components.

The following screenshot shows lines drawn using a sample script provided by Unity at https://docs.unity3d.com/ScriptReference/AudioSource.GetSpectrumData.html:

Figure 4.17: Debug DrawLines audio visualization in the Scene panel

Note that, in the preceding sample code, the use of Debug.DrawLine() only appears in the Scene panel when running the game in the Unity editor (not for final builds). Therefore, it cannot be seen by the game player. In this recipe, we’ll take that same spectral data and use it to create a runtime audio spectral visualization in the Game panel. We’ll do this by...

Synchronizing simultaneous and sequential music to create a simple 140 bpm music-loop manager

There are times when we need to precisely schedule audio start times to ensure a smooth transition from one music track to another, or to ensure simultaneous music tracks play in time together.

In this recipe, we’ll create a simple 4-track 140 bpm (beats-per-minute) music manager that starts playing a new sound after a fixed time – the result of which is that the tracks fit together perfectly, and those that do overlap do so in synchronicity.

Getting ready

For this recipe, we have provided several free 140 bpm music clips inside the 04_12 folder.

How to do it...

To create a music-loop manager, perform the following steps:

  1. Create a new Unity 3D project, and import the provided sound clip files.
  2. Create four GameObjects in the scene that contain an AudioSource component, linked to a different AudioClip loop from the 140 bpm files provided. You...

Recording sound clips with the free Audacity application

When I quickly want a “placeholder” sound clip, and don’t have a file on my computer, or can’t find one quickly on the web, then often I’ll just record a sound file using the free, open-source, multi-platform Audacity application. I would also use this application to record the final version of dialogue, such as a narrative voice-over for introduction scenes, or spoken words when a player interacts with a non-player character in a game.

This recipe shows you how to record and save audio clips easily with this application.

Figure 4.21: The free, open-source Audacity audio editor application

Getting ready

Download and install the Audacity application for your computer system (Windows/Mac/Linux). You can find it at https://www.audacityteam.org/download/.

How to do it...

To record sound clips with the free Audacity application, perform the following steps:

    ...

Further reading

The recipes in this chapter demonstrate both scripted and Unity audio system approaches to help you manage audio and introduce dynamic effects at runtime. Games can become far more engaging when the audio environment of effects and music subtly changes, based on the context of what is happening in the game. You may wish to explore further with the Unity Audio Mixer and Audio Reverb Zones, for example. Please refer to the following:

What is possible with special audio is now becoming even more interesting with the introduction of ambisonic audio when playing 3D VR games. This allows listeners to enjoy rich audio experiences based on whether sounds are above or below the listener, as well as their distance from an audio source. To find out more about ambisonic audio, please view the following references:

    ...
lock icon
The rest of the chapter is locked
You have been reading a chapter from
Unity Cookbook - Fifth Edition
Published in: Nov 2023Publisher: PacktISBN-13: 9781805123026
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €14.99/month. Cancel anytime

Authors (3)

author image
Shaun Ferns

Shaun is a lecturer at Technological University Dublin. He is currently teaching on the BA (Hons) in Creative Digital Media where he is lead in the delivery of the Multimedia Stream. He is currently exploring serious games for construction-related training as well as the opportunities transmedia provides in improving user experience and engagement in cultural archive artifacts. His educational research is currently driven by his interest in self-determined learning (heutagogy), rhizomatic learning theory, micro-credentialing /digital badging, and curriculum development.
Read more about Shaun Ferns

author image
Sinéad Murphy

Sinead Murphy is currently Data Analytics Manager for the Irish NGO Trocaire. She has over 25 years of computing experience, including freelance IT training and database consulting, university lecturing in mathematics, IT skills and programming at TU Dublin (Ireland) and Middlesex University (London). She is a published academic, with undergraduate and postgraduate degrees in mathematics, computing and data science. She is passionate about the use of IT for understanding and visualising data, and using that understanding to make meaningful differences in the world. She is currently exploring the use of Python and Unity for data analytics and interactive visualisations.
Read more about Sinéad Murphy