Answers for "how to fade out music in unity"

C#
1

how to fade out music in unity

//credit to gamedevbeginner.com
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public static class FadeAudioSource {

    public static IEnumerator StartFade(AudioSource audioSource, float duration, float targetVolume)
    {
        float currentTime = 0;
        float start = audioSource.volume;

        while (currentTime < duration)
        {
            currentTime += Time.deltaTime;
            audioSource.volume = Mathf.Lerp(start, targetVolume, currentTime / duration);
            yield return null;
        }
        yield break;
    }
}
Posted by: Guest on July-01-2020

C# Answers by Framework

Browse Popular Code Answers by Language