Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question How to have Audio Mixer unaffected by timeScale?

Discussion in 'Audio & Video' started by mercurymane, Dec 16, 2021.

  1. mercurymane

    mercurymane

    Joined:
    Dec 16, 2021
    Posts:
    2
    I have a pause menu that sets the timeScale to 0, but this affects the fading out audio when the player selects to go back to the main menu.

    This is Audio Mixer fading script:
    Code (CSharp):
    1. public static class FadeAudioMixer
    2. {
    3.     public static IEnumerator StartFade(AudioMixer audioMixer, string exposedParam, float duration, float targetVolume)
    4.     {
    5.         float currentTime = 0;
    6.         float currentVol;
    7.         audioMixer.GetFloat(exposedParam, out currentVol);
    8.         currentVol = Mathf.Pow(10, currentVol / 20);
    9.         float targetValue = Mathf.Clamp(targetVolume, 0.0001f, 1);
    10.  
    11.         while (currentTime < duration)
    12.         {
    13.             currentTime += Time.deltaTime;
    14.             float newVol = Mathf.Lerp(currentVol, targetValue, currentTime / duration);
    15.             audioMixer.SetFloat(exposedParam, Mathf.Log10(newVol) * 20);
    16.             yield return null;
    17.         }
    18.         yield break;
    19.     }
    20. }
    Edit: I've tried setting the timeScale to 1 before the fading, but this "unpauses" the world and character as the fading happens.
     
  2. JLF

    JLF

    Joined:
    Feb 25, 2013
    Posts:
    137
    If you change currentTime += Time.deltaTime to use unscaled time, like this:

    Code (CSharp):
    1. currentTime += Time.unscaledDeltaTime;
    It should work.

    Hope that helps.
     
  3. mercurymane

    mercurymane

    Joined:
    Dec 16, 2021
    Posts:
    2
    Perfect! Thank you sooo much!