How to add background MUSIC and play SOUNDS in Unity

Introduction

In this article we will see how to play music and sound effects in Unity, making them play automatically when starting the game mode, but we will also see how to create a control script to play music or sounds in Unity through code.

To play audio clips in Unity we use a component called AudioSource, this component allows us to configure different parameters such as volume, make it play in loop, among others.

The AudioSource component by itself is not enough to listen to the audio in Unity, it is also necessary to have an AudioListener component in the scene, this component is usually assigned to the camera by default when creating a new scene, the AudioListener acts as the “ears” that hear the sound played by the AudioSources. We must make sure that there is only one AudioListener component in the scene, especially if we are using 3D sound effects, that is to say that we take into account the position from where the sound is originating, for example if a sound is produced to the right of the player’s perspective, that the sound is heard with greater volume in the speakers on the right.


How to play an AUDIO CLIP in Unity at startup

1. Create a GameObject and in the inspector assign the AudioSource component (Add Component button).

2. Configure the AudioSource component with the “Play On Awake” option activated and the rest of the parameters configured as you like (see below for the different options).

3. When you enter the game mode, the sound will start playing automatically with the parameters set.

How to play an AudioClip in Unity through code

1. Create a GameObject and in the inspector assign the AudioSource component (Add Component button). Deactivate the “Play on Awake” option and configure the other parameters as you wish.

2. Create a Script to control the start of the sound, the volume, the end, among other actions. Assign this Script to the same GameObject that has the AudioSource component.

3. In the script define a reference for the AudioSource object and find it in the Start method using GetComponent<>().

4. Define a public function to play the sound from any other script, within this function execute the instruction: “audioSource.Play()”.

5. Detect the proper event in which the sound should be played and execute the play function defined in the audio script.

AudioSource component setup

The AudioSource component has many adjustable parameters to achieve different results with our sounds. In the following image we see what the AudioSource component looks like in the inspector in Unity.

Fig. 1: Generic AudioSource component in Unity

To start with, you can manually assign the sound you want to play by dragging it to the AudioClip field. This can also be done from a script with the instruction “audioSource.clip=aClip;”, being “audioSource” the reference of the AudioSource component of the inspector and “aClip” a field that contains the reference of some sound.

Then we can mute the sound and adjust the volume. Enable the “Play On Awake” option so that the sound is played as soon as the GameObject enters the enabled state. The Loop option makes the sound play again when it is finished.

The Spatial Blend is a parameter that allows us to adjust if it is a background sound or a sound that depends on the distance between the AudioSource and the AudioListener. If we put the indicator in 3D, we will obtain a stereo sound whose volume and distribution in the headphones will depend on the position of the source. Within the 3D Sound Settings you can adjust parameters related to this.

Exit mobile version
Secured By miniOrange