Forum begins after the advertisement:


[Part 9] Pause Menu when resume don’t take input anymore

Home Forums Video Game Tutorial Series Creating a Metroidvania in Unity [Part 9] Pause Menu when resume don’t take input anymore

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #16369
    pepecry
    Level 7
    Participant
    Helpful?
    Up
    0
    ::

    So i was making the pause menu, the quit to main menu and other function look good. But the thing is when I pause the game and click the resume button. I cannot move my character anymore, even attack or healing.

    Resume button assign
    <code>private void Update()
    {
        if(Input.GetKeyDown(KeyCode.P))
        {
            SaveData.Instance.savePlayerData();
        }
        if (Input.GetKeyDown(KeyCode.Escape) && !gameIsPause)
        {
            pauseMenu.fadeUIIn(fadeTime);
            Time.timeScale = 0;
            gameIsPause = true;
        }
    }
    
    public void unpauseGame()
    {
        Time.timeScale = 1;
        gameIsPause = false;
    
    }</code>

    I already put the if(GameManager.Instance.gameIsPause) return; in the enemy and player. Here the enemy

    <code>protected virtual void Update()
    {
    
        if(GameManager.Instance.gameIsPause) return;
    
        if (isRecoilling)
        {
            if (recoinTimer < recoilLength)
            {
                recoinTimer += Time.deltaTime;
            }
            else
            {
                isRecoilling = false ;
                recoinTimer = 0 ;
            }
        }
        else
        {
            UpdateEnemyStates();
        }
    
    }</code>

    and here is the player

    <code>private void Update()
    {
        if (GameManager.Instance.gameIsPause) return;
        if (playerStateList.incutscene) return;
    
        if(playerStateList.alive)
        {
            getInput();
            OpenMap();
            OpenInventory();
        }
        UpdateJump();
        RestoreTimeScale();
        if (playerStateList.dashing) return;
        FlashWhenInvi();
        if (playerStateList.alive){
    
            if (!iswallJumping)
            {
                Moving();
                Flip();
                Jump();
            }
        };
        Heal();
        CastSpell();
        if (playerStateList.healing) return;
    
        if (unlockWalljump)
        {
            WallSlide();
            WallJump();
        }
        if (unlockDash)
        {
            StartDash();
        }
        Attack();
    }</code>

    Is this error cause because of the potion of the if statement in the player script?

    #16414
    Chloe Lim
    Level 10
    Moderator
    Helpful?
    Up
    1
    ::

    Try calling the unpause function then the fade time, it might be possible that the fade time is calling delta time and it won’t be completed for the game to be unpaused try putting the fadeUIout within the unpause game function

    <code>    public void UnpauseGame()
        {
            Time.timeScale = 1;
    pauseMenu.FadeUIOut(fadeTime);
            gameIsPaused = false;
        }</code>

    then just call for unpausegame in the Onclick() for resume

    has upvoted this post.
    #16420
    pepecry
    Level 7
    Participant
    Helpful?
    Up
    0
    ::

    So i tried as you said. But it doesn’t solve the problem. When I click the resume. All the thing still can move like the boss but my character cannot get my input. I cannot attack or doing anything as the below record. Resume error And I also want to as you for another question. My boss when doing the dive attack, he only create 1 pillar instead of many like in the video, and the pillar when spawn is spawning in some very weird location. Also the pillar will one hit me because it deal multiple time damage to player in a very shot time. Could said in every frame of the animation. Can you help me fix this?

    #16446
    Terence
    Level 30
    Keymaster
    Helpful?
    Up
    0
    ::

    Can you show us the GetInputs() function in your script?

    Edit: Double click on the ArgumentNullException in your Console and show us the highlighted line as well.

    #16571
    A_DONUT
    Level 6
    Moderator
    Helpful?
    Up
    0
    ::

    Your issue likely arises because the Time.timeScale is being set back to 1 when resuming, but the character might still be affected by other factors, such as disabled inputs or mismanaged states.

    Here’s a step-by-step breakdown to fix it:


    1. Ensure Inputs Are Enabled After Unpause:

    When you pause and unpause, ensure that input is re-enabled. Check if any part of your code explicitly disables character control during the pause and forgets to re-enable it.

    Modify your unpauseGame() function to reset all critical states:

    csharp
    public void unpauseGame()
    {
        Time.timeScale = 1;
        gameIsPause = false;
    
        // Ensure player state and input are restored
        PlayerInputEnabled(true);  // A custom method to re-enable input
    }
    <code></code>

    Implement the PlayerInputEnabled method:

    csharp
    private void PlayerInputEnabled(bool enabled)
    {
        // This assumes you have an input system or component to manage player controls
        playerStateList.alive = enabled;  // Ensure alive state is active
        // Add more logic if you have an Input Manager or Controller script
    }
    <code></code>

    2. Check Animator and Rigidbody States:

    Sometimes, pausing affects the Animator or Rigidbody components, making them inactive.

    Ensure the Animator is still running:

    csharp
    animator.enabled = true;  // Ensure Animator is enabled when resuming
    <code></code>

    Ensure Rigidbody constraints are reset:

    csharp
    rbd2.simulated = true;  // Ensure Rigidbody is active after unpausing
    <code></code>

    3. Verify Input Handling Logic:

    Check if the getInput() method might be returning early after the pause.

    Add Debug Statements: Insert Debug.Log() in getInput() and other critical methods to verify they are being called after resuming:

    csharp
    void getInput()
    {
        Debug.Log("Checking player input...");
        // Your input handling code
    }
    <code></code>

    4. Confirm Pause/Unpause Flow:

    Make sure your gameIsPause flag is correctly toggled:

    • Pause Button Logic:
      csharp
      if (Input.GetKeyDown(KeyCode.Escape) && !gameIsPause)
      {
          PauseGame();  // Call a method to pause
      }
      else if (Input.GetKeyDown(KeyCode.Escape) && gameIsPause)
      {
          unpauseGame();
      }
      <code></code>

    Example PauseGame() function:

    csharp
    void PauseGame()
    {
        Time.timeScale = 0;
        gameIsPause = true;
        pauseMenu.SetActive(true);  // Ensure the UI is visible
    }
    <code></code>

    5. Check Update() Flow:

    In the player script’s Update(), the order of if statements matters. Ensure the gameIsPause check is the very first condition to prevent unnecessary checks during pause.

    csharp
    private void Update()
    {
        if (GameManager.Instance.gameIsPause) return;  // Ensure this is at the top
    
        if (playerStateList.incutscene) return;
    
        // All other gameplay logic follows...
    }
    <code></code>

    6. Ensure Coroutines Resume:

    If you’re using coroutines, they might not resume automatically after pausing. You need to restart them or manage them separately.


    Debugging Strategy:

    • Log each step: Add Debug.Log() in the unpauseGame() and key player methods to ensure they are triggered.
    • Check for disabled components: Ensure no component (Animator, Rigidbody, etc.) gets disabled during pause.

    This should help you diagnose and resolve the issue with the character not responding after resuming. Let me know if you need more targeted assistance! 🚀

Viewing 5 posts - 1 through 5 (of 5 total)
  • You must be logged in to reply to this topic.

Go to Login Page →


Advertisement below: