Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

Question Switch Pro Controller is registered as Two Controllers at the same time (mixed A/B- and X/Y- events)

Discussion in 'Input System' started by deshalb, May 26, 2023.

  1. deshalb

    deshalb

    Joined:
    Feb 22, 2013
    Posts:
    12
    I am creating a game with local multiplayer support. Players can login by pressing the right trigger of their controller at any point.

    My problem is, that Switch Pro Controllers seem to be recogniced as two controllers at once. Gamepad.all gives me a SwitchProControllerHID and a XInputController, even if only one Pro Controller is connected. Also e.g. if i press B, the SwitchProControllerHID will recognice the button south as pressed, the XInputController will say that button east is pressed. Other Buttons like triggers or the sticks will work on both similarly, despite the fact that i only have one physical controller connected.

    Does anybody know why Unity thinks the Switch Pro Controller should also be an XInputController? Is there a way to determine, that both are the same controller? (they have differend deviceIds)
    I tried to banthe XInputControllers device-ID, when a switch and a xinput controller try to login to the game at the same frame, but more often than not the input from the XInputControllers comes at least one frame later. So this is not feasable...

    Thanks in Advance for any help :)
     
  2. unormal

    unormal

    Joined:
    Jan 10, 2012
    Posts:
    71
    Steam input is probably intercepting it and creating the XInputController.
     
  3. deshalb

    deshalb

    Joined:
    Feb 22, 2013
    Posts:
    12
    Thanks for the answer. Steam is Not running (but installed). But nontheless i have to make it work also for users who have steam installed ;) is there any way to recognice a virtual steam xinputcontroller or any ID or something, to find out that two Controllers are Actually the same physical Controller? Or how can i make use of the Pro Controller without being tricked by steam All the time?
     
  4. Ferrhoe

    Ferrhoe

    Joined:
    Jan 7, 2014
    Posts:
    15
    Hi.

    It's not gonna help you, but I ran into the exact same problem with a PS4 controller.
    My thread here: https://forum.unity.com/threads/ps4-controller-detected-twice-on-2022-2.1437199/#post-9022231

    No solution so far, but at least I know that I'm not alone.

    Can you tell me which version of Unity you are running ?
    My problem is happening on 2022, and an older version of my project on 2020 doesn't have it.

    Still trying to find a solution, and none of the "ID or something" worked for me, to detect if a controller was fake or not.
     
  5. deshalb

    deshalb

    Joined:
    Feb 22, 2013
    Posts:
    12
    Hey, thanks for the answer. I am on 2022 too and i also have an older Projekt thats does not have the Problem. I think the input System was still Experimental at that point. I will Look it up as soon as i can.
     
  6. zledas

    zledas

    Joined:
    Nov 3, 2010
    Posts:
    23
    I'm also having an issue with Switch Pro gamepad on Windows 10 using Unity 2022.3.4f1 and Input System 1.6.1. When I connect Switch Pro gamepad, Unity sees it as two devices: XInputController and SwitchProControllerHID. This is very unconveniente, because now we get multiple inputs for our single player game and multiple onControlsChanged events on each input...

    Anyone have any idea how this could be solved or what workaround could be used?
     
  7. deshalb

    deshalb

    Joined:
    Feb 22, 2013
    Posts:
    12
    For your singleplayer-game, you might Cache the Initial Controller-ID and then only use Inputs from that specific Controller. But in that case the User can not switch his Controller ingame...
     
  8. zledas

    zledas

    Joined:
    Nov 3, 2010
    Posts:
    23
    Yeah, I would like for the players to be able to switch controller seamlessly :(
     
    Last edited: Jul 7, 2023
  9. zledas

    zledas

    Joined:
    Nov 3, 2010
    Posts:
    23
    Might be useful for someone else. This ugly workaround at least lets keep using Switch Pro gamepad:

    Code (CSharp):
    1. // We execute this code on `playerInput.onControlsChanged`
    2. if (gamepad is UnityEngine.InputSystem.Switch.SwitchProControllerHID) {
    3.     foreach (var item in Gamepad.all) {
    4.         if ((item is UnityEngine.InputSystem.XInput.XInputController) && (Math.Abs(item.lastUpdateTime - gamepad.lastUpdateTime) < 0.1)) {
    5.             Debug.Log($"Switch Pro controller detected and a copy of XInput was active at almost the same time. Disabling XInput device. `{gamepad}`; `{item}`");
    6.             InputSystem.DisableDevice(item);
    7.         }
    8.     }
    9. }
     
  10. fben

    fben

    Joined:
    Oct 24, 2015
    Posts:
    10
    Was having the same issue with 2022.3.25 but your hack helps. thanks a lot
     
  11. beevik_

    beevik_

    Joined:
    Sep 27, 2020
    Posts:
    107
    I just ran into this today. Switch pro inputs are causing XInput and SwitchPro inputs to be generated in 2023.2.5 as well. I'm surprised it's still a bug. Does anyone from Unity read this forum?
     
  12. FrankTMY

    FrankTMY

    Joined:
    Nov 13, 2021
    Posts:
    6
    Same Problem
     
  13. Lucrecious

    Lucrecious

    Joined:
    Jul 1, 2022
    Posts:
    1
    Same problem. Seriously, it's issue after issue with Unity's "Input System".

    The UI overlaps everywhere too, making it almost impossible to navigate at times. You can imagine my disappointment when I looked up this issue and people were complaining about it over half a decade ago.

    Honestly, the only reason I even use Unity is due to the job market. If I want any contracts as a freelancer, Unity is the way to go. If I could have it any other way, I'd never think about touching this engine.
     
  14. Cypresdelabute

    Cypresdelabute

    Joined:
    Dec 18, 2021
    Posts:
    1
    Same problem for me in version 2022.3.12f1.
    The same issue occurred with PS4/PS5 controllers and was resolved recently, but it persists for the Switch Pro controller...
     
  15. Rocco_

    Rocco_

    Joined:
    Apr 27, 2015
    Posts:
    17
    What is the gamepad variable in the if statement in this scenario? OnControlsChanged seems to take a PlayerInput as a parameter but how do we get gamepad?
     
  16. Rocco_

    Rocco_

    Joined:
    Apr 27, 2015
    Posts:
    17
    Figured it out. For anyone else coming here from Google! Note that the solution below is absolutely not production ready and the real solution will need to be addressed by Unity. For temporary purposes though, this should work most of the time but in a scenario where an Xbox controller and a Switch controller are connected at virtually near the same time, the genuine Xbox controller may be disabled instead.

    Code (CSharp):
    1. // input is a PlayerInput type
    2. input.onControlsChanged += OnControlsChanged;
    3.  
    4. // Callback method
    5. private void OnControlsChanged(PlayerInput input)
    6. {
    7.     Gamepad gamepad = input.GetDevice<Gamepad>();
    8.     if (gamepad is UnityEngine.InputSystem.Switch.SwitchProControllerHID)
    9.     {
    10.         foreach (var item in Gamepad.all)
    11.         {
    12.             if ((item is UnityEngine.InputSystem.XInput.XInputController) && (Math.Abs(item.lastUpdateTime - gamepad.lastUpdateTime) < 0.1))
    13.             {
    14.                 Debug.Log($"Switch Pro controller detected and a copy of XInput was active at almost the same time. Disabling XInput device. `{gamepad}`; `{item}`");
    15.                 InputSystem.DisableDevice(item);
    16.             }
    17.         }
    18.     }
    19. }
     
    Last edited: Jun 12, 2024 at 6:00 AM