Search Unity

How to check what controller/device is in use?

Discussion in 'Input System' started by WizByteGames, Sep 25, 2019.

  1. WizByteGames

    WizByteGames

    Joined:
    Mar 28, 2013
    Posts:
    70
    I'm working on a Tower Defense game and I'm using the new input system. I have set up 2 control schemes one for Keyboard/Mouse and one for Gamepad. I want to switch to one scheme depending on what device has received input.

    Is there a way to check this? I've not seen anything in the documentation as far as scheme swapping or detecting per device interactions.
     
  2. jonas-echterhoff

    jonas-echterhoff

    Unity Technologies

    Joined:
    Aug 18, 2005
    Posts:
    1,666
    If you want to support multiple simultaneous local players, you can use the PlayerInput class to allow different players to use different devices. The PlayerInput class will then automatically match players to control schemes and devices.

    If you are building a single player game, you may not need control schemes at all, as you can simply have all controls for all device types enabled all the time.
     
  3. WizByteGames

    WizByteGames

    Joined:
    Mar 28, 2013
    Posts:
    70
    Yeah that part I know but what I was thinking was if I'm playing with a controller and I need to switch to a controller because of dead battery or whatever I would need to have it so the player can do so without having to restart the game or go into an options menu to do so.
     
  4. PixelLifetime

    PixelLifetime

    Joined:
    Mar 30, 2017
    Posts:
    90
    I have no problem connecting gamepad after starting the game and it works. At the same time it supports keyboard movement from the start. Isn't it the same for you? Are you using InputActionImporter asset?
     
  5. WizByteGames

    WizByteGames

    Joined:
    Mar 28, 2013
    Posts:
    70
    Oh no that isn't what I'm looking for. Lets say I am using a gamepad and decide to swap to keyboard and mouse. I want the my game to basically on the fly swap to the correct button prompts. But I can't do that since I don't know what device is currently in use. I'm not talking about if you connect/disconnect devices. Maybe I'm a bit confusing in what I want.
     
  6. Rene-Damm

    Rene-Damm

    Joined:
    Sep 15, 2012
    Posts:
    1,779
    If, say, you have a "Keyboard&Mouse" control schemes with bindings for keyboard and mouse and a "Gamepad" control scheme with bindings for gamepads, PlayerInput will allow the player to automatically switch between the two and you'll get a notification when the switch happens.

    So, say, the player is currently using the gamepad and then moves the mouse, the system will switch from the "Gamepad" to the "Keyboard&Mouse" scheme and unpair the gamepad and pair the keyboard and mouse to the player instead.

    ATM the notification only comes in at the InputUser level (which sits underneath PlayerInput).

    Code (CSharp):
    1. InputUser.onChange +=
    2.     (user, change, device) =>
    3.     {
    4.          if (change == InputUserChange.ControlSchemeChanged)
    5.             Debug.Log($"User {user} switched control scheme");
    6.     };
    Ultimately, it'll surface on PlayerInput, too, but either way, you'll get notified and can update UI hints in response.

    You can query the devices currently paired to the player from the PlayerInput's "devices" property.

    Code (CSharp):
    1. // Devices paired to first player.
    2. PlayerInput.all[0].devices
     
  7. WizByteGames

    WizByteGames

    Joined:
    Mar 28, 2013
    Posts:
    70
    Oh OK I have been just using the input system without using PlayerInput and InputUser. Now it is time to figure that out. Thanks a lot for the info.