Search Unity

Time.timeScale isn't full freezing my animations

Discussion in 'Animation' started by XIIl_, Oct 1, 2021.

  1. XIIl_

    XIIl_

    Joined:
    Jul 21, 2021
    Posts:
    23
    Hi!

    I'm working in the Pause Menu with Time.timeScale and the only problem is been that player is not fully freezing. When i pause, he stop, the animator show that he is stoped, but if i use some moviment key, he "try" the transition for the next animation, but get stuck. Here a video to be easier to understand.



    The input system still responding. In my searchs, i got that can be something related to my Updade(), FixedUpdate(), Time.deltaTime or Time.fixedDeltaTime, things like this.

    The relevant part of my player script is here:

    Code (CSharp):
    1. void Update()
    2.     {
    3.         if (state == State.Idle)
    4.         {
    5.             Movement();
    6.             Actions();
    7.         }
    8.  
    9.         if (state == State.Rolling)
    10.         {
    11.             Roll();
    12.         }
    13.     }
    14.  
    15. void FixedUpdate()
    16. {
    17.         if (state == State.Idle && movement != Vector2.zero)
    18.         {
    19.             rigidb.MovePosition(rigidb.position + movement * Time.fixedDeltaTime);
    20.         }
    21. }
    22.  
    23. void Movement()
    24. {
    25.         movement.x = Input.GetAxisRaw("Horizontal") * moveSpeedHorizontal;
    26.         movement.y = Input.GetAxisRaw("Vertical") * moveSpeedVertical;
    27.         animator.SetFloat("Speed", movement.sqrMagnitude);
    28.         SetFlip();
    29. }
    The Update is working with states and actions and the FixedUpdate is only for movement inputs.

    If someone have a light, please!

    Thank you!
     
  2. Unrighteouss

    Unrighteouss

    Joined:
    Apr 24, 2018
    Posts:
    599
    Hey,

    I'm no expert on this, but Unity will still look at your inputs and execute code when Time.timeScale is 0, you wouldn't be able to unpause the game otherwise.

    I found this useful page that explains a ton of stuff about pausing in Unity: https://gamedevbeginner.com/the-right-way-to-pause-the-game-in-unity/#what_gets_paused

    According to that page, Update() will continue to be called, except for anything in Update() that relies on time based measurements. So the game is still reading your movement inputs and executing code based off that.

    If you don't want anything in your Update() method to run at all while the game paused, you could add something like this to the beginning of the method:
    Code (CSharp):
    1.     void Update()
    2.     {
    3.         if (Time.timeScale < 0.1f) { return; }
    4.  
    5.         if (state == State.Idle)
    6.         {
    7.             Movement();
    8.             Actions();
    9.         }
    10.  
    11.         if (state == State.Rolling)
    12.         {
    13.             Roll();
    14.         }
    15.     }
    This will return out of the method if Time.timeScale is less than 0.1f, effectively ignoring all of your inputs.

    The above is just an example, the page I linked covers this in depth, so give that a look if you'd like to learn more. Use CRTL + F to search the page for "How to prevent control input when the game is paused" - this section has a lot of relevant information.
     
    XIIl_ likes this.
  3. XIIl_

    XIIl_

    Joined:
    Jul 21, 2021
    Posts:
    23
    Really thank you!

    I was trying something like this, put my inputs pass by a bool gameIsPaused verification, but i will read this article to find a best way with lowest performance impact, including this your suggention.

    Thank you again!
     
    Unrighteouss likes this.