- Home /
Fade between day and night music
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);
}
}
Looking at the time of day manager I found IsDay and IsNight bools which make the whole thing easier, though now I have the issue of the music fading when the game starts.
Here's the new start and update functions:
void Start () {
if (_time.IsNight) {
print ("It is currently night.");
day$$anonymous$$usic.outputAudio$$anonymous$$ixerGroup.audio$$anonymous$$ixer.SetFloat ("DayVolume", $$anonymous$$inVolume);
night$$anonymous$$usic.outputAudio$$anonymous$$ixerGroup.audio$$anonymous$$ixer.SetFloat ("NightVolume", $$anonymous$$axVolume);
}
if (_time.IsDay) {
print ("It is currently daytime.");
day$$anonymous$$usic.outputAudio$$anonymous$$ixerGroup.audio$$anonymous$$ixer.SetFloat ("DayVolume", $$anonymous$$axVolume);
night$$anonymous$$usic.outputAudio$$anonymous$$ixerGroup.audio$$anonymous$$ixer.SetFloat ("NightVolume", $$anonymous$$inVolume);
}
}
// Update is called once per frame
void Update () {
if (_time.IsNight) {
lerpValue += Time.deltaTime * TransitionSpeed;
DayVolume = $$anonymous$$athf.Lerp ($$anonymous$$axVolume, $$anonymous$$inVolume, lerpValue);
NightVolume = $$anonymous$$athf.Lerp ($$anonymous$$inVolume, $$anonymous$$axVolume, lerpValue);
}
if (_time.IsDay) {
lerpValue += Time.deltaTime * TransitionSpeed;
NightVolume = $$anonymous$$athf.Lerp ($$anonymous$$axVolume, $$anonymous$$inVolume, lerpValue);
DayVolume = $$anonymous$$athf.Lerp ($$anonymous$$inVolume, $$anonymous$$axVolume, lerpValue);
}
day$$anonymous$$usic.outputAudio$$anonymous$$ixerGroup.audio$$anonymous$$ixer.SetFloat ("DayVolume", DayVolume);
night$$anonymous$$usic.outputAudio$$anonymous$$ixerGroup.audio$$anonymous$$ixer.SetFloat ("NightVolume", NightVolume);
}
Another issue that just came up, it doesn't fade at sunrise or sunset, only when I start testing.
In-game I have the transition speed set to 0.3
Answer by tanoshimi · Mar 16, 2017 at 10:58 PM
The logic in your if conditions is wrong.
_time.timeline < 18.3f || _time.timeline > 5.6f // daytime
should be
_time.timeline < 18.3f && _time.timeline > 5.6f // daytime
I have it as or because the time of day manager goes from 0 to 24, so at 23.99 it loops back to zero. sunrise isn't until 5.5, so anything under that or above 18.3 is nighttime.
Either way that doesn't matter any more as I discovered the day/night system has IsDay and IsNight bools, as stated above.
I understand that you're using a 24hr clock, and that's why I pointed out your if condition was wrong. Every time is before 18.3 or 5.6, so that condition is always true.
In your new code, lerpValue only ever seems to increase - you need it to oscillate between 0 and 1.
So I changed it to set lerpValue to 0 once DayVolume and NightVolume hit their respective values, as well as set up bools so it doesn't loop. Unfortunately I have run into a new problem. I need it to set this new bool to true as soon as it hits night, but after it's set to false it shouldn't be set to true until sunrise. At the moment it only transitions once (at the start) and then stays at whatever music the game started on.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using AC.TimeOfDaySystemFree;
public class Stage$$anonymous$$usicFader : $$anonymous$$onoBehaviour {
public AudioSource day$$anonymous$$usic, night$$anonymous$$usic;
public float TransitionSpeed = 5;
float lerpValue = 0;
float $$anonymous$$inVolume = -80;
float $$anonymous$$axVolume = 0;
public TimeOfDay$$anonymous$$anager _time;
float DayVolume, NightVolume;
bool DidTransition = false;
// Use this for initialization
void Start () {
if ( _time.IsNight) {
print ("It is currently night.");
day$$anonymous$$usic.outputAudio$$anonymous$$ixerGroup.audio$$anonymous$$ixer.SetFloat ("DayVolume", $$anonymous$$inVolume);
night$$anonymous$$usic.outputAudio$$anonymous$$ixerGroup.audio$$anonymous$$ixer.SetFloat ("NightVolume", $$anonymous$$axVolume);
//lerpValue = 1;
}
if ( _time.IsDay) {
print ("It is currently daytime.");
day$$anonymous$$usic.outputAudio$$anonymous$$ixerGroup.audio$$anonymous$$ixer.SetFloat ("DayVolume", $$anonymous$$axVolume);
night$$anonymous$$usic.outputAudio$$anonymous$$ixerGroup.audio$$anonymous$$ixer.SetFloat ("NightVolume", $$anonymous$$inVolume);
//lerpValue = 1;
}
}
// Update is called once per frame
void Update () {
if (!DidTransition)
{
if (_time.IsNight) {
TransitionNight ();
}
}
if (!DidTransition)
{
if (_time.IsDay) {
TransitionDay ();
}
}
day$$anonymous$$usic.outputAudio$$anonymous$$ixerGroup.audio$$anonymous$$ixer.SetFloat ("DayVolume", DayVolume);
night$$anonymous$$usic.outputAudio$$anonymous$$ixerGroup.audio$$anonymous$$ixer.SetFloat ("NightVolume", NightVolume);
}
void TransitionDay()
{
DidTransition = false;
lerpValue += Time.deltaTime * TransitionSpeed;
NightVolume = $$anonymous$$athf.Lerp ($$anonymous$$axVolume, $$anonymous$$inVolume, lerpValue);
DayVolume = $$anonymous$$athf.Lerp ($$anonymous$$inVolume, $$anonymous$$axVolume, lerpValue);
if (NightVolume == $$anonymous$$inVolume && DayVolume == $$anonymous$$axVolume) {
lerpValue = 0;
DidTransition = true;
}
}
void TransitionNight()
{
DidTransition = false;
lerpValue += Time.deltaTime * TransitionSpeed;
DayVolume = $$anonymous$$athf.Lerp ($$anonymous$$axVolume, $$anonymous$$inVolume, lerpValue);
NightVolume = $$anonymous$$athf.Lerp ($$anonymous$$inVolume, $$anonymous$$axVolume, lerpValue);
if (DayVolume == $$anonymous$$inVolume && NightVolume == $$anonymous$$axVolume) {
lerpValue = 0;
DidTransition = true;
}
}
}
Your answer