Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by ToeBeanGames · Mar 16, 2017 at 10:00 PM · musicdaycycle

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);
     }
 }
 

Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ToeBeanGames · Mar 16, 2017 at 10:11 PM 0
Share

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);
     }
avatar image ToeBeanGames ToeBeanGames · Mar 16, 2017 at 10:27 PM 0
Share

Another issue that just came up, it doesn't fade at sunrise or sunset, only when I start testing.

avatar image ToeBeanGames ToeBeanGames · Mar 16, 2017 at 10:27 PM 0
Share

In-game I have the transition speed set to 0.3

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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
Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ToeBeanGames · Mar 16, 2017 at 11:13 PM 0
Share

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.

avatar image ToeBeanGames ToeBeanGames · Mar 17, 2017 at 06:33 AM 0
Share

Either way that doesn't matter any more as I discovered the day/night system has IsDay and IsNight bools, as stated above.

avatar image tanoshimi · Mar 17, 2017 at 06:52 AM 0
Share

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.

avatar image ToeBeanGames tanoshimi · Mar 17, 2017 at 06:40 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

64 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

memory usage of compressed audio playerback iphone 1 Answer

Cycle things with one key 1 Answer

How can i create an AudioClip from byte array? 0 Answers

Trigger Start of Sound 1 Answer

Music continued in the all level 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges