Search Unity

Pausing a game without settiing timescale to 0

Discussion in 'Scripting' started by AmazingRuss, Jul 24, 2010.

  1. AmazingRuss

    AmazingRuss

    Joined:
    May 25, 2008
    Posts:
    933
    My game uses a lot of physics, animation and coroutines, so the only way I have been able to figure out to pause it while the user is in a menu is to set timescale = 0. Unfortuantely this takes away my ability to use coroutines or animations for GUI effects.

    As far as I can tell, if I want timed things happening in the GUI, I'm going to have to work based on the system clock, which is going to be a bit rough. Does anybody know of a nicer way to go about all this?
     
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Instead of using 0 for the time scale, use something like .000001, and multiply any relevant Time.deltaTime calculations by Time.timeScale.

    --Eric
     
  3. AmazingRuss

    AmazingRuss

    Joined:
    May 25, 2008
    Posts:
    933
    Brilliant! Thanks.
     
  4. bumba

    bumba

    Joined:
    Oct 10, 2008
    Posts:
    358
    I have some problems with this.

    I set Time.timeScale = 0.000001;

    and after that i make something like this

    Invoke("continue", 25 * Time.deltaTime);

    The problem is that the script invokes the method not always at the same time. Its kind of random.
     
  5. tomvds

    tomvds

    Joined:
    Oct 10, 2008
    Posts:
    1,028
    Just set the time scale to 0 and use Time.realtimeSinceStartup (instead of Time.time) for animations and coroutines that should run during pause state.

    That's because you are using Time.deltaTime in your Invoke. You normally provide invoke with the amount of seconds it has to wait before calling the function. You multiply that number with the time that the last frame happened to take, which will be different every time.

    Simply remove Time.deltaTime, and probably also make 25 a lot smaller number.
     
  6. fnavarrensis

    fnavarrensis

    Joined:
    Aug 1, 2020
    Posts:
    9
    OK I just created an ID to congratulate you on this very simple but very smart solution. I was breaking my head with LeanTween as all animations would take about 2 seconds to pause the game (critical for a pause menu) so did your scale thing, set the scale to ms, and the LeanTween animation instead of 2 seconds, takes 200f. Worked like a charm!

    Thanks a lot good sir!!
     
  7. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,909
    It's not really a great solution. I'd imagine it would introduce some floating point precision problems. The better solution is to use e.g. WaitForSecondsRealtime in the coroutine. Not sure if/how that translates to any particular Tweening library, but I'd guess they provide options for using "realtime" or "unscaled time".