In this article, I will introduce how to change the volume of an audio source according to it’s distance from the player.
This setting is useful when you want to make a scene where you want the sound to become bigger as you approach a certain object.
The sound will become smaller as you take distance from the object.
Confirm sample assets with WebEditor
* If you register STYLY, you can check the sample assets created with this TIPS with WebEditor.
If you do not register an account, please register again and click this button again.
You can experience this setting in a sample scene at STYLY GALLERY.
The Unity project introduced in this article can be downloaded here.
Adding an Audio Source
In Unity, add a component in order to use Audio.
Select an object you want to use as the source of the sound, and click Add Component.
Type “Audio” in the search window and choose Audio Source from the list.
An Audio Source has been added.
Next, we need to set an audio file to the Audio Source.
Adding an Audio Clip
Choose a audio file you want to use and add it as an Audio Clip.
In this example, I will use a file named “Don”.
Set how you want the file to be played.
Play On Awake : Play the sound file immediately when the scene starts to play
Loop : Loop the audio file
Setting the audio source as a 3DSound
With the initial settings, the audio will sound indifferently in different distances.
We need to add an additional setting to make it a 3DSound.
Change the Spatial Blend parameter to 1(3D).
The Audio Source has become a 3DSound.
You can set the sound change amount using Volume Rolloff.
Logarithmic Rolloff : The sound rapidly changes according to distance
Linear Rolloff : The sound gradually changes according to distance
In this article, I introduced how to use 3DSound in Unity.
For example, you can use 3DSound to make a scene in where you can hear the character’s voice when you get near the character.
Setting sound effects on objects, or setting a BGM so the player can hear it when he or she approaches would also work nicely.
Depending on the scene, try using the better suited option, 2DSounds or 3DSounds.
How to upload an asset to STYLY using Unity
Read this article