Once the class is in your project all you have to do is use one line of code to call it from anywhere you like. The stepping effect gets much worse at lower frame rates. Unlike the audio itself which is made up of tens of thousands of samples being processed every second, the fade is limited to whatever frame rate the game is running at.Īt high frame rates, such as 60fps, this isn’t a problem, as the stepping effect isn’t audible, although it is visible when inspecting a recording of the audio waveform: It means that the script can only change the volume as frequently as it is updated. Secondly, because the changes to the volume are frame rate dependent, you’ll get a stepped volume change while fading out. Then every time you want to trigger a fade, use this line of code, passing in the parameters for the Audio Source, the duration and the volume you want to fade to.įor example: StartCoroutine(FadeAudioSource.StartFade(AudioSource audioSource, float duration, float targetVolume)) What are the drawbacks of using this method?įirst off it’s limited to only one Audio Source so this really only works for single Audio Sources or for music tracks. Public static IEnumerator StartFade(AudioSource audioSource, float duration, float targetVolume)ĪudioSource.volume = Mathf.Lerp(start, targetVolume, currentTime / duration) Just copy the script below into a new class called FadeAudioSource (you don’t even have to add it to an object): using System.Collections Just use whatever target volume you want and the script will do the rest. Set it to 0 to fade it out, or smoothly change to any volume in between, all with one simple function. You can fade audio in by setting the target volume to 1. There’s no separate function for fading in and out. It works by Lerping the Audio Source volume over a set duration and produces an even fade, just as you’d expect. This is the easy method, and the simplest way to fade any single Audio Source.
#Play sounds in unity how to
Method 1: How to fade an Audio Source (the easy method) Method 3: Fade using Audio Mixer Snapshots.
The Snapshot Method produces a smooth fade, just like with the second method, and it’s quite simple, since it doesn’t require coroutines. The third method is to use an Audio Mixer Snapshot.This is the best overall method for fading audio in Unity. Like the first method the fade is even and linear, but only if you use logarithmic conversion (I’ll explain why later on). This method requires an Audio Mixer to work but produces a much smoother fade. The second method, and technically the best method, is to still use Lerp in a coroutine but, instead of fading the Audio Source directly, fade an Audio Mixer group.This method is fine, and very convenient, it’s just not the best way to do it. In this case using a coroutine to Lerp the volume from one value to another over a set duration for an even, linear fade. The first method, which I’m calling the easy method, is to fade the Audio Source directly.What are the other options then, and why is this method the best? Generally, there are three different ways to fade audio in Unity: Doing it in this way produces a very smooth, linear fade, even at lower frame rates. What’s the best method for fading audio in Unity? Overall, the best way to fade audio in Unity is to Lerp an Audio Mixer Group’s volume using a coroutine over a set duration with a logarithmic conversion calculation. So I actually tested the different methods to find out, once and for all, which one is the best, and why. While I do think that the most convenient method often is the best method, I wanted to know if there was a noticeable difference in quality between doing it one way and doing it another way. Often I would resort to whatever method was the most convenient at the time. Fading audio in Unity is a simple thing to do, however, because there are several different ways to do it, it often gets overcomplicated.Įven as someone who works in game audio, I didn’t know for sure what the absolute best method was.