Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // FADEOUT
- public IEnumerator FadeOut(AudioSource audioSource, float duration)
- {
- float startVolume = audioSource.volume;
- float time = 0f;
- while (time < duration)
- {
- time += Time.deltaTime;
- audioSource.volume = Mathf.Lerp(startVolume, 0f, time / duration);
- yield return null;
- }
- audioSource.volume = 0f;
- audioSource.Stop();
- }
- // FADEIN
- public IEnumerator FadeIn(AudioSource audioSource, float duration, float targetVolume = 1f)
- {
- float time = 0f;
- audioSource.volume = 0f;
- audioSource.Play();
- while (time < duration)
- {
- time += Time.deltaTime;
- audioSource.volume = Mathf.Lerp(0f, targetVolume, time / duration);
- yield return null;
- }
- audioSource.volume = targetVolume;
- }
Advertisement
Add Comment
Please, Sign In to add comment