So before I start, it’s not the fading I need help with. It’s when it fades.
Pretty much i have a music manager set up with the time of day manager so that once the time of day manager’s time reaches sunrise or sunset, the music will fade to day or night music, respectively. I also have it set up so that if the game starts while it’s daytime it plays the day music, and if the game starts at night it plays the night music.
This is where I’m running into problems.
The audio manager script worked perfectly fine until i put audio clips in. Now if I start during the day it doesn’t mute the night music, and if I start during the night it plays the day music and fades to night. As I said earlier, before I put audio in, it worked fine. Now I’ve tried using bools to control it among other things, with no luck. The bools actually made it worse, somehow reversing the day and night music.
Here’s the music manager script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using AC.TimeOfDaySystemFree;
public class StageMusicFader : MonoBehaviour {
public AudioSource dayMusic, nightMusic;
public float TransitionSpeed = 5;
float lerpValue = 0;
float MinVolume = -80;
float MaxVolume = 0;
public TimeOfDayManager _time;
float DayVolume, NightVolume;
// Use this for initialization
void Start () {
if (_time.timeline >= 18.3f || _time.timeline < 5.6f) {
print ("It is currently night.");
dayMusic.outputAudioMixerGroup.audioMixer.SetFloat ("DayVolume", MinVolume);
nightMusic.outputAudioMixerGroup.audioMixer.SetFloat ("NightVolume", MaxVolume);
}
if (_time.timeline < 18.3f || _time.timeline > 5.6f) {
print ("It is currently daytime.");
dayMusic.outputAudioMixerGroup.audioMixer.SetFloat ("DayVolume", MaxVolume);
nightMusic.outputAudioMixerGroup.audioMixer.SetFloat ("NightVolume", MinVolume);
}
}
// Update is called once per frame
void Update () {
if (_time.timeline >= 18.3f) {
lerpValue += Time.deltaTime * TransitionSpeed;
DayVolume = Mathf.Lerp (MaxVolume, MinVolume, lerpValue);
NightVolume = Mathf.Lerp (MinVolume, MaxVolume, lerpValue);
}
if (_time.timeline == 5.6f) {
lerpValue += Time.deltaTime * TransitionSpeed;
NightVolume = Mathf.Lerp (MaxVolume, MinVolume, lerpValue);
DayVolume = Mathf.Lerp (MinVolume, MaxVolume, lerpValue);
}
dayMusic.outputAudioMixerGroup.audioMixer.SetFloat ("DayVolume", DayVolume);
nightMusic.outputAudioMixerGroup.audioMixer.SetFloat ("NightVolume", NightVolume);
}
}