Search Unity

Bugs: Action Map Ignores Keyboard After Disabling And Re-enabling, Error Messages On Reload

Discussion in 'Input System' started by GreatKnight, Apr 14, 2019.

  1. GreatKnight

    GreatKnight

    Joined:
    Apr 14, 2019
    Posts:
    1
    Hello. I've searched online to try to see if someone else has had these issues, but so far I have had no luck. I apologize if the answer to my issues are already on in this forum, if it is then I may have overlooked it.

    I recently started using the experimental new input system and am loving it so far. However, I think I am having a bug occur when disabling and enabling certain controls. I am writing a pause menu for a 2D platformer prototype and want to freeze player controls while the menu is present (I thought that freezing time would be enough, but the player could still change the direction that they are facing and queue input). I currently have two action maps; one for the player, and one for the pause menu.

    Player Action Map:
    upload_2019-4-14_12-47-47.png

    Menu Action Map:
    upload_2019-4-14_12-48-19.png

    My initial thought was to disable the Player action map when pausing the game and re-enable the action map when unpausing. Disabling the player action map appears to work, but when I unpause my game my player can no longer move or jump, although the player can still attack. Since the Attack control is bound to Mouse left-click, I rebound it to the s key to see if it would still work; once this was done, it too lost control after pausing and unpausing. Since I also put a Debug.Log() statement in my Movement method, I also can see that the methods tied to the Keyboard are not being called at all in this state.

    This inactivity is not accompanied by any error messages in the console. I also double checked to make sure that controls.Player.enabled is indeed true after unpausing, which it is.

    Only the Player action map appears to lose use of the Keyboard though, as my Menu action map can still toggle the pause status with the escape key (this is likely due to the fact that the Menu action map has not been disabled and re-enabled).

    In case it is relevant, the keyboard I am using is the Razer BlackWidow Ultimate 2016 edition.

    Below is the code for the pause menu and player:

    Pause Code:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.Experimental.Input;
    5.  
    6. public class PauseMenu : MonoBehaviour
    7. {
    8.     public static bool isPaused = false;
    9.     public GameObject pauseMenuUI;
    10.     public InputMaster controls;
    11.  
    12.     void Awake()
    13.     {
    14.         controls.Menu.Pause.performed += action => OnPauseButtonPress();
    15.     }
    16.  
    17.     void OnPauseButtonPress()
    18.     {
    19.         if (isPaused)
    20.         {
    21.             Resume();
    22.         }
    23.  
    24.         else
    25.         {
    26.             Pause();
    27.         }
    28.     }
    29.  
    30.     void Resume()
    31.     {
    32.         pauseMenuUI.SetActive(false);
    33.         Time.timeScale = 1f;
    34.         controls.Player.Enable();
    35.         isPaused = false;
    36.     }
    37.  
    38.     void Pause()
    39.     {
    40.         pauseMenuUI.SetActive(true);
    41.         Time.timeScale = 0f;
    42.         controls.Player.Disable();
    43.         isPaused = true;
    44.     }
    45.  
    46.     void OnEnable()
    47.     {
    48.         controls.Enable();
    49.     }
    50.  
    51.     void OnDisable()
    52.     {
    53.         controls.Disable();
    54.     }
    55. }


    Player Code:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.Experimental.Input;
    5. using UnityEngine.PostProcessing;
    6. using UnityEngine.Tilemaps;
    7.  
    8. public class Player : MonoBehaviour
    9. {
    10.     public InputMaster controls;
    11.     public float jumpMagnitude = 13f;
    12.     public float movementSpeed = 9f;
    13.     private bool isGrounded = false;
    14.     private Animator animator;
    15.     public bool invertFaceDirection = true;
    16.     public bool isAlive;
    17.  
    18.     void Awake()
    19.     {
    20.         isAlive = true;
    21.         animator = gameObject.GetComponentInChildren<Animator>();
    22.  
    23.         controls.Player.Jump.performed += action => Jump();
    24.  
    25.         controls.Player.Movement.performed += action => Move(action.ReadValue<float>());
    26.         controls.Player.Movement.cancelled += action => Stop();
    27.  
    28.         controls.Player.Attack.performed += action => Attack();
    29.  
    30.         animator.Play("Idle");
    31.         controls.Enable();
    32.     }
    33.  
    34.     void Attack()
    35.     {
    36.         animator.Play("Attack");
    37.     }
    38.  
    39.     void Jump()
    40.     {
    41.         {
    42.             Animator animator = gameObject.GetComponentInChildren<Animator>();
    43.             if (isGrounded && isAlive)
    44.             {
    45.                 isGrounded = false;
    46.                 gameObject.GetComponent<Rigidbody2D>().AddForce(Vector2.up * jumpMagnitude, ForceMode2D.Impulse);
    47.                 animator.Play("Jump");
    48.             }
    49.         }
    50.     }
    51.  
    52.     void Move(float dir)
    53.     {
    54.         Debug.Log("Moved");
    55.         if (isAlive)
    56.         {
    57.             if (invertFaceDirection)
    58.             {
    59.                 gameObject.GetComponent<Transform>().localScale = new Vector3(-dir, 1f, 1f);
    60.             }
    61.  
    62.             else
    63.             {
    64.                 gameObject.GetComponent<Transform>().localScale = new Vector3(dir, 1f, 1f);
    65.             }
    66.  
    67.             if (isGrounded)
    68.             {
    69.                 animator.Play("Run");
    70.             }
    71.  
    72.             Vector2 direction = new Vector2(dir * movementSpeed, 0f);
    73.             gameObject.GetComponent<Rigidbody2D>().velocity = direction + new Vector2(0f, gameObject.GetComponent<Rigidbody2D>().velocity.y);
    74.         }
    75.     }
    76.  
    77.     void Stop()
    78.     {
    79.         if (isGrounded && isAlive)
    80.         {
    81.             gameObject.GetComponent<Rigidbody2D>().velocity = new Vector2(0f, gameObject.GetComponent<Rigidbody2D>().velocity.y);
    82.             animator.Play("Idle");
    83.         }
    84.     }
    85.  
    86.     void OnEnable()
    87.     {
    88.         controls.Player.Enable();
    89.     }
    90.  
    91.     void OnDisable()
    92.     {
    93.         controls.Player.Disable();
    94.     }
    95.  
    96.     void OnCollisionEnter2D(Collision2D collision)
    97.     {
    98.  
    99.         switch(collision.transform.tag)
    100.         {
    101.             case "Ground":
    102.                 isGrounded = true;
    103.                 if (isAlive) animator.Play("Idle");
    104.                 break;
    105.             case "water":
    106.             case "hazard":
    107.             case "mace":
    108.             case "enemy":
    109.                 controls.Disable();
    110.                 isAlive = false;
    111.                 animator.Play("Die");
    112.                 break;
    113.             default:
    114.                 break;
    115.  
    116.         }
    117.     }
    118.  
    119.     void OnTriggerEnter2D(Collider2D collision)
    120.     {
    121.         switch (collision.transform.tag)
    122.         {
    123.             case "cave background":
    124.                 gameObject.GetComponentInChildren<PostProcessingBehaviour>().profile.vignette.enabled = true;
    125.                 break;
    126.             case "above ground":
    127.                 gameObject.GetComponentInChildren<PostProcessingBehaviour>().profile.vignette.enabled = false;
    128.                 break;
    129.             default:
    130.                 break;
    131.         }
    132.     }
    133. }
    134.  


    I have found a "work around" for my issue that involves adding a check for the PauseMenu's isPaused static variable to my Player controls instead of disabling/enabling the Player action map, but I would much prefer to just toggle the action map.
    _________________________________________________________________________________

    I am also experiencing error messages when reloading my scene after the player's death. The game plays the same as it does on the initial scene load after reloading the scene, but player input is met with errors that reference the input system.

    Error Examples:
    upload_2019-4-14_13-42-19.png

    Despite these error messages, player input still works exactly the same as the initial load. I'm not sure if this is an error with the experimental input system or if it has to do with how I'm loading the scene.

    Reload Code:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.SceneManagement;
    5. using UnityEngine.Experimental.Input;
    6.  
    7. public class PlayerObservation : MonoBehaviour
    8. {
    9.     public GameObject player;
    10.     private float waitTimeInSeconds = 2f;
    11.     public static int coinCount = 0;
    12.     public const int totalCoins = 35;
    13.  
    14.     void Awake()
    15.     {
    16.         coinCount = 0;
    17.     }
    18.  
    19.     void Update()
    20.     {
    21.         if (!player.GetComponent<Player>().isAlive)
    22.         {
    23.             StartCoroutine(WaitThenLoadScene(waitTimeInSeconds));
    24.         }
    25.     }
    26.  
    27.     IEnumerator WaitThenLoadScene(float seconds)
    28.     {
    29.         yield return new WaitForSeconds(seconds);
    30.         SceneManager.LoadScene("2DPlatformScene1");
    31.     }
    32. }
    33.