How to Add Different Audios in Unity (Part 2) | Unity 3D

Posted March 12, 2020 by Joudet Ahsan in Unity

In precious tech-recipe, we taught you basic audio addition to the game. In this tech-recipe, we will cover the addition of all sorts of sounds in the game. Check out this tech-recipe, How to Add Different Audios in Unity

There are three important things that you need to know before you move along with the addition of audio. The unity game needs three things to play audio. The source, listener and audio clip. If you want to play audio at certain points, you can use trigger colliders. Scripts are the way to go.

To simulate effect o position, unity requires sound to originate from audio sources attached to objects. The sounds emitted are then picked up by an audio listener attached to some other object in the game. By default, the camera is the listener. There can only be one listener in unity and one or more sources.

Unity can then simulate the effect of a source’s distance and position from the listener object and play them to the user accordingly. Unity can even access the computer microphones from a script and create audio clips by direct recording.

Let’s discuss Audio clips, Audio Source and Audio Listener.

Audio Sources

The audio source plays back the audio clip that we provide. It attaches to an object like the other components. For Example, you have a zombie in your scene, it has to have an audio source and a zombie voice clip inserted in it.

There are numerous properties to audio sources.

3D Sound Settings

Other Settings

Audio Listener

An audio listener, as the name suggests listens from the audio source and plays it. There can only be one audio listener. By default, camera is the listener. But you can always change that and make your third-person player an audio listener. When the audio listener is in the vicinity of a source, it will listen and output to your computer speakers.

While audio itself has many properties, like reverberation i.e echo, etc. Unity provides a many as possible.

Audio Scripting

If you are developing a  game, you do not only need to background audio. There are times when you need audio to start, pause or even stop playing. Audios need to be played on an event basis. Here’s the list of audio functions that you can use to eventually play audio in your game.

This is an example of audio scripting in the Roll a ball game.

How to Add Different Audios in Unity

Step-by-Step Scripting:

1. Introduce the audio source variable.

private AudioSource myAudio;

2. Initialize the audio source in start()

myAudio = GetComponent<AudioSource> ();

3. Make sure to drop the clip in the source

4. Simply play the audio in the trigger or whichever event you want to play it in.

myAudio.Play ();

Check out these tech-recipes

How to Add Audio in Unity | Unity 3D Game Development

How to Pick Objects in Unity | Unity 3D Game Development

Use Joints in Unity | Unity 3D Game Development

How to Use Material and Physics Material in Unity | Unity 3D

How to Use Lights and Cookies in Unity to Beautify the Scene (Part 2)

Add Trees and Windzones to Simulate Air in Unity

How to Move the Player in Unity | Force and Physics in Unity

 

About Joudet Ahsan

SEO Content Creator, a Computer Scientist in making and someone who sucks at writing bios.
View more articles by Joudet Ahsan

The Conversation

Follow the reactions below and share your own thoughts.

Leave a Reply