how to fade audio if Time.timeScale = 0.0 ??

if (Input.GetKeyUp(“p”)|| (Input.GetButtonDown(“Fire2”))) {
if(paused == true){
paused = false;
}else{
paused = true;

       }
      
        if(paused == true){
        Time.timeScale = 0.0;
       pausedGUI.enabled = true;
       rigidbody.isKinematic = true;
          audio.volume = 0.05;
        }else{
        
       Time.timeScale = 1.0;
         pausedGUI.enabled = false;
         rigidbody.isKinematic = false;
         audio.volume = 0.2;
         }
         }

Done thx for the help

 if (Input.GetKeyUp("p")|| (Input.GetButtonDown("Fire2"))) {
       if(paused == true){
       paused = false;
          }else{
       paused = true;

       }


        if(paused == true){
        Time.timeScale = 0.0;
       pausedGUI.enabled = true;
       rigidbody.isKinematic = true;
          
        }else{

       Time.timeScale = 1.0;
         pausedGUI.enabled = false;
         rigidbody.isKinematic = false;
       
         }
         }
         if(paused == true){
         (VolOut());
         }else{
         (Volin());
         }

         function VolOut(){
         while(audio.volume>0.05){
         audio.volume-=0.001;
         yield;
         }
         }
         function Volin(){
         while(audio.volume<0.2){
         audio.volume += 0.001;
         yield;
         }
         }

3 Answers

3

Coroutine still runs while timeScale is set to 0. You can then use a variable that is equivalent to deltaTime to be passed to the coroutine.

void Update(){
   if(pause){
      float t = Time.deltaTime;
      StartCoroutine(VolOut(t));
      Time.timeScale = 0;
   }
}
IEnumerator VolOut(float t){
   while(audio.volume>0.2){
     audio.volume-=t;
     yield return null;
   }
}

EDIT. Js version to be confirmed by someone that really knows about Js

function Update(){
   if(pause){
      var t = Time.deltaTime;
      StartCoroutine(VolOut(t));
      Time.timeScale = 0;
   }
}
function VolOut(t:float){
   while(audio.volume>0.2){
     audio.volume-=t;
     yield;
   }
}

sorry C# is not me, i just starte using javascript in february, can you translate it ??

I can translate it but can you understand it? I don't mean to be rude or looking down on you but there is no point for you to use code that do not understand yet. You 'd rather use a more simple alternative that you manage but does not do everything you want. Later you get back on it and make it do exactly what you want with a big smile of "I just totally get it".

oki i no i just starte in this new world, but my game is all most done, http://www.youtube.com/watch?v=NDN2Ul5xT3k newest video of i

@fafase you don't need the StartCoroutine in JS. It's done magically.

I know. I am just preparing his conversion to C#.

Hello and welcome to the community!

I searched the forums and found something that you could try:

Unity Answer


If that isn’t what you are looking for, please let us know.

no i cant use that, time scale needs to be 1 for that to work i am looking for a way to fade if time scale is 0

Right. That method uses "yield WaitForSeconds(..)" and seconds never happen. (untested) Replacing with "wait one frame" would probably work, which is yield return null;. You'd have to lower the changeBy amount appropriately, since it runs more often.

Time.realTimeSinceStartup runs even while timeScale is set to zero.

i just tryed print(Time.realtimeSinceStartup); and it runs, now to find out have to use it :)

Ten years later, but anyway “Time.fixedDeltaTime” is the one to use instead of “Time.deltaTime” when you want it to run while Time.timeScale = 0.0

And 10 years later this is incorrect. If need delta time independent of time scale, there are unscaled versions: https://docs.unity3d.com/6000.0/Documentation/ScriptReference/Time-unscaledDeltaTime.html