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

How to check which device is currently being used

Discussion in 'Input System' started by Static4242, Feb 1, 2020.

  1. Static4242

    Static4242

    Joined:
    Jan 27, 2020
    Posts:
    8
    So I'm trying to make a local multiplayer/online game

    My main issue is that I would like player1 to be able to simultaneously use both keyboard, mouse and gamepad.
    The autoswitching works as I have two control schemes, one for player1 that gets set on create and one for all other players that by default makes them connect to a gamepad.

    My main problem now is to detect which device is being used as to create some code in regards to the aim direction of the player.

    The controller works, but now I need to see if the kb/mouse are being used as to switch how the direction is found.

    Here is my code:

    Code (CSharp):
    1.     Vector3 move;
    2.     Vector2 moveDir;
    3.  
    4.     float aim;
    5.     Vector2 aimDir;
    6.  
    7.     void Start()
    8.     {
    9.         player = Instantiate(playerObj, new Vector3(0.0f, 1.0f, 0.0f), Quaternion.identity);
    10.         player.GetComponent<MCPlayerinit>().playNum = playNum;
    11.     }
    12.  
    13.     void Update()
    14.     {
    15.         Move();
    16.         Aim();
    17.     }
    18.  
    19.     void Move()
    20.     {
    21.         move = new Vector3(moveDir.x, 0.0f, moveDir.y) * moveSpeed * Time.deltaTime;
    22.         player.transform.Translate(move, Space.World);
    23.     }
    24.  
    25.     void Aim()
    26.     {
    27.         Vector3 lookTowards = new Vector3(aimDir.x,0.0f,aimDir.y);
    28.         if (lookTowards != Vector3.zero)
    29.         {
    30.             player.transform.rotation = Quaternion.LookRotation(lookTowards);
    31.         }
    32.     }
    33.  
    34.     void OnMove(InputValue value)
    35.     {
    36.         moveDir = value.Get<Vector2>();
    37.     }
    38.  
    39.     void OnAim(InputValue value)
    40.     {
    41.         aimDir = value.Get<Vector2>();
    42.     }
     
  2. tntmeijs

    tntmeijs

    Joined:
    Oct 18, 2015
    Posts:
    1
    Hiya!
    Did you manage to figure this out yet?
    I'd like to implement something similar.

    I'm currently using this code to check which control scheme is active, but it's not great. I'd like to avoid hard-coding the control scheme names in my scripts.

    Code (CSharp):
    1. public void OnControlsChanged(PlayerInput input)
    2. {
    3.     if (input.currentControlScheme.ToLower() == "controller")
    4.     {
    5.         SensitivityModifier = ControllerSensitivityModifier;
    6.     }
    7.     else if (input.currentControlScheme.ToLower() == "keyboard and mouse")
    8.     {
    9.         SensitivityModifier = MouseSensitivityModifier;
    10.     }
    11. }
    Keeping an eye on this thread to see whether someone knows how to do this.

    Cheers!
     
    Static4242 likes this.
  3. Static4242

    Static4242

    Joined:
    Jan 27, 2020
    Posts:
    8
    Not yet, instead I've currently been setting them within a player selection. But thanks for the input! Honestly any kind of information regarding matters like this is helpful.
     
  4. Eldoir

    Eldoir

    Joined:
    Feb 27, 2015
    Posts:
    60
    Hi,
    I'm boggling my mind around this multiplayer issue as well.
    I thought I had a working Lobby scene where 4 players could join, but it actually seems to work pretty badly when inputs are processed at the same time (i.e 2 controllers pressing A button at the same time)... most of the time, only 1 of the 2 controllers is responding to the input. This is really bad. Maybe I'm not using the API properly, but I've read the doc enough and saw multiple Youtube videos, none is having the same problem as me.

    Anyway, as of your question, I encountered the same problem. Here is how I solved this:

    Code (CSharp):
    1. public void OnJoin(InputAction.CallbackContext ctx)
    2. {
    3.     Debug.Log(ctx.control.device.deviceId);
    4.  
    5.     bool isKeyboard = ctx.control.device is Keyboard;
    6.     bool isGamepad = ctx.control.device is Gamepad;
    7.     Debug.Log($"Keyboard: {isKeyboard}, Gamepad: {isGamepad}");
    8.  
    9.     if (isGamepad)
    10.     {
    11.         var dic = JsonConvert.DeserializeObject<Dictionary<string, object>>(ctx.control.device.description.capabilities);
    12.  
    13.         if (dic.ContainsKey("userIndex"))
    14.         {
    15.             int controllerIdx = int.Parse(dic["userIndex"].ToString());
    16.             Debug.Log(controllerIdx);
    17.         }
    18.     }
    19. }
    Further explanation :

    - The device id is unique and identifies the device (be it a keyboard, mouse, gamepad...)

    - Keyboard and Gamepad are classes that are inheriting InputDevice, so that's why we can use the C# keyword "is" to check the type of the input device. It's better than using hardcoded strings of course :) And you can now safely rename your schemes without breaking the code!

    - The last, more complicated chunk of code is how I can know what controller it is (numbered 0 to 3). This method is using Newtonsoft.Json, which you can find on the Asset Store for free. I had to use Newtonsoft.Json because it just didn't work with the native Unity way for some reason (with the method called JsonUtility.FromJson)

    I don't know what coding skills you have, so I'm just throwing bits in this topic if it can help.
    I feel like even after months of development on this new Input System, we're still really in preview... I'm thinking about going back to the good ol' method.

    EDIT: I stumbled upon this thread while looking for a solution to my multiplayer problem: https://forum.unity.com/threads/current-control-scheme-null-on-player-join.819723/. The current preview version in the package manager seems to have bugs indeed!
    Here's the fixed version: https://github.com/Unity-Technologies/InputSystem/pull/1037
    I'll try it right now.
     
    Last edited: Feb 9, 2020
    Static4242 likes this.
  5. Static4242

    Static4242

    Joined:
    Jan 27, 2020
    Posts:
    8
    Thanks for the link mahn! Yeah apparently a lot this issues were due to a problem in 1.0.0 preview4's regression.
    Glad to know it's being fixed now.