Pause in Unity with the new input system

Hi guys, I’m trying to set a pause menu with the new input system. I managed to set the input and functions, but the input is taken 2-3 times in the frame when I press the key/button. I don’t know why this happen, can anyone give me a hand please?
This is the code in the GameManager script:

public bool needPause;
public bool pressingPause;
public static bool isPaused = false;

private void Update()
{
   //Handle Play/Pause
   PausingGame();
}

// Play/Pause methods
void PausingGame()
    {
        if (needPause && pressingPause)
        {
            if (isPaused)
            {
                ResumeGame();
            }
            else
            {
                PauseGame();
            }
        }
    }

    public void PauseGame()
    {
        audioPlayer.PlayOpenPauseClip();
        audioPlayer.GetComponent<AudioSource>().volume = 0.1f;
        pausePanel.SetActive(true);
        scoreText.text = "You Scored :\n" + scoreKeeper.GetScore();
        InputSystem.settings.updateMode = InputSettings.UpdateMode.ProcessEventsInDynamicUpdate;
        Time.timeScale = 0f;
        isPaused = true;
    }

    public void ResumeGame()
    {
        audioPlayer.GetComponent<AudioSource>().volume = 0.3f;
        audioPlayer.PlayClosePauseClip();
        pausePanel.SetActive(false);
        InputSystem.settings.updateMode = InputSettings.UpdateMode.ProcessEventsInFixedUpdate;
        Time.timeScale = 1f;
        isPaused = false;
    }

This is the code in the PlayerController script:

    [HideInInspector] public bool pauseButtonPressed;
    GameManager gameManager;
    void Awake()
    {
       gameManager = FindObjectOfType<GameManager>();
    }
    private void OnPauseGame(InputValue value)
    {
        if(gameManager != null)
        {
            Debug.Log("Pause");
            gameManager.pressingPause = value.isPressed;
        }
    }

Thanks in advance and if you need more code or other elements just ask. :smiley:

First, you don’t need to check if it was paused in Update(). You could simply make PausingGame() public and call it from your PlayerController script.

private void OnPauseGame(InputValue value)
{
    if(gameManager != null)
    {
        Debug.Log("Pause");
        gameManager.PausingGame();
    }
}

This isn’t going to fix the problem, though, since it will still be called over multiple frames. The Unity Messages you are using don’t give you nice control because InputValue has been stripped down to only give you isPressed which is not ideal. The message is called for as long as the button is down and humans tend to be too slow to press the button for a single frame only. You could switch from messages to C# events because those are fired each time an action happens, and that includes pressing the button as well as releasing it. You could then check when it’s released before caring about the pause again.

If you change it to events, your scripts will change a bit. The PlayerController can wait for the event and call gameManager.PausingGame() like now, but then it can set a flag to indicate that the button is held down. You can then ignore more events until the button is released. It would be something like this

// PlayerController
private bool pauseIsPressed;

private void OnPauseGame(InputAction.CallbackContext value)
{
    if (value.performed && !pauseIsPressed)
    {
        pauseIsPressed = true;
        if (gameManager != null)
        {
            gameManager.PausingGame();
        }
    }
    if (value.canceled && pauseIsPressed)
    {
        pauseIsPressed = false;
    }
}

This should now only call gameManager.PausingGame() once if the button was pressed and released.

Note: I haven’t tested this. You would also need to change the Player Input component’s behaviour from Send Messages to Invoke C Sharp Events.

Well, THANK YOU VERY MUCH! :grin:

Now it works seamlessly. It has been quite difficult to change all the inputs to Invoke Events and I managed to do this only with “Invoke Unity Events” and not C# Events, but now it works, so it’ super fine to me!
Again, thanks a lot bixarrio!

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms