Pause Script?

Can someone explain why this isn’t working? In the Input, I have defined “Pause” with a positive button named escape. I’d rather not use Key.KeyCode and would like to use GetButtonDown for console controller purposes. Any help would be appreciated! :slight_smile:

private var isPaused : boolean = false;                  
function Update() {
    Pause();
}
function Pause () {                                                      
    if(Input.GetButtonDown("Pause") && !isPaused){                                            .
        Time.timeScale = 0.000001;                                                           
        isPaused = true;
    }
 
    if(Input.GetButtonDown("Pause") && isPaused){                                            .
        Time.timeScale = 1.0;                                                              
        isPaused = false;
    }
}

Is that timeScale so low it just can’t seem to register this action from Update?
Maybe try something a little larger and tweak down to desired?

function Update () {
    if (Input.GetButtonDown ("Pause")) { 
        Time.timeScale = (Time.timeScale == 0.01 ? 1 : 0.01);
    }
}

Also, have you looked at using a coroutine?

You could try something like this :

    function Update () {
    if(Input.GetKeyDown(KeyCode.Escape)){
            ispaused = !ispaused;
        }

        if(ispaused){
            Time.timeScale = 0;
        }
        else{
            Time.timeScale = 1;
       }
    }

What exactly is not working there? Meaning what is the exact behavior you see? Did you also try to put some Debug.Log() statements in there to get some debugging?

@Ian094 it’s rather bad practice to really set the timeScale to 0 as this could affect loads of other stuff. Instead like Cooper37 did a very low value is recommended instead.

I’ve never really encountered any problems with setting the timeScale to 0.

I’ve heard of there being issues but tbh I’ve never actually seen examples of it. Only thing I can think of is things that don’t rely on the timeScale like GUI but if nothing is moving that shouldn’t really matter either I wager.

http://answers.unity3d.com/questions/55010/how-would-physics-act-when-timetimescale-is-set-to.html

BTW to OP, all your answers are here:

http://answers.unity3d.com/questions/9878/freeze-game-using-timetimescale-0-wait-3-secs-and.html

Literally what everyone suggested is here.

I just tested this code on a button in my game, worked perfectly:

Time.timeScale = (Time.timeScale == 0 ? 1 : 0);

@Ian094 Time.timeScale = 0 makes cloth go crazy. lol
@crag That didn’t exactly work…

After using Debug.Log, it turned out that the game paused, then immediately unpaused the game, making it seemed like nothing happened. I needed to add else if :stuck_out_tongue: Sometimes being a bit more specific in code helps:

function Update() {
    Pause();
}
function Pause() {                                                      
    if(Input.GetButtonDown("Pause") && !isPaused){                                           
        Time.timeScale = 0.0000001;                                                           
        isPaused = true;
        Debug.Log("Paused!");
    }

    else if(Input.GetButtonDown("Pause") && isPaused){                                           
        Time.timeScale = 1.0;                                                               
        isPaused = false;
        Debug.Log("UnPaused");
    }
}

I might be asking for much, but I ran into another problem with this. @Troas I learned a lot from the link and will be a bit more careful of what is using time so they can be paused or not. My player runs on a custom platforming controller script and when I pause the game, he stops in mid air when jumping because the gravity is controlled by Time:

moveDirection.y -= gravity * Time.deltaTime * 2;

However, the rotation is still applied ONLY when I’m using my game controller, not when I use my keys. Weird…

forward = Camera.main.transform.TransformDirection(Vector3.forward);                       
        forward.y = 0;
        forward = forward.normalized;
        right = Vector3(forward.z,0,-forward.x);
        h = Input.GetAxis("Horizontal");                                                           
        v = Input.GetAxis("Vertical");                                                               
      
      
        var XZmoveDirection:Vector3 = (h * right + v * forward); 
        XZmoveDirection *= moveSpeed;                                                               
        moveDirection.x = XZmoveDirection.x;
        moveDirection.z = XZmoveDirection.z;

if(XZmoveDirection != Vector3.zero){                                                         
        var rotation0 = transform.rotation;                                                         
        rotation0.SetLookRotation(new Vector3(moveDirection.x,0,moveDirection.z) * rotateSpeed);   
        transform.localRotation = rotation0;   
    }
  
    controller.Move(moveDirection * Time.deltaTime);

And finally, I have this in my Health Script, when I pause, the player respawns to the spawnpoint. Any clue about that?

function FixedUpdate(){
//Debug.Log(healthPercent);
    if(healthPercent > 100){
        healthPercent = 100;
    }
    if(healthPercent <= 0){
        healthPercent = 100;
        if(reSpawn){
            transform.position = spawnPoint.position;
        }
    }
}

Thanks for the help so far and in advance! :slight_smile:

I think your rotation doesn’t stop because it isn’t based using “Time.Deltatime”. All you do is a simple rotation transform that is updated once per frame, not once per second. Use Time.Deltatime right after rotateSpeed like this.

rotation0.SetLookRotation(new Vector3(moveDirection.x,0,moveDirection.z) * rotateSpeed * Time.Deltatime);

See if that solves it.

Remember just because time is paused doesn’t mean your screen isn’t updating frames. It is and technically the program is still running, only that commands based on the time step are halted. If you do not explicitly tell it a transform needs to use the time step it will still update it as if it were a new frame unaffected by time.

At least, that’s what it sounds like is happening to me.

You wouldn’t want to stop updating frames anyway as that would make it impossible to make a pause menu that will do anything more than show a giant “Pause” in-front of you.

Makes since! That solves it perfectly! My pause screen is a bit transparent, so I wouldn’t want anything weird happening in the background while paused.

I learned a lot in this. For years I’ve been wondering why my rotated object don’t pause with everything else because I was using: transform.Rotate(Vector3.up * rotateSpeed);
This is better efficient!

transform.Rotate(Vector3.up * Time.deltaTime * rotateSpeed);

I still don’t quite know what’s going on with my health thing, but I guess I’ll just change it accordingly. Thanks for all the help! :slight_smile:

If you don’t want the cloth going crazy then all you really have to do is disable the cloth when paused.gameObject.GetComponent(InteractiveCloth).enabled = false;

if(healthPercent <= 0){
        healthPercent = 100;
        if(reSpawn){
            transform.position = spawnPoint.position;

This keeps telling the computer that if health is 0 or less than 0 to basically do the respawn and set health percent to 100%. I’m not sure why you nested a respawn in a health percent check but this should fix that.

if(reSpawn){
            transform.position = spawnPoint.position;
}else{
            transform.position = transform; //transform.position is equal to this objects' current transform (includes pos, rot, scale).
}

You’ll have to define what the conditions for “respawn” are. I’d just do a simple check on if health is 0. Or the code below.

One more fix. Set healthpercent to 0 if it is 0 or falls below 0… not 100.

if(healthPercent <= 0){
        healthPercent = 0;
        reSpawn = true;
}

I guess I needed to be more specific in my code then. It worked perfectly, thank you so much for your help! :slight_smile:

No worries bro, just work on recognizing logic for methods that use if statements and if statements in general.

A good practice project is to make a Grade calculator that displays a grade letter and percentage and color codes it based on the grade received.

I’ll actually take up on that challenge

This is something you should NOT do…
You only have to change the value once, not every frame…

Would you do that by creating a separate script creating a Pause() method, calling it in the Start() and then create an if that grabs that script when you press the pause button?

There are plenty of ways to do this, I’m not sure what you’re suggesting, since the sentence structure doesn’t make much sense. Create what exactly?

Sorry kinda typed that out fast… basically was saying:

  • Make new MainMenu Class
  • Make a Pause() Method in this Class that sets time to 0
  • Call Pause method in Start() of this Class
  • Call the MainMenu Class if a certain button is pressed

That way it doesn’t update every frame for the time to be 0, since you said that was a bad idea.

Sure, that works.