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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

How to pause the game but not the menu

Discussion in 'Scripting' started by KlausKlaudius, Sep 18, 2015.

  1. KlausKlaudius

    KlausKlaudius

    Joined:
    Jul 22, 2015
    Posts:
    8
    I'm making a game with a pause button. When I pause the game (Time.timescale=0) a GUI menu appears. The problem is that my menu buttons use animations, and animations don't work if the timescale is stopped.

    Any idea to solve this?
     
  2. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,534
    Don't use Time.timeScale to pause, at least not directly.

    Many people write a wrapper script, such as:
    Code (csharp):
    1. public static class GameTime {
    2.     public static bool isPaused = false;
    3.     public static float deltaTime { get { return isPaused ? 0 : Time.deltaTime; } }
    4. }
    This is a very simplified version of a wrapper just to demonstrate the concept.

    In your game scripts, use GameTime.deltaTime instead of Time.deltaTime. In your Update methods, return immediately is GameTime.isPaused is true. For example:
    Code (csharp):
    1. void Update() {
    2.     if (GameTime.isPaused) return;
    3.     controller.Move(moveDirection * GameTime.deltaTime);
    4. }
     
  3. justoon

    justoon

    Joined:
    Nov 7, 2013
    Posts:
    9
    You can also set your Animator Controller > Update Mode to "Unscaled" which will allow your animations to play even if the game is paused / Time scale set to 0.
     
  4. KlausKlaudius

    KlausKlaudius

    Joined:
    Jul 22, 2015
    Posts:
    8
    That was it! Now it works properly. Thanks a lot for your help, justoon
     
  5. KlausKlaudius

    KlausKlaudius

    Joined:
    Jul 22, 2015
    Posts:
    8
    Thank you very much, TonyLi. Anyway, the answer of justoon is more useful for my purposes.

    By the way, I'd like to ask you about the time wrapper class you suggest, if you don't mind: What if your game uses physics? You can stop your own kinematic movements, but the physics engine wouldn't stop, would it?
     
  6. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,534
    Agreed. That's the better way to do it for GUIs.

    Nope. You'd have to handle that yourself, or use something like Chronos. It's nice that Unity added unscaled animation to make this a cleaner process.
     
  7. KlausKlaudius

    KlausKlaudius

    Joined:
    Jul 22, 2015
    Posts:
    8
    Thanks for your answer, TonyLi
     
  8. infinitypbr

    infinitypbr

    Joined:
    Nov 28, 2012
    Posts:
    3,149
    This seems like a good solution, I'll give it a try!
     
  9. craig4android

    craig4android

    Joined:
    May 8, 2019
    Posts:
    124
    don't this is one of the worst solution, at least try to disable the monobehaviour instead of add a check in the update loop