Fade out:
IEnumerator FadeOut(
AudioSource source,
float duration)
{
float start =
source.volume;
float time = 0f;
while (time < duration)
{
time +=
Time.unscaledDeltaTime;
source.volume =
Mathf.Lerp(
start,
0f,
time / duration
);
yield return null;
}
source.volume = 0f;
}
Use unscaled time when fades must work during pause.