Search Unity

items speed up after unpausing

Discussion in 'Scripting' started by smokingbunny, Dec 19, 2014.

  1. smokingbunny

    smokingbunny

    Joined:
    Jul 24, 2014
    Posts:
    98
    hello all,

    this is a little bit strange. it makes sense in a way. BUT.
    i have a pause screen, happy with it, it passes and unpauses. but when i unpause after some time has passed ALL of the enemies are super fast.

    im gathering that this has to do with Time.timeScale. but i had put that to 0. when pausing, but really I'm a little bit unsure

    here is my current code that i have done. it works all fine just this speeding un after unpausing
    Code (csharp):
    1.  
    2.  
    3. private bool paused;
    4. private float TS;
    5.  
    6. void Start(){
    7.    TS = Time.timeScale;
    8. }
    9.  
    10. void Update(){
    11.    if(Input.GetKeyDown("esc") ){
    12.       paused = false;
    13.       pauseScreen.enabled = false;
    14.    } else {
    15.       paused = true;
    16.       pauseScreen.enabled = true;
    17.    }
    18.    if(paused){
    19.       if(Time.timeScale > 0.0f){
    20.          Time.timeScale = 0.0f;
    21.       }
    22.    } else {
    23.       if(Time.timeScale < TS){
    24.          Time.timeScale = TS;
    25.       }
    26.    }
    27. }
    28.  
    ill look more into it. just strange but understandable
    any pointers would be great

    thanks
     
    Last edited: Dec 19, 2014
  2. BenZed

    BenZed

    Joined:
    May 29, 2014
    Posts:
    524
    While your code is functionally tight (in the sense due to your conditional checks, you're technically not assigning timeScale every frame), I would refactor it accordingly for readability:

    Code (CSharp):
    1.     private bool paused = false;
    2.  
    3.     //Unless you want to have some kind of fancy time bending effects, I'd leave unpaused speed as a constant, 1f == realtime
    4.     private const float UnpausedSpeed = 1f;
    5.  
    6.  
    7.     void Update () {
    8.         if(Input.GetKeyDown("esc") ){
    9.  
    10.             paused = !paused;
    11.  
    12.             Time.timeScale = (paused) ? 0f : UnpausedSpeed;
    13.  
    14.             pauseScreen.enabled = (paused);      
    15.         }
    16.     }
    That being said, it DOES look to me like the speeds should be accurate. Try either this code, or logging what TS is computed as. I'm imagining the problem is originating from somewhere other than this code.
     
  3. smokingbunny

    smokingbunny

    Joined:
    Jul 24, 2014
    Posts:
    98
    hmmm. it did work. so thanks for that. but still getting this speed up when unpausing.
    looking at my code for anything that contains speed, there is nothing apart from the enemy which is not related to Time.timeScale, but rather a box2d rigidbody2d.AddForce for its movement

    other than that, i can't see anything. ill dig deeper

    but thanks again. yours does look much cleaner [special officer doofy] ;)
     
  4. smokingbunny

    smokingbunny

    Joined:
    Jul 24, 2014
    Posts:
    98
    oooh. did think that i do actually have InvokeReapeating, for creating enemies at certain times set with InvokeRepeat
     
  5. Nubz

    Nubz

    Joined:
    Sep 22, 2012
    Posts:
    553
    Code (CSharp):
    1. if(Time.timeScale > 0.0f){
    2.          Time.timeScale = 0.0f;
    How is it working at all when you told it to always be zero?

    I suppose you meant < 0 not >?
     
  6. smokingbunny

    smokingbunny

    Joined:
    Jul 24, 2014
    Posts:
    98
    weridly it works like that. i worked from things i found or could find. but this was a hack of something i found.

    did try another way. much simpler that i have made, but still the process is the same. it speeds up all the other items. also if any box2d items are flying away from you if they miss, once you press pause, leave for a few seconds and then unpause, they have changed direction right towards you. obviously they are aimed to go to you, but the change in direction is interesting

    Code (csharp):
    1.  
    2. if(Input.GetKeyDown("esc")){
    3.    paused = false;
    4.    Time.timeScale = 1.0f;
    5.    pauseScreen.enabled = false;
    6. } else {
    7.    paused = true;
    8.    Time.timeScale = 0.0f;
    9.    pauseScreen.enabled = true;
    10. }
    11.  
     
  7. smokingbunny

    smokingbunny

    Joined:
    Jul 24, 2014
    Posts:
    98
    no im just stumped by this. i cant figure it out at all.

    no matter what i try with pausing and unpausing, the enemies continue to gain a momentum whilst paused, so when i finally unease, they are super fast.
    i just don't know what to do to be honest. ive actually tried 6 different ways, actually more 9. but i cant figure it out

    if anyone can shed some light id be happy. but still thanks to the people who have helped up to now

    cheers