Search Unity

Gamepad not working with Unity events, but working when accessed directly

Discussion in 'Input System' started by ben4d85, Aug 3, 2020.

  1. ben4d85

    ben4d85

    Joined:
    Dec 26, 2018
    Posts:
    47
    I am having a weird problem trying to get my gamepad to work with the new Input System package 1.0.0 and Unity 2019.4.4f1.

    I use the PlayerInput component method with the Behaviour set to Invoke Unity Events as per Input System Docs.

    In the below code,
    OnMoveInput
    and
    OnJumpInput
    get called fine when the keyboard is used. When the gamepad is used however, they do not get called at all.

    So you'd think the gamepad isn't working. However, in the
    Update
    method, the gamepad works just fine when accessed directly!

    Why does the gamepad work when accessed directly, but does not cause the Unity events to be fired off?

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.InputSystem;
    3.  
    4. public class PlayerInputHandler : MonoBehaviour
    5. {
    6.     // Works with keyboard, but NOT with gamepad (Value)
    7.     public void OnMoveInput(InputAction.CallbackContext context)
    8.     {
    9.         Debug.Log("Move (Action)");
    10.     }
    11.     // Also works with keyboard, but also NOT with gamepad (Button)
    12.     public void OnJumpInput(InputAction.CallbackContext context)
    13.     {
    14.         Debug.Log("Jump (Action)");
    15.     }
    16.  
    17.     // Works with gamepad!!!
    18.     private void Update() {      
    19.         if(Gamepad.current.aButton.wasPressedThisFrame) {
    20.             Debug.Log("Jump (Update)");
    21.         }
    22.     }
    23. }
    What stands out in the following screenshots, is that nowhere do they mention Unity actually using my Gamepad control scheme???

    Screenshot 2020-08-03 at 09.25.38.png Screenshot 2020-08-03 at 09.25.49.png Screenshot 2020-08-03 at 09.28.29.png Screenshot 2020-08-03 at 09.42.32.png
     
  2. ben4d85

    ben4d85

    Joined:
    Dec 26, 2018
    Posts:
    47
    Right, so I finally got it working. For anyone else who runs into this, the solution is as follows...

    In my "Gamepad" Control Scheme, I had added two devices to the list (Wired and Wireless Gamepad). However, I hadn't noticed that by default, under Requirements, both devices were set to Required. And that was the problem.

    Switching both devices from Required to Optional solved it for me! Hooray!

    Screenshot 2020-08-04 at 13.04.26.png
     
    Pnvanol likes this.
  3. Pnvanol

    Pnvanol

    Joined:
    Jan 11, 2016
    Posts:
    123
    such a sneaky option, I had the same problem as you, thank you for posting the solution
     
    ben4d85 likes this.