Search Unity

Resolved New Input System - Timescale Set To 0 - Input Controls don't work properly

Discussion in 'Input System' started by DonPuno, Mar 19, 2021.

  1. DonPuno

    DonPuno

    Joined:
    Dec 22, 2020
    Posts:
    57
    Background Information:
    Unity Build: 2019.4.16f1 | LTS
    Input System: Version 1.0.2 | Release January 21, 2021


    Hello everyone,

    Problem: When the Pause Menu opens, and the time.timescale = 0, the menu element behaviour and the input controls are also scaled by the timescale to 0. I tested this by setting the value just to 0.1, and the time which had to pass was 10 times longer than before for an event to happen, e.g. button click answer or key pressed answer, in this case ESC.

    I have the assumption, that the events that are talking to each other via the two classes mentioned below are somehow effected by the time.timescale reduction. But I have no clue, how to fix that in an elegant way. I already understood, that the execution of the Update() Method is not effected by the timecale, which I could confirm by placing a decent debug.Log() statement into one of them.

    Classes:
    MenuController : MonoBehaviour
    InputController : MonoBehaviour


    MenuController.cs
    Code (CSharp):
    1.    
    2. public class MenuController : MonoBehaviour
    3. {
    4.  
    5.     public GameObject pauseMenuObject;
    6.  
    7.     public void OnPauseMenuStart(bool isPressed_Pause)
    8.     {
    9.         if (isPressed_Pause)
    10.         {
    11.             if (pauseMenuObject.activeSelf)
    12.             {
    13.                 pauseMenuObject.SetActive(false);
    14.                 NormalizeGameSpeed();
    15.             }
    16.             else
    17.             {
    18.                 pauseMenuObject.SetActive(true);
    19.                 PauseGame();
    20.             }
    21.  
    22.         }
    23.     }
    24.  
    25.  
    26.     public void PauseGame()
    27.     {
    28.         Time.timeScale = 0.1f;
    29.     }
    30.  
    31.     public void NormalizeGameSpeed()
    32.     {
    33.         Time.timeScale = 1.0f;
    34.     }
    35.  
    36. }

    InputController.cs
    Code (CSharp):
    1.  
    2.  
    3. [Serializable]
    4. public class OpenPauseMenuEvent : UnityEvent<bool> { }
    5. public class InputController : MonoBehaviour
    6. {
    7.     public OpenPauseMenuEvent openPauseMenuEvent;
    8.  
    9.     private void Awake()
    10.     {
    11.            // Instantiate the Player1 controller class.
    12.            controllerClassPlayer1 = new Player1_();
    13.     }
    14.  
    15.     private void OnEnable()
    16.     {
    17.         controllerClassPlayer1.Gameplay.PauseGame.started += OnOpenPauseMenu;
    18.         controllerClassPlayer1.Gameplay.PauseGame.canceled += OnOpenPauseMenu;
    19.  
    20.     }
    21.  
    22.     private void OnOpenPauseMenu(InputAction.CallbackContext context)
    23.     {
    24.         if (context.started)
    25.         {
    26.             openPauseMenuEvent.Invoke(context.started);        
    27.          
    28.         }
    29.     }
    30. }
     
    Kyu_bo likes this.
  2. DonPuno

    DonPuno

    Joined:
    Dec 22, 2020
    Posts:
    57
    Last edited: May 13, 2021
  3. dhymers

    dhymers

    Joined:
    Nov 26, 2021
    Posts:
    1
    Holy crap this saved me, I can't believe nobody touches on this in their "how to pause unity the right way with timescale" tutorials. Nuts.
     
    mcfly_44 and DonPuno like this.