Search Unity

Setting Time.time to 0 pauses all timeline tracks except from audio

Discussion in 'Timeline' started by crdmrn, Dec 16, 2018.

  1. crdmrn

    crdmrn

    Joined:
    Dec 24, 2013
    Posts:
    152
    As the title says, I'm setting Time.time to 0 in order to pause my game, as well as AudioListener.pause to true to pause all sounds. But this has no effect on the audio tracks of my timeline, which keep on playing regardless of everything else. It feels like it's an unwanted behavior, or that I'm missing something... anyone got an idea? ^^"
     
  2. MadeFromPolygons

    MadeFromPolygons

    Joined:
    Oct 5, 2013
    Posts:
    3,980
    You should never set time.time to 0 for pauses. That is a terrible way to do pausing. You should have a flag, lets call it IsPaused, that you enable disable when pausing/unpausing. Then do a check in all logic against this on anything you want to not happen when paused.

    Code (CSharp):
    1. if(!IsPaused)
    2. {
    3. //Do stuff that is pausable here
    4. }
    5. else
    6. {
    7. // here is what happens when paused
    8. }
     
  3. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    6,609
    How would you pause things you don't really have control over, such as Physics, Animation, Particles and so on...?
     
    crandellbr and Menion-Leah like this.
  4. MadeFromPolygons

    MadeFromPolygons

    Joined:
    Oct 5, 2013
    Posts:
    3,980
    I would actually pause those, using stuff like https://docs.unity3d.com/ScriptReference/ParticleSystem.Pause.html etc

    Rather than flipping one big switch and hoping that the right systems are being paused, and the right ones are being left running which is what setting time.time = 0 is like.

    Here are some good discussion around the topic:

    https://answers.unity.com/questions/174448/stop-physics-without-using-timetimescale-0.html

    Edit: but essentially, if you bind everything to that flag, it should be easy to disable/enable/pause any behaviours you require. This includes stopping animators and timelines, pausing audio sources etc.

    On our big scale projects we normally make a pause manager for this purpose that helps keep track of what requires what level of activity during pause, and sets things back when unpaused
     
    Peter77 likes this.
  5. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    6,609

    Overwriting actual state of objects and then having to restore it afterwards, sounds pretty error prone to me.

    I've never had issue with Time.timeScale=0 though. What problems did you run into try to solve by not using Time.timeScale=0? I would like to know/understand those issues, so I can doge them in the future.
     
  6. MadeFromPolygons

    MadeFromPolygons

    Joined:
    Oct 5, 2013
    Posts:
    3,980
    Anything that requires behaviour to continue, so if you want any animations to play when paused such as an idle animation etc they wont work. Most of our applications are simulations which require stuff to execute over time even when paused in the background so they have always required a proper pause procedure.

    If you want literally everything to stop, as if the game is frozen, then sure timescale is a perfectly fine solution. But its still better to actually track what your doing and manage it yourself. And no its only error prone if you dont understand what your own code is doing, As in if you have a good handle on everything, such as what a manager will give you, then it is no issue, or at least no different to keeping track of any other collection of objects and their states.
     
  7. MadeFromPolygons

    MadeFromPolygons

    Joined:
    Oct 5, 2013
    Posts:
    3,980
    EDIT: @Peter77 I should clarify and say that the method I recommend only is better over yours if you specifically need things to continue to happen during pause. If not then you should just use timescale as it is a quicker solution to implement :)
     
  8. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    6,609
    Every game I worked so far, needed to stop every ingame stuff on pause, so I probably never ran into this issue.

    For animations and sounds inside the pause menu that need to run even if timeScale=0, I just configure the UI Animator to use UpdateMode=UnscaledTime and the AudioSources to use AudioSource.ignoreListenerPause=true. This by-passes timescale, allowing me to have animated menus when the game is paused, thus was never a problem for me.
     
    imaginadio and Menion-Leah like this.
  9. crdmrn

    crdmrn

    Joined:
    Dec 24, 2013
    Posts:
    152
    I couldn't have explained my reasons better, so thanks a lot for doing this for me :D

    That said, I still need an explanation to my original question which is why do Time.timeScale = 0 stop everything except audio tracks in a running playableasset?
     
  10. seant_unity

    seant_unity

    Unity Technologies

    Joined:
    Aug 25, 2015
    Posts:
    1,516
    Because audio always runs against the DSP clock, even when the timeline is set to use game time. Switching the timescale to 0 doesn't trigger it to 'pause'. It probably should though.
     
  11. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    6,609
    I don't think it should. To comply with how other Unity systems work in my experience, audio should pause if the AudioListener.pause flag is set to true. Audio output isn't bound to Time.timeScale in Unity as far as I know.

    https://docs.unity3d.com/ScriptReference/AudioListener-pause.html
     
    Menion-Leah likes this.
  12. scottnc27603

    scottnc27603

    Joined:
    Jul 20, 2014
    Posts:
    3
    I have a training application and I want the user to be able to explore the scene and have the audio playing the entire time whlie the Timeline is paused. I want to control the animations based upon user interactions with the scene. I'm trying to use the Timeline to halt the state of the scene until the user clicks on a certain item within the scene. I've seen where people are suggesting an alternate method of pausing the scene(Time.time is now read only so this is out of date with my version of Unity), but I'm still not sure how to do this. I'm probably missing something easy, but I'm new to the Timeline. TIA, Scott
     
  13. crdmrn

    crdmrn

    Joined:
    Dec 24, 2013
    Posts:
    152
    @seant_unity @Peter77 As I mentioned in my first post, I'm also using AudioListener.pause to pause all sounds, but that also has no effect on the Timeline's audio tracks (while it works as intended for all the other audiosources in the scene). So I guess whether I'm missing something, or it's a bug.
     
    Menion-Leah likes this.
  14. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    6,609
    It sounds like a bug to me.

    I would submit a bug-report with a project to reproduce the issue, as described in this document:
    https://unity3d.com/unity/qa/bug-reporting

    Submitting a bug-report allows Unity Technologies to take a look at this issue.

    After you submitted the bug-report, you receive a confirmation email with a bug-report Case number. You can post this Case number here (in this forum thread) for Unity staff to pick up, in case they see your post.
     
  15. seant_unity

    seant_unity

    Unity Technologies

    Joined:
    Aug 25, 2015
    Posts:
    1,516
    @crdmrn That does sound like a bug. AudioListener.pause controls the DSPtime which should affect timeline. @Peter77 is spot on, a bug report will allow us to investigate.
     
    Peter77 likes this.
  16. FlashyGoblin

    FlashyGoblin

    Joined:
    Apr 1, 2017
    Posts:
    23
    Has this been fixed yet? I'm on 2018.3.5 and it is still happening. Any workaround found yet?
     
  17. seant_unity

    seant_unity

    Unity Technologies

    Joined:
    Aug 25, 2015
    Posts:
    1,516
    I don't believe the issue has been looked at yet. At least I haven't seen any related changes. And I can't find a relevant bug for it...if I missed it, maybe someone could post the case #?
     
  18. gustavopinent

    gustavopinent

    Joined:
    Jan 14, 2019
    Posts:
    38
    Hi! I am making my first games in Unity, but got some experience in other languages. Unity does not seems to work with global stuff, so Time is just described as "The interface to get time information from Unity", and the static property timeScale as "The scale at which the time is passing. This can be used for slow motion effects." It seems to me there is no canon answer for how to pause the game, neither why soundtrack won't use Time as reference, audio got it's own Time. Unity seems a lot of objects inside a scene talking together and there is few things global (maybe none really). That said, though the pause function will have at list to things to do: stop time and stop music, but might have other things still.

    I honestly didn't find a real reason for avoiding Time.timeScale = 0 as it is available and there is no restrictions in documentation. Scripting each object with if schemas to pause, all rigid bodies to stop... I personally think about Murphy's law and keep with the simple solution. I would like to see an example, a real case for a comparison between pause technics.
     
  19. DGordon

    DGordon

    Joined:
    Dec 8, 2013
    Posts:
    649
    Yeah ... I kind of find it frustrating when people ask a question, and someone else chimes in "thats wrong!". ... Maybe the project isn't that big, and doing it the "right" way is actually more scope then the project warrants? Or any number of other reasons.

    We use Time.timescale in our project to pause the game. It works fine. For our UI tweens when the game is paused, we just ignore the timescale. Is it "wrong"? Who cares? It works, it was easy, and it hasn't caused a single bug for us in four years and multiple games using the codebase. Are there better ways of handling pausing to make sure we have fine grained control? Of course ... but sometimes "better" is also "over designing". We have other features and not enough hours.

    With that said, I don't think Time.timeScale = 0 should stop Audio. If anything, it should be simple enough to create a track that checks the timescale and calls whatever is appropriate to pause the audio if needed. I'm pretty sure thats how we're handling pausing in our games ... when we open the settings it sets Time.timeScale = 0 to pause all animations, etc, and manually tells our AudioManager to pause all VO. Works well enough. If Time.timeScale = 0 did stop ALL sounds, it would wreak havoc on our audio.
     
  20. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    6,609
    azzpit5 likes this.
  21. Menion-Leah

    Menion-Leah

    Joined:
    Nov 5, 2014
    Posts:
    189
    Is there any update on this matter?

    I'm working with Timeline now (Unity 2019.4.16f1) and I can confirm that disabling the AudioListener component, or setting AudioListener.pause = true has absolutely no effect on Timeline Audio Tracks (whether they're bound to an AudioSource or not).
     
  22. seant_unity

    seant_unity

    Unity Technologies

    Joined:
    Aug 25, 2015
    Posts:
    1,516
    In 2021.1, audio in timeline properly follows Time.timeScale, including pausing where there is a timeScale of 0 provided the playableDirector follows gameTime. When using the DSP clock, timeline ignores the time scale.

    Pausing the audioListener still is not considered on Timeline audio, as far as I know. I'm not sure if we would change the behaviour when there is no audio source applied, as that implies flat 2D audio.
     
  23. Menion-Leah

    Menion-Leah

    Joined:
    Nov 5, 2014
    Posts:
    189
    Thanks for your answer, @seant_unity

    Unfortunately, it's still not clear how to deal with audio muting on Timeline: I'm in a situation in which I need to fast-forward a portion of the simulation (including Timeline), and I'm not able to avoid sounds being played.
    The only solution that comes to my mind would require muting Timeline audio tracks from script, but this would imply rebuilding the PlayableGraph twice and I would prefer to avoid that.

    Any suggestion?
     
    DeadCastles likes this.
  24. MUGIK

    MUGIK

    Joined:
    Jul 2, 2015
    Posts:
    481
    Unfortunately, looks like there is no other way rather than to do this:
    Code (CSharp):
    1. // on pause
    2. foreach (var playableDirector in FindObjectsOfType<PlayableDirector>())
    3. {
    4.     playableDirector.Pause();
    5. }
    6.  
    7. // on unpause
    8. foreach (var playableDirector in FindObjectsOfType<PlayableDirector>())
    9. {
    10.     playableDirector.Play();
    11. }
     
  25. Volkerku

    Volkerku

    Joined:
    Nov 23, 2016
    Posts:
    114
    How can I lock Audio back to DSP time in timeline?
    I would like to slow down physics, but keep the sound playing at normal speed (voice over).
    Thx.