- Home /
 
               Question by 
               ItArth · Jan 25, 2020 at 10:56 PM · 
                audiosourceincreasedecreasedaycycle  
              
 
              Decrease and Increase source
I have a Day/Night Audio effect, I want that when the Day timeline reach 16:00 the source decrease and increase to be the night audio effect like a fade with an audio source, how i can make that with this script?:
         private LSkyTOD skyTod;
 
         public AudioSource DayNightAudio;
         public AudioClip DayClip;
         public AudioClip NightClip;
 
         private bool PlayingDay = false;
 
         private bool isDay;
         private bool isNight;
 
         public float startNight = 17;
         public float startDay = 5.7f;
 
         void Start()
         {
             skyTod = GetComponent<LSkyTOD>();
             DayNightAudio.loop = true;
         }
 
         void Update()
         {
             if (skyTod.timeline >= startNight || skyTod.timeline <= startDay)
             {
                 isNight = true;
                 isDay = false;
             }
 
             else
             {
                 if (skyTod.timeline >= startDay)
                 {
                     isNight = false;
                     isDay = true;
                 }
             }
 
             if (isDay && !PlayingDay)
             {
                 DayNightAudio.Stop();
                 DayNightAudio.clip = DayClip;
                 DayNightAudio.Play();
                 PlayingDay = true;
             }
             else if (isNight && PlayingDay)
             {
                 DayNightAudio.Stop();
                 DayNightAudio.clip = NightClip;
                 DayNightAudio.Play();
                 PlayingDay = false;
             }
         }
     }
               Comment
              
 
               
              Answer by medinaline · Jan 26, 2020 at 12:12 AM
I'm not sure you can do that with a single audio source (DayNightAudio) swapping the clips between day and night. You need both to be playing at the same time to be able to cross fade them. 
 If you had two separate AudioSources, it would be easy to do it like this: 
 if (isDay && NightAudio.volume > 0)
 {
      DayAudio.volume = Mathf.Lerp(DayAudio.volume, 1, .05f);
      NightAudio.volume = Mathf.Lerp(NightAudio.volume, 0, .05f);
 }
 else if (isNight && DayAudio.volume > 0)
 {
      DayAudio.volume = Mathf.Lerp(DayAudio.volume, 0, .05f);
      NightAudio.volume = Mathf.Lerp(NightAudio.volume, 1, .05f);
 }
You can adjust the .05f amount to a smaller number (.025f) for a slower fade or larger number (.1f) for a quicker fade.
Your answer
 
 
              koobas.hobune.stream
koobas.hobune.stream 
                       
               
 
			 
                