This website uses cookies to enhance the user experience

Audio in Unity

Share:

In Unity, audio plays a vital role in creating an immersive experience. Ranging from background music to sound effects, Unity provides a suite of powerful tools and features for audio manipulation. In this chapter, we'll explain the fundamentals of implementing and manipulating audio in Unity using specific examples to illustrate key concepts.

Let's start off with the basics of audio implementation.

Audio Clips and Audio Sources

The two primary components to manage audio in Unity are Audio Clips and Audio Sources.

  1. Audio Clip: This is the actual sound file that you will be using. Unity supports files in the format of .wav, .mp3, and .ogg. You can import these audio clips into Unity like you would import any other asset.

  2. Audio Source: This is the component that hosts the audio clip and provides properties to further refine the sound effects. It control how and when the audio clip is played, the volume, pitch, spatial blend, and several other properties.

In order to play an audio clip in Unity, we need to attach the clip to an audio source. Then, the source can be added to a game object. Now, when the game is run, the audio clip will be played through the audio source.

Let's delve into some code for adding an AudioSource to our game character, Gizmo, and assigning an Audio Clip to it.

using UnityEngine;
[RequireComponent(typeof(AudioSource))]

public class GizmoSounds : MonoBehaviour
{
    public AudioClip gizmoLaughClip; //audio file
    private AudioSource audioSource;  //audio source

    void Start()
    {
        audioSource = GetComponent<AudioSource>();
        audioSource.clip = gizmoLaughClip;
    }

    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Space))
        {
            audioSource.Play();
        }
    }
}

In the code above, we've added an Audio Source component to our Gizmo game object, played the audio clip when the spacebar is pressed.

Spatial Blend

Spatial Blend in Unity determines whether the audio source is 2D (plays the same across the entire scene) or 3D (plays in a particular location and thus is affected by distance and direction).

This is a powerful feature, particularly in designing immersive environments. For instance, if we had a spaceship passing by in our game, we would want the sound of the spaceship to originate from the spaceship's location and fade away as it moves further away.

By default, an audio source's spatial blend is set to 2D. We can modify this through the inspector or in our C# script.

void Start()
{
    audioSource = GetComponent<AudioSource>();
    audioSource.spatialBlend = 1.0f;  //sets audio to 3D
}

In the above snippet, the spatialBlend property is set to 1.0f, setting the audio to 3D.

Sound Effect Timing

Sometimes, we want to control the exact moment of playing a sound effect. This could be the fires blasting from a spaceship's engines or the explosion sound at the moment of an impact. This can be easily achieved using Unity's AudioSource.PlayDelayed method.

public float delay = 2.0f; //a delay of 2 seconds

void Start()
{
    audioSource = GetComponent<AudioSource>();
    audioSource.PlayDelayed(delay);
}

In this example, the PlayDelayed function plays the audio source with a delayed start. The delay variable is the time, in seconds, to delay before starting the audio source.

Volume and Pitch

Using Unity, we can control the loudness of our sound effects and their pitch. The volume modifies the loudness while the pitch modifies the frequency, essentially enabling us to speed up or slow down the playback speed.

void Start()
{
    audioSource = GetComponent<AudioSource>();
    audioSource.volume = 0.5f; //half the maximum volume
    audioSource.pitch = 1.5f; //increases the speed of the sound
}

In this code example, volume is set to 0.5f, halving the sound effect's loudness. The pitch is increased to 1.5f, speeding up the sound's playback.

Looping Sounds

Looping sounds are crucial in games for repeating background music or continuous sound effects like roaring waterfalls. Luckily, Unity has a built-in feature for this in the form of the loop property.

void Start()
{
    audioSource = GetComponent<AudioSource>();
    audioSource.loop = true;
    audioSource.Play();
}

Here, setting the loop property to true will make the audio source continuously repeat the audio clip once it's finished.

These are the basic concepts and features you will employ while working with Unity Audio. In the next chapter, we'll learn about Unity's advanced audio manipulation tools — the Audio Mixer and its various subviews, including the Reverb and Distortion effects.

0 Comment


Sign up or Log in to leave a comment


Recent job openings

Egypt, Alexandria, Alexandria Governorate

Remote

Python

Python

Java

Java

+5

posted 1 week ago

United Kingdom

Remote

Full-time

TypeScript

TypeScript

React

React

posted 1 week ago

Egypt, Alexandria, Alexandria Governorate

Remote

JavaScript

JavaScript

Ruby

Ruby

posted 1 week ago

United Kingdom, London, England

Remote

Contract

Swift

Swift

Unreal Engine

Unreal Engine

posted 1 week ago

United Kingdom, London, England

Remote

Python

Python

C++

C++

posted 1 week ago