Search Unity

Can I have a Pause Menu with Time.timeScale?

Discussion in 'Scripting' started by ExbowFTW, Aug 25, 2015.

  1. ExbowFTW

    ExbowFTW

    Joined:
    May 2, 2015
    Posts:
    281
    When the user pushes the pause button, I do "time.timeScale = 0f;" However, by doing this, can the game still pop up a GUI pause menu and tell when the user presses a button on the Pause Button?
    If it can't, is there any other way to do a pause button? (I'm making the game for iOS).

    Thanks!
     
  2. adnan-e94

    adnan-e94

    Joined:
    Dec 17, 2012
    Posts:
    70
    You can have a lot of issues animating your pause menu this way.

    Not knowing if there is a better way, or whether this is good or bad, to achieve this without much hassle i added a class called TimeLayers, like:
    Code (CSharp):
    1. public class TimeLayers {
    2.   public static float ActorsLayer = 1f;
    3.   public static float UILayer = 1f;
    4.   ...
    5. }
    And assigned a layer to any kind of motion in my code (which already depended on Time.deltaTime in some way), for example moving an object wouldnt be
    Code (CSharp):
    1.  
    2.    box.transform.position += Vector3.right * speed * Time.deltaTime;
    3.  
    but


    Code (CSharp):
    1.  
    2.    box.transform.position += Vector3.right * speed * Time.deltaTime * TimeLayers.CustomLayer;
    3.  
    and if I wanted to stop all objects that depend on CustomLayer, i just set it to 0 (as for animations, I updated my animators anim speed with the changed value as well via each objects animation-related script)
     
    Last edited: Aug 25, 2015
    lordofduct likes this.
  3. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,537
    Nice,

    I have a similar thing, but I wanted something with an object identity to go along with it.

    This way I could generalize code, and pass in the 'time' object that I desired for which sort of deltatime to use:

    The interfaces for the objects that represent the various types of time:
    https://github.com/lordofduct/spacepuppy-unity-framework/blob/master/SpacepuppyBase/ITimeSupplier.cs

    The class for custom time suppliers (just like your 'layers'):
    https://github.com/lordofduct/space...b/master/SpacepuppyBase/CustomTimeSupplier.cs

    And a static access point for all of them, including the normal built in time:
    https://github.com/lordofduct/spacepuppy-unity-framework/blob/master/SpacepuppyBase/SPTime.cs

    So this:

    Code (csharp):
    1.  
    2. public class SomeScript : MonoBehaviour
    3. {
    4.  
    5.    public Transform box;
    6.    public float speed = 1f;
    7.  
    8.    void Update()
    9.    {
    10.      box.position += Vector3.right * speed * Time.deltaTime;
    11.    }
    12.  
    13. }
    14.  
    might become:

    Code (csharp):
    1.  
    2. public class SomeScript : MonoBehaviour
    3. {
    4.  
    5.    public Transform box;
    6.    public float speed = 1f;
    7.  
    8.    private ITimeSupplier _time;
    9.  
    10.    void Start()
    11.    {
    12.      _time = SPTime.Custom("CustomLayer"); //cache for fast access
    13.    }
    14.  
    15.    void Update()
    16.    {
    17.      box.position += Vector3.right * speed * _time.Delta;
    18.    }
    19.  
    20. }
    21.  
    This means that with like my tweener, I can reference the time you desire, and update at that rate.

    Code (csharp):
    1.  
    2. SPTween.Tween(box).To("position", someEndPosition, someDuration).Use(SPTime.Custom("CustomLayer")).Play();
    3.  
     
    Last edited: Aug 25, 2015
  4. ExbowFTW

    ExbowFTW

    Joined:
    May 2, 2015
    Posts:
    281
    The thing is - my game relies on a Constant Force (which is attached to the player). How would I be able to stop the constant force?
     
  5. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,537
    So, things like Physics and what not are impacted by the Time.timeScale directly.

    Keep 'gameplay' on that. That being: 'Time.time' and 'Time.deltaTime' and anything dealing with Rigidbody.

    When you slow the 'Time.timeScale' to 0, all those things slow down, but 'Update' and Coroutines still fire at the framerate. This is what 'Time.realTime' and 'Time.realDeltaTime' are for. They are uneffected by 'Time.timeScale'.

    This is why in my code my 'custom' times are based around the real time. You can then set those custom layers with the real time, and during the update code for say menus and what not, you use the custom layers.

    That way you never have to worry about the impact of the timescale on your actual game physics logic.
     
  6. dustedge

    dustedge

    Joined:
    Oct 29, 2012
    Posts:
    4
    Basically you can't use timescale to pause. Everything will be slowed down by timescale. Its better to have a global variable, and inside update() function check this variable and then execute function or not.
    For game physics, you need to delegate a event to this variable, when pause changed to true, set the objects velocity to 0. When pause changed to false, set objects velocity to the original state
     
  7. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,537
    Not true, I specifically just stated a way to use timescale for pause.

    It's exactly how I do pause in the game I'm currently working on, and our previous Unity game:
    http://triatami.itch.io/how-now-sea-cow

    Put in simplest terms without my framework.

    1) use Time.deltaTime and Time.time for gameplay and physics
    2) use Time.realTime and Time.realDeltaTime for menus and other things that should not be effected by pause
     
    Chris-Trueman likes this.
  8. ExbowFTW

    ExbowFTW

    Joined:
    May 2, 2015
    Posts:
    281
    So what you're saying is this: If I use Time.timeScale = 0f, all the gameplay/physics will be stopped, but all GUI Components will still be able to:
    1. Receive inputs from the user (e.g. a click on a button)
    2. Be enabled/disabled
    ?

    Thanks!
     
  9. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,537
    Yes, the game still calls Update at the regular framerate.

    Time.timeScale just adjusts the rate of simulation.

    So as long as you use Time.realDeltaTime for any simulations in your menus, that any animations/tweens/motion and the sort there will work just fine.
     
  10. ExbowFTW

    ExbowFTW

    Joined:
    May 2, 2015
    Posts:
    281
    So I do Time.timeScale = 1.0f, and leave Time.realDeltaTime at 1.0f, correct? (to stop gameplay but still allow GUI Menus to do stuff)
     
  11. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,537
    Time.realDeltaTime is readonly, and it's the amount of time that passed since last frame... it should seldom ever be 1f (if it were, your game is running at 1 frame per second).

    You READ Time.realDeltaTime in places where you'll need a delta time. Such as if you're animate a button to move across the screen in an eased manner in the 'Update' function or during a Coroutine.
     
  12. ExbowFTW

    ExbowFTW

    Joined:
    May 2, 2015
    Posts:
    281
    Okay I think I understand what you are trying to say. If you were to do Time.realDeltaTime = 60.0f;, would the frames go up to 60 FPS?
     
  13. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,537
    First off, sorry, it's not Time.realDeltaTime (sorry, mistyping this whole time, I'm so used to using my SPTime wrapper that I'm used to typing real).

    It's Time.unscaledDeltaTime.

    And it's read only.

    You can't set it!

    And if the value were to be read as 60f, that would be 1 frame per minute. The value that comes back from 'deltaTime' and 'unscaledDeltaTime' is the duration of time between frames. It's just that unscaled version is uneffected by Time.timeScale.

    The time between frames is super small. For instance most games are expected to run at 60fps, they look good there. That's 1/60th of a second between frames. Or 0.016666666666....
     
  14. ExbowFTW

    ExbowFTW

    Joined:
    May 2, 2015
    Posts:
    281
    Ohhhhh I get what you are saying :)
    Okay, I get that part now. Thanks so much!