Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question Can't find PlayerInput.currentControlScheme

Discussion in 'Input System' started by Vazili_KZ, May 12, 2020.

  1. Vazili_KZ

    Vazili_KZ

    Joined:
    Sep 18, 2016
    Posts:
    25
    Hi,

    So i'm trying to figue out which control scheme is currently used by the player in order to adjusted some variables accordingly. But i can't access PlayerInput.currentControlScheme for some reason.

    The way i set up my Input System is the following :

    I have a PlayerInputManager script attached to a GameObject in the scene.

    Code (CSharp):
    1. public class PlayerInputManager : MonoBehaviour
    2. {
    3.     public PlayerInput playerInput;
    4.     #region Singleton
    5.  
    6.     public static PlayerInputManager instance;
    7.     private void Awake()
    8.     {
    9.         instance = this;
    10.         playerInput = new PlayerInput();
    11.     }
    12.  
    13.     #endregion
    14.  
    15.     private void OnEnable()
    16.     {
    17.         playerInput.Enable();
    18.     }
    19.  
    20.     private void OnDisable()
    21.     {
    22.         playerInput.Disable();
    23.     }
    24. }
    This way, in any other script where i want to access the player input i just call :

    Code (CSharp):
    1. _playerInput = PlayerInputManager.instance.playerInput;
    But when i want to call PlayerInput.currentControlScheme this happens :

    It may be something silly that i'm just missing here but i'd really appreciate your help guys.

    PS: These are my Input Actions, I Have two separate Control Schemes For "Gamepad" and "Keyboard & Mouse" :

     
  2. Vazili_KZ

    Vazili_KZ

    Joined:
    Sep 18, 2016
    Posts:
    25
    Nothing... :(
    If anyone could just help point me in the right direction, some suggestion maybe, anything would help, i'm really stuck...
     
  3. fstorehaug

    fstorehaug

    Joined:
    Apr 3, 2017
    Posts:
    10
    hey.

    I haven't done anything like this myself, but perhaps you could use
    _playerInput.Controllscheemes[0] or just check if
    mouse.current == null && keyboard.current == null,
    or something like that.

    I don't know where you found ".currentControllScheme", but if it makes you feel better, I can't access that either. does it exist?
     
    Vazili_KZ likes this.
  4. Rene-Damm

    Rene-Damm

    Unity Technologies

    Joined:
    Sep 15, 2012
    Posts:
    1,779
    The auto-generated C# class has no currentControlScheme property. UnityEngine.InputSystem.PlayerInput does but it seems you have a PlayerInput.inputactions file for which you are using a generated C# class, i.e. the two classes are unrelated.

    The generated C# classes have no built-in support for control schemes ATM. It's on the list. For now, device and binding group restrictions have to be manually scripted when not using PlayerInput (the built-in one).
     
    Vazili_KZ likes this.
  5. Vazili_KZ

    Vazili_KZ

    Joined:
    Sep 18, 2016
    Posts:
    25
    Hi @fstorehaug , thanks for your reply.

    Well this won't work since there is no way to check the "currently used scheme", you can just for ex check if _playerInput.Controllscheemes[0] == "Gamepad") but if i'm not mistaken, that's just going to check if the first scheme in your InputActions is "Gamepad". If it is, it will always return true, false if not. but that's it, it's independent of which button you pressed ( be it a keyboard button or a gamepad one)

    Well this is a property in the PlayerInput Class, it returns the currently active control scheme as a String, you can get more details about it here.[/QUOTE]
     
  6. Vazili_KZ

    Vazili_KZ

    Joined:
    Sep 18, 2016
    Posts:
    25
    Thanks for your reply @Rene-Damm

    Well that explains everything! and since i'm using the generated C# class, i don't have access to the "currentControlScheme" property. Thanks for clearing that up.

    I checked out the link you referred to me. It explains how to assign different Control Schemes to different players in a Multiplayer situation. But in my case i have only one player that can use both Control Schemes.
    What i'm trying to achieve is a way to know which control scheme is the player currently using, is he playing with the Keyboard/Mouse or is he using a gamepad.
    This would help me for instance set up the camera rotation sensitivity. cause for example a 0.1f mouseSensitivity is perfect for the camera rotation, but when using that same value for a gamepad, the rotation is extremely slow.

    here is an example of the code i'm using currently :
    Code (CSharp):
    1. _yaw += _yawPitch.x * sensitivity;
    2. _pitch -= _yawPitch.y * sensitivity;
    and here is the logic i want to achieve :
    Code (CSharp):
    1. if (playerIsUsingGamepad)
    2. {
    3.     _yaw += _yawPitch.x * gamepadSensitivity;
    4.     _pitch -= _yawPitch.y * gamepadSensitivity;
    5. }
    6. else if(playerIsUsingKeyboardMouse)
    7. {
    8.     _yaw += _yawPitch.x * mouseSensitivity;
    9.     _pitch -= _yawPitch.y * mouseSensitivity;
    10. }
     
  7. Rene-Damm

    Rene-Damm

    Unity Technologies

    Joined:
    Sep 15, 2012
    Posts:
    1,779
    The thing with the auto-generated C# class is that it has no concept of a current control scheme. If you call Enable() on it with no further configuration, all the bindings from all the control schemes will simply be activated. I.e. in your case, both kb&mouse and gamepad bindings will be used at the same time.

    So, in that sense, with the manually scripted thing I linked, you'd know when the control scheme changes because you'd have to be the one changing it :)
     
    Vazili_KZ likes this.
  8. Vazili_KZ

    Vazili_KZ

    Joined:
    Sep 18, 2016
    Posts:
    25
    Thank you for your Input @Rene-Damm it helped a lot.

    The way i achieved the behavor i wanted is by "further configuring" it as you said, when calling OnEnable().
    I created an enum for InputType that has Gamepad and KeyboardMouse, and i check it's value to see which Input Bindings to enable :

    Code (CSharp):
    1. public enum InputType {KeyboardMouse, Gamepad }
    2. public InputType inputType;
    3.  
    4. private void OnEnable()
    5. {
    6.     if (inputType == InputType.Gamepad)
    7.     {
    8.         playerInput.devices = new InputDevice[] { Gamepad.all[0] };
    9.         playerInput.bindingMask = InputBinding.MaskByGroup("Gamepad");
    10.     }else if (inputType == InputType.KeyboardMouse)
    11.     {
    12.         playerInput.devices = new InputDevice[] { Keyboard.current, Mouse.current };
    13.         playerInput.bindingMask = InputBinding.MaskByGroup("KeyboardMouse");
    14.     }
    15.     playerInput.Enable();
    16. }
    And to check which Binding mask is currently active i just do this :

    Code (CSharp):
    1. if (_playerInput.bindingMask == InputBinding.MaskByGroup("Gamepad"))
    2. {
    3.     _yaw += _yawPitch.x * gamepadSensitivity;
    4.     _pitch -= _yawPitch.y * gamepadSensitivity;
    5. }
    6. else if (_playerInput.bindingMask == InputBinding.MaskByGroup("KeyboardMouse"))
    7. {
    8.     _yaw += _yawPitch.x * mouseSensitivity;
    9.     _pitch -= _yawPitch.y * mouseSensitivity;
    10. }
    Hope this might help someone else who has a similar issue.
     
    Awufumana and fstorehaug like this.