Search Unity

Problem with pausing game

Discussion in 'Scripting' started by Frostraver, Oct 26, 2012.

  1. Frostraver

    Frostraver

    Joined:
    Oct 26, 2012
    Posts:
    25
    Hi everyone,

    I've been puzzled for some time because I can't seem to pause my game.

    I've tried using Time.timeScale = 0 in a script but it doesn't pause anything.
    It's called in OnGUI() (maybe that's the problem) when a Raycast hits a certain object.

    Can somebody help me with this?

    Thanks in advance
     
  2. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    What in you game is not paused?
    If you some scripted interaction in your game and the movement uses Time.deltaTime, the game should be paused. If you are not using Time.deltaTime, you will run into issues anyway and you should update your scripts accordingly. The cause is, that the game is not frame rate independent without Time.deltaTime, meaning it runs slower on slow computers and faster on fast computers.
     
  3. hima

    hima

    Joined:
    Oct 1, 2010
    Posts:
    183
    To add more on what Dantus said, there is no way to pause a game in Unity like how you just press a pause button in the editor. You have to design what does a pause state mean to your game.

    Time.timeScale = 0 is popular because it stop all the basic physics in the game. This won't stop player from accessing or interacting with the game GUI. You must script this yourself, with some kind of flag or state machine. Something to let the game objects know that the game is in a pause state and it shouldn't interact with the player.
     
  4. Frostraver

    Frostraver

    Joined:
    Oct 26, 2012
    Posts:
    25
    I'm moving a gameObject in the update function but not with deltaTime I guess (except if Update is using deltaTime itself). So when I set the Time.TimeScale to 0 it keeps on moving but it should stop moving.
     
  5. hima

    hima

    Joined:
    Oct 1, 2010
    Posts:
    183
    If you aren't using Time.deltaTime, then you have to set some kind of flag so that it knows what not to do when the game is in a pause state.

    It can be as simple as
    Code (csharp):
    1.  
    2. void Update()
    3. {
    4.      if( !isPause)
    5.      {
    6.             UpdateMovement();
    7.      }
    8. }
    9.  
    10. void UpdateMovement()
    11. {
    12.         // Movement update related codes should be here
    13.         this.transform.Translate( 5, 0 , 0 );
    14. }
    15.  
     
  6. Frostraver

    Frostraver

    Joined:
    Oct 26, 2012
    Posts:
    25
    Ok thanks! I'll try that and let you know if it fixed everything! (It probably will)
     
  7. Frostraver

    Frostraver

    Joined:
    Oct 26, 2012
    Posts:
    25
    That fixed it :) Thanks for helping me out!
     
  8. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    Even if the code works, some time in the future you are going to run into issues. Think about multiplying the numbers you are using with Time.deltaTime. You have to heavily increase the numbers. As soon as this works, you can be sure, that you get the same result on about every machine, independent of how fast it is.

    Code (csharp):
    1.     void Update()
    2.     {
    3.          if( !isPause)
    4.          {
    5.                 UpdateMovement();
    6.          }
    7.     }
    8.      
    9.     void UpdateMovement()
    10.     {
    11.             // Movement update related codes should be here
    12.             this.transform.Translate( 300 * Time.deltaTime, 0 , 0 );
    13.     }