UnityScripterAI
Discord ↗
Link Copied to Clipboard
×
"Music Fade-In that makes volume go smoothly from 0 to 1 using coroutines and lerping"
using UnityEngine; using System.Collections; public class MusicFadeIn : MonoBehaviour { public AudioSource audioSource; public float fadeInTime = 2f; // Start is called before the first frame update void Start() { audioSource.volume = 0f; audioSource.Play(); StartCoroutine(FadeInMusic()); } IEnumerator FadeInMusic() { float currentTime = 0f; while (currentTime < fadeInTime) { currentTime += Time.deltaTime; audioSource.volume = Mathf.Lerp(0f, 1f, currentTime / fadeInTime); yield return null; } } }