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:
    56
    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. GameDevCouple_I

    GameDevCouple_I

    Joined:
    Oct 5, 2013
    Posts:
    1,762
    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

    Joined:
    Jun 12, 2013
    Posts:
    3,912
    How would you pause things you don't really have control over, such as Physics, Animation, Particles and so on...?
     
  4. GameDevCouple_I

    GameDevCouple_I

    Joined:
    Oct 5, 2013
    Posts:
    1,762
    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

    Joined:
    Jun 12, 2013
    Posts:
    3,912

    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. GameDevCouple_I

    GameDevCouple_I

    Joined:
    Oct 5, 2013
    Posts:
    1,762
    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. GameDevCouple_I

    GameDevCouple_I

    Joined:
    Oct 5, 2013
    Posts:
    1,762
    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

    Joined:
    Jun 12, 2013
    Posts:
    3,912
    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.
     
  9. crdmrn

    crdmrn

    Joined:
    Dec 24, 2013
    Posts:
    56
    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:
    919
    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

    Joined:
    Jun 12, 2013
    Posts:
    3,912
    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
     
  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:
    56
    @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.
     
  14. Peter77

    Peter77

    Joined:
    Jun 12, 2013
    Posts:
    3,912
    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:
    919
    @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:
    919
    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:
    28
    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:
    397
    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

    Joined:
    Jun 12, 2013
    Posts:
    3,912