How to add audio effects in Unity

Audio effects are an essential part of game development, enhancing the game experience in Unity. They play an essential role in creating immersion and providing feedback to the game players.

Audio effects are not limited to adding audio; developers can create a more dynamic soundscape by adding reverb, distortion, echo, and filters.

Games might seem confusing or lifeless, and players might miss important events or cues without audio effects. Thus, it is an essential tool for creating engaging and captivating games.

How to add audio effects

Here is a detailed guide on how to add audio clips in Unity.

Import the audio clips

  • Open the Unity project

  • Go to Assets and select Import new Asset

  • Navigate to the audio files and click Import

Create an Audio Source

  • Select the Gameobject you want to add the audio clip to

  • Click Add component in the Inspector pane and search Audio Source

  • Click on the Audio Source and add it to the GameObject

Assign the audio clip

  • In the Audio Source component, navigate to AudioClip field

  • Drag and drop the imported audio clip to the AudioClip field

Configure the Audio Source

There are several parameters in the Audio Source component which you can adjust to change the sound.

  • Play on awake: If you enable this option, the sound starts playing as soon as the GameObject is active in the scene.

  • Loop: If this option is enabled, the sound repeats after finished playing.

  • Volume: It helps in controlling the loudness of the sound.

  • Pitch: A higher value in this option makes it more sharp, and the lower value makes it more deep.

  • Spatial Blend: It determines how the sound can be affected by the 3D positioning. The sound behaves as 2D at position 0. At 1, it is a full 3D sound and keeps on changing based on the object's position.

  • Reverb Zone Mix: It determines how the sounds can be affected by the reverb zonesTakes the audio clip and distort it based on the listener's position..

This Answer discusses the use of audio clips with buttons. You can attach the clips to other GameObjects as well. They need to have the Audio Source component attached to them first.

Create a C# script

A sample script is given below.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class sampleScript : MonoBehaviour
{
public AudioSource audioName;
public void PlayAudio()
{
audioName.Play();
}
}
Explanation

Lines 1–3: These lines import all the required namespaces.

Line 5: A class is declared named sampleScript. It inherits methods from the class MonoBehaviour, a base class in Unity for scripts attached to the specific GameObjects.

Line 7: It declares an audioName variable of type AudioSource. Since this variable is public, it will appear in the Inspector window of the object this script is attached to. The

Line 9: A method PlayAudio is declared.

Line 11: It calls the Play method on the AudioSource. It is a built-in function in Unity which plays audio.

Assign script to Canvas

  • Assign the C# script to the canvas

  • Drag and drop the Audio Source from the Buttons to the serialized fields (the public variables) of the script in the canvas.

    • In our example, we drag and drop the Beep button (with the Audio Source) to the "Beep" field of the C# script in the canvas.

  • Repeat the process for each button.

Set up On Click() listener

  • Click on the button.

  • In the Inspector pane, navigate to the On Click() list.

  • Click the plus icon.

  • Drag and drop the canvas to in the empty field.

  • In the properties drop down, select the method from the script that you want to call when the button is clicked. In our case, we want to call the method that plays the corresponding audio when the button is clicked.

  • Repeat the process for all the GameObjects

After hitting play, you should be able to listen the sound effects when the corresponding buttons are clicked.

Demonstration

In the slides, audio clip is being attached to the canvas object. It plays the audio differently (Audio is played when user clicks the button) as compared to when it is attached to the GameObjects.

Note: To follow along the steps mentioned above, you need to have the UI set up.

Import the audio clips
1 of 6

Sample project

Explanation

In this above project, each UI button plays different audio which is being imported by the steps mentioned above.

Each button is assigned a different function to play the audio. The audio is being assigned by the methods mentioned in the sample C# script.

Note:

You can create multiple AudioSource and PlayAudio functions to play the different audios.

Example
  • PlayBeep() function is attached to the Beep button to play the audio.

  • PlayBang() function is attached to the Bang button to play the audio.

  • PlayDing() function is attached to the Ding button to play the audio.

  • PlayGunshot() function is attached to the Gunshot button to play the audio.

Continue reading about Unity


Copyright ©2024 Educative, Inc. All rights reserved