Search Unity

  1. Calling all beginners! Join the FPS Beginners Mods Challenge until December 13.
    Dismiss Notice
  2. It's Cyber Week at the Asset Store!
    Dismiss Notice

Game Pause (time.timescale Vs Bunch Of Line Of Code And References)

Discussion in '2D' started by OGNE, Apr 15, 2019.

  1. OGNE

    OGNE

    Joined:
    Jun 9, 2017
    Posts:
    8
    Hey guys I just wanted to ask a quick question that I can pause a non physics based 2D endless game.
    Right now I am doing it with many lines of code and 3-4 references.
    I recently figured out that the same can be done with time.timescale = 0.
    So From a perspective of good coding practice "what should I do"?
    Any help would be appreciated.
     
  2. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    14,887
    My preferences tend to be towards a single object responsible for determining if the game is paused and a static boolean variable that every script can check.

    Code (csharp):
    1. if (PauseManager.isPaused)
    2. {
    3.     // do stuff
    4. }
     
    Last edited: Apr 15, 2019
    OGNE likes this.
  3. jeffreyschoch

    jeffreyschoch

    Joined:
    Jan 21, 2015
    Posts:
    2,642
    Using Time.timeScale is fine, and that will also allow you to do slow motion. It can all work seamlessly if your moving objects are using Time.deltaTime in their speed calculation.

    If you have objects that should never pause or slow down then use Time.unscaledDeltaTime.

    for example if your object had this update, it would move right at 10 units per second, and if Time.timeScale was 0.5, it would move 5 units per second. If Time.timeScale was 0, it would stop.
    Code (CSharp):
    1. private void Update()
    2. {
    3.     float speed = 10;
    4.     Vector3 direction = Vector3.right;
    5.     Vector3 positionOffset = direction * speed * Time.deltaTime;
    6.  
    7.     transform.position += positionOffset;
    8. }
    As long as you scale the appropriate values by Time.deltaTime, it should all just work.
     
    Rockaso and OGNE like this.
  4. RidgeWare

    RidgeWare

    Joined:
    Apr 12, 2018
    Posts:
    65
    I guess if there's no need for slowmotion below the standard speed, then it's probably better to have a simple IsGamePaused if statement in each moving object's Update instead of playing around with TimeScale.

    Otherwise they'll all still be firing off new transform positions every frame (along with whatever code surrounds it). Probably won't matter much, but I'm not sure if it's the most optimal/efficient way to go about things.
     
  5. jeffreyschoch

    jeffreyschoch

    Joined:
    Jan 21, 2015
    Posts:
    2,642
    Don't make the mistake of premature optimization. The scale of the performance gain you're speaking of is pretty much negligible. The "right way" is to do things in the cleanest/easiest way you can, in the way that makes the most sense to you, and only if performance becomes an issue should you worry about it. You're not making an enterprise tool or anything, and you don't have tens of thousands of objects on screen, so I don't think you'll have any issue either way.

    In any case, to accomplish the same behavior you could have a property:
    Code (CSharp):
    1. public bool IsPaused
    2. {
    3.     get { return Time.timeScale == 0; }
    4. }
    and then do:
    Code (CSharp):
    1. private void Update()
    2. {
    3.     if(IsPaused)
    4.     {
    5.         return;
    6.     }
    7. }
    I believe if Time.timeScale is 0, FixedUpdate will automatically stop running, but since you're not using physics that doesn't matter as much.

    If you're going to go that route, I would advise you abstract that behavior somehow, so you just have to implement an IPausable interface or inherit from a base object that supports that, maybe a component that can handle it? You definitely don't want to be rewriting that if statement into every single object that needs to pause.
     
    Last edited: Apr 16, 2019
    OGNE likes this.
  6. OGNE

    OGNE

    Joined:
    Jun 9, 2017
    Posts:
    8
    Hey Guys thanks for the help. Now I get it.
     
    jeffreyschoch likes this.