Search Unity

  1. Unity 2017.2 beta is now available for download.
    Dismiss Notice
  2. Unity 2017.1 is now released.
    Dismiss Notice
  3. Introducing the Unity Essentials Packs! Find out more.
    Dismiss Notice
  4. Check out all the fixes for 5.6 on the patch releases page.
    Dismiss Notice
  5. Help us improve the editor usability and artist workflows. Join our discussion to provide your feedback.
    Dismiss Notice

Game Controller that works with Unity.

Discussion in 'Editor & General Support' started by Calixto, Sep 16, 2012.

  1. Calixto

    Calixto

    Joined:
    Aug 31, 2012
    Posts:
    66
    Hi, I'd like to purchase a handheld game controller device that I can use to more easily control my games in Unity. However, I went to our local computer store (CompUSA/TigerDirect) and only found ones that claimed to work with Wii. Is that what I should get, or is there a specific type that I should purchase so that I can use it to help me manipulate my game characters instead of using the WASD keys and the mouse?

    Thanks for your help,
    Calixto
     
  2. Black-Mantis

    Black-Mantis

    Joined:
    Feb 7, 2010
    Posts:
    1,457
    XBox Controllers can be used, and there were a few scripts floating around the forum(showcase). I'm not sure if Xbox controllers are a windows PC thing only though. I know I got the scripts for it somewhere in my archive but I cant remember where lol. If you look at the new animation system videos for Unity 4 then you can see Will Goldstone(hope I spelled It Right Will) using one in the video.
     
  3. Beennn

    Beennn

    Joined:
    Sep 11, 2010
    Posts:
    370
    If you just want it for testing, there's a program called motionjoy which I believe you can use a number of different console controllers with, definitely PS3 and Xbox controllers.

    (for the controller)
    http://www.motioninjoy.com/

    (for the mapping)
    http://xpadder.com/
     
  4. Batzarro

    Batzarro

    Joined:
    Apr 22, 2012
    Posts:
    50
    This topic has reminded me of something I've been meaning to look into. When making a game in Unity for PC, is it possible to make it compatible with generic USB pads? I know little about how they work exactly, but you don't need to install a specific driver, they just use a generic Windows driver when you plug them in. In Control Panel / Game Controllers, they're usually named 'USB Gamepad', and their properties just show 9 or so numbers which light up when you press a button, to show if they work. These numbers are the names of each button which the driver gives them.
     
  5. Beennn

    Beennn

    Joined:
    Sep 11, 2010
    Posts:
    370
  6. Batzarro

    Batzarro

    Joined:
    Apr 22, 2012
    Posts:
    50
    What is the Answers account thing? I can't access it, when I enter my username and password, it says they're wrong. But I'm logged into the forum right now with the exact same details :p
     
  7. Beennn

    Beennn

    Joined:
    Sep 11, 2010
    Posts:
    370
    Lol, I have a similar issues.

    It says:
     
  8. Calixto

    Calixto

    Joined:
    Aug 31, 2012
    Posts:
    66
    Hi Folks, thank you for your input. I'll look into the PS3 type. I would like to get one that I can use on my Windows PC and my Mac. :roll: OK< mybe I'm being to wishful on this one, but I'll have to continue doing my research. Thanks for the leads.

    Calixto
     
  9. i3DTutorials

    i3DTutorials

    Joined:
    Aug 26, 2010
    Posts:
    564
    I use the wired XBox 360 controller, works just fine.
     
  10. Storyteller

    Storyteller

    Joined:
    May 15, 2012
    Posts:
    23
    I use a logitech 'dual action usb' its a USB controller that looks like a psx controller. Very basic, no rumble. Was about 10$ at gamestop or wah-wah.
     
  11. CreativeChris

    CreativeChris

    Unity Technologies

    Joined:
    Jun 7, 2010
    Posts:
    364
    Generally a majority of JoySticks and gamepads work with Unity and your PC or Mac. The more popular pads appear to be X-Box 360 or ps2/3 like pads, I would recommend purchasing one of these, but if your also just looking for one for testing your games with, then you can pick up something a lot cheaper.

    Thanks,
    Chris
     
  12. Batzarro

    Batzarro

    Joined:
    Apr 22, 2012
    Posts:
    50
    Cheers.
     
  13. atmuc

    atmuc

    Joined:
    Feb 28, 2011
    Posts:
    989
    is wired controller different model than wireless? or you have a cable to connect to wireless model?
     
  14. atmuc

    atmuc

    Joined:
    Feb 28, 2011
    Posts:
    989
    i have a very cheap snopy model controller. i connected it to my imac. i works for movement like a used with cursor keys with game pad's left stick and its cursor buttons. 4th button works for jump. is not it standard to use right stick for mouse function like look around?
     
  15. guavaman

    guavaman

    Joined:
    Nov 20, 2009
    Posts:
    2,620
    "Work" should only be used very loosely here. Unity's support for joysticks on the PC is not complete by any means. For example, hot plugging is not supported at all. You'll have to warn your players beforehand that they must plug in their joysticks before running the game. It also means you have to detect when a controller is removed in-game and force the player to quit and start over because they won't be able to control the game with the joystick anymore. It's even worse with wireless controllers like XBox 360 because if the controller goes to sleep (take a break), it is disconnected and you cannot get Unity to recognize it being connected again. "Solution": force player to quit and start over. In the same vein, making mappable joysticks using Input.GetJoystickNames is hopelessly flawed. It's the only way you can get any sort of identifier of what joysticks are attached, but when a controller is disconnected it doesn't leave a null entry for the missing controller so your assignments get all twisted. (Say you have 2 controllers and you pull out ID 0. The array comes back saying joy2 = id0, so your controller assignments are shot. Even worse for 3-4 controller setups.) Also, the mapping of the name indices to joystick ports is not always the same from run to run. I've also seen other issues with people not having enough buttons/axes for complex controllers like racing wheels and flight systems. I've reported all these bugs/issues to Unity and they've gone by for 2 years now with no fixes so don't hold your breath. It's quite sad they never address these basic, simple problems and yet keep adding new features every day.

    Edit: It seems they did add some level of hot plugging somewhere along the line (Unity reports in the console when a joystick has been plugged in / unplugged, but the game doesn't seem to see it). So far I can't get it to work at all, but I'll experiment and see if anything new has been done. UPDATE: Finished testing. Hotplugging is not supported at all still. Once a controller is unplugged, even though the editor will detect it when re-plugged, the game will not. And the joystick to name association stuff hasn't been touched. There REALLY needs to be a better way than getting a string array of names to get attached joysticks (they're not even unique strings so there's no way to tell the difference between multiple joys of the same name ...)
     
    Last edited: Oct 8, 2012
    Shkarface-Noori likes this.
  16. CreativeChris

    CreativeChris

    Unity Technologies

    Joined:
    Jun 7, 2010
    Posts:
    364
    Hi @guavaman

    Your feedback there is useful, just to let you know we are going to be working on such improvements and more in an early version of 4.x, anything before then is high risk implementation at this stage.

    Thanks,
    Chris
     
  17. guavaman

    guavaman

    Joined:
    Nov 20, 2009
    Posts:
    2,620
    Boy, you don't know how glad I am to hear that! I've been waiting for some acknowledgement of the problem for 2 years now. (I didn't even realize you worked at Unity when I posted. I sure am glad I did.) Hopefully this will come in before I finish up the rest of my game. I was dreading releasing it the way things currently are, it being a console-style game designed around game controllers. Thanks!
     
  18. cynel

    cynel

    Joined:
    Apr 18, 2012
    Posts:
    734
    it appears the open beta uses game controllers to move and it detects them when there plugged in
     
  19. guavaman

    guavaman

    Joined:
    Nov 20, 2009
    Posts:
    2,620
    @CreativeChris

    We're on 4.3.3 now and its been 1 year and 3 months since your post. Any news on when joysticks will finally work in Unity? I've been waiting since 2009 for a fix...
     
  20. lmbarns

    lmbarns

    Joined:
    Jul 14, 2011
    Posts:
    1,459
    I'm trying to figure out which wireless controllers will work on Unity standalone PC games....

    Any advice?
     
  21. guavaman

    guavaman

    Joined:
    Nov 20, 2009
    Posts:
    2,620
    I am seriously disappointed. Unity 5 has now been announced and absolutely nothing at all has been done on this. I have spent thousands of dollars on Unity and this most basic, fundamental feature is completely broken going on 9 years now. I upgraded my Pro license when 4.x was announced specifically because of this. Now I'm going to have to do it again on 5. This is not right.
     
  22. Steve-Tack

    Steve-Tack

    Joined:
    Mar 12, 2013
    Posts:
    1,178
  23. guavaman

    guavaman

    Joined:
    Nov 20, 2009
    Posts:
    2,620
    Thanks, but no that won't help. I watched his video and read through the web site. This system exists on top of Unity's flawed Input system, therefore it contains all the flaws of that system. For Windows, he did the standard thing of wrapping XInput, so controllers will work in Windows including hot plugging, but all other platforms appear to simply use Unity's Input class. No hot plugging, same issues with controller names getting crossed, etc. See https://github.com/JISyed/Unity-XboxCtrlrInput/issues?state=open for another attempt to wrap the broken system -- this is the page of issues he ran into trying to make a cross-platform system. Mac and Linux have serious issues he cannot fix. The only reason Windows works is because, again, XInput which just works.

    So while this plugin provides an input map for common controllers cross platform, it doesn't really fix anything. I already have my own input mapper that does all this and more allowing users to customize their controllers fully in-game, but it's completely broken because of Unity's Input class.

    The only way possible I can see to get cross platform joysticks to work as they should work in the first place would be to write native DLLs for every platform. Essentially we need an XInput for Linux and Mac. However this will never work for web-player builds, which I'm not interested in anyway, but...

    Edit: Well, even XInput is not a cure-all for Windows. It doesn't work with any other controllers I've tested besides Xbox 360.
     
    Last edited: Mar 21, 2014
  24. Steve-Tack

    Steve-Tack

    Joined:
    Mar 12, 2013
    Posts:
    1,178
    I know that InControl is just a wrapper on top of the built-in stuff, but I wasn't aware of the Unity input functionality being that gimped. That is disappointing.

    I haven't done a ton with controller support in my Unity game, other than some hard-coded button and axis references for the 360 gamepad, with the intention of going with that sort of wrapper later. I'm used to XNA that *did* have proper connect/disconnect logic, but I suppose the fact that it *only* supported Xbox 360 controllers now makes more sense.

    It's curious that this issue isn't discussed more.
     
  25. guavaman

    guavaman

    Joined:
    Nov 20, 2009
    Posts:
    2,620
    There has been quite a bit of discussion on this over the many (many) years its plagued us, but since Unity won't or can't do anything about it, I guess everyone just gave up like myself. A quick search turned up these:

    Threads:
    http://forum.unity3d.com/threads/151349-Game-Controller-that-works-with-Unity
    http://forum.unity3d.com/threads/114993-Joystick-detection-and-direct-axis-detection
    http://forum.unity3d.com/threads/13...ported-quot-OFFICIAL-from-unity-quot-Now-what
    http://answers.unity3d.com/questions/390447/check-whether-specific-joystick-connected.html
    http://forum.unity3d.com/threads/201516-Associating-Input-GetJoystickNames()-with-joystick-1-2-3
    http://forum.unity3d.com/threads/5112-Controller-Inputs?p=896380&viewfull=1#post896380
    http://forum.unity3d.com/threads/86736-How-does-Windows-decide-which-joystick-is-1-which-is-2

    Feedback requests:
    http://feedback.unity3d.com/suggestions/joystick
    http://feedback.unity3d.com/suggestions/fix-multiple-joystick-support-w
    http://feedback.unity3d.com/suggestions/4148--more-joystick-events
    http://feedback.unity3d.com/suggest...ames-value-equals-joystick-id-at-inputmanager

    Attempts to fix it:
    https://github.com/speps/XInputDotNet
    https://github.com/JISyed/Unity-XboxCtrlrInput

    The general answer is always "use XInput" and I think most have accepted that and are fine with the very limited use case of Windows + X360 controller. Also, many may not even be aware of these issues, especially if they only support 1 controller in their games. (Many games may use networking for multiplayer and not need two or more controllers on one system.) I cannot accept the idea that my users should only be allowed to use X360 controllers on Windows if they want it to work as it should. Nor can I accept the idea that Mac and Linux users should be forced to either use the KB/mouse or make do with a broken, non-customizable controller system.

    Unity seems almost entirely focused on phones/tablets and adding fancy graphical effects and tools for people who don't want to learn how to program now it seems.

    Controllers are getting more important, not less, as a plethora of new consoles and microconsoles are continually being released.

    Games have had in-game customizable controller layouts since the early 90's. We still have no ability to offer this feature in our games in Unity because Input.GetJoystickNames() is completely inadequate for the purpose when dealing with more than one controller.

    - Clearly the Unity editor is able to detect hot-plugged devices because it does report connections/disconnections in the log.
    - Clearly Unity is able to tell when the same controller has been plugged in because it reports "Joystick Reconnected" if the joystick was removed then plugged back in. Therefore, they have access to some kind of unique ID they're keeping track of on the controller.
    - Clearly they have no interest in making this data available to us.

    EDIT:
    I just discovered something... Hot-plugging does now work in Windows provided you are taking input from all joysticks instead of one single joystick. EDIT2: ONLY IN THE EDITOR. Hot-plugging in builds does not work at all! The reason it doesn't appear to work is the joystick ID gets changed when you unplug/replug. Say you have 2 joysticks connected. If you unplug joystick 1, when you replug it will be come joystick 3. The next time you unplug and replug it becomes 1 again, then back to 3... So this could further explain why many don't even know about this problem. 1-player games have no need to target specific joysticks so they just leave the default "Get Motion from All Joysticks" and they never know the difference. That way you're still getting input even though the joystick ID changed on you. Edit: However now that I have determined hot plugging doesn't work in game builds, I'm baffled.

    Somewhere along the way they tried to add hotplugging, but the fact that there is no way to get a true ID of which joystick is which makes it impossible to use in a game that supports more than 1 joystick. So being able to correct for this anomaly would be possible if only they'd give us a joystick ID array and not just that dang useless Input.GetJoystickNames. The fix would be so unbelievably simple... sigh.
     
    Last edited: Mar 22, 2014
    aquilinajake likes this.
  26. guavaman

    guavaman

    Joined:
    Nov 20, 2009
    Posts:
    2,620
    I decided to do some testing on PC, Mac, and Linux to see exactly where the state of joystick support stands right now. I haven't tested linux yet, but already with just Windows and Mac I'm finding some very different behavior.

    -----------------------------
    Windows 8, Unity 4.3.4

    Joysticks:
    Editor: Works
    32-bit build: Works
    64-bit build: Works

    Hot plugging:
    Editor: Works (in some usb ports)
    32-bit build: FAILED
    64-bit build: FAILED

    Behavior of Joystick IDs when hot plugging:
    Joystick ID may or may not match the array index returned by Input.GetJoystickNames(). JoyID of plugged-in joysticks does not change when another joystick is removed, however the GetJoystickNames array does collapse. This leads to an ID/name mismatch.

    If you have joy1 and joy2 and remove joy1, joy2 stays in joystickIndex[1], but moves to nameIndex[0].
    Re-plugging joy1 may result in it occupying joystickIndex[0] or [2] (seems to alternate) and it may move to nameIndex[0] or [1]. There is no way possible to retrieve the true joystick ID to know which joystick a name corresponds to in the Unity Input Manager.

    State of Editor hot-plugging:
    Hot-plugging usually works in the editor, but certain USB ports may not work when hot plugged -- they only work when the device was plugged in from the launch of the editor, but once removed, the port no longer works even though the message that the joystick was plugged in does show up in the log.

    State of Build hot-plugging:
    Hot-plugging in game builds is utterly broken and unusable in Windows. Tests show hot-plugging never works in builds no matter what USB ports or controllers are used, 32 or 64 bit builds.

    --------------------------------------------
    OSX Mavericks 10.9.2, Unity 4.3.4

    Joysticks:
    Editor: Works
    32-bit build: Works
    64-bit build: FAILED - NO JOYSTICK SUPPORT - (Edit: Fixed in Unity 4.5. Haven't re-tested yet.)

    Hot plugging:
    Editor: Works
    32-bit build: Works
    64-bit build: N/A

    Behavior of Joystick IDs when hot plugging:

    In OSX, the ID of the joystick DOES actually match the array index returned by Input.GetJoystickNames(). What this means is that your joystick ID can and do change when a joystick is (un)plugged.

    For example:
    [0] = XBox 360
    [1] = P880

    Unplug Xbox 360 controller:
    [0] = P880

    Re-plug Xbox 360 controller:
    [0] = XBox 360 <- inserted itself back to ID 0
    [1] = P880

    When you unplug a joystick the array collapses and what used to be Joy ID 1 becomes 0. All input from that joy now goes to Joystick 1 in Unity's Input Manager.

    When you re-plug the Xbox 360 controller, it inserts itself back in index 0, so the P880 is pushed back to 1.

    Additionally, with 3 or more controllers the game of musical chairs with joystick IDs is more pronounced.

    This is completely different from the behavior on Windows, and probably in some ways a bit better behavior even if flawed. At the very least, you have the ability to KNOW what the ID of a joystick is if you can match the name. It still presents inconveniences to the users, for example making them switch controllers every time one is hot plugged.

    64-bit builds in OSX do not support joysticks at all. (Confirmed: https://github.com/JISyed/Unity-XboxCtrlrInput/issues/1) (Edit: Fixed in Unity 4.5. Haven't re-tested yet.)

    --------------------------------------------------------

    I need to set up Linux to do some testing and will post back.

    But the conclusion is, hot plugging is still completely broken on Windows in everything but the Editor (and not very good in the Editor either), works fairly well on the Mac (except 64 bit build), but both contain significant flaws that makes it impossible to give players a proper joystick experience with remapping, etc. because of the ID issues.
     
    Last edited: May 28, 2014
  27. guavaman

    guavaman

    Joined:
    Nov 20, 2009
    Posts:
    2,620
    Continued from the above post. Now on to Linux...

    ------------------------------------
    Ubuntu 12.04, Unity 4.3.4

    Joysticks:
    32-bit build: (Could not get to run)
    64-bit build: Works

    Hot plugging:
    32-bit build: N/A
    64-bit build: FAILED

    Behavior of Joystick IDs when hot plugging:
    Joystick ID may or may not match the array index returned by Input.GetJoystickNames(). JoyID of plugged-in joysticks does not change when another joystick is removed, however the GetJoystickNames array does collapse. This leads to an ID/name mismatch. Behavior is similar to Windows except joyIDs appear locked to specific USB hubs. Even with 2 controllers plugged in, your joystick IDs may be anything depending on the port/hub. You can never know the joystick ID even if there is only 1 joystick plugged in making it impossible to use any joystick option in the Input Manager except "Get Motion from All Joysticks" even for 1 player games.

    There is no way possible to retrieve the true joystick ID to know which joystick a name corresponds to in the Unity Input Manager.

    State of Build hot-plugging:
    Hot plugging does not work at all.

    --------------------------------

    Summary:

    Windows:
    Hot plugging does not work at all in game builds.
    It is impossible to reliably get a joystick ID to map controllers for players.

    OSX:
    Hot plugging does work in 32-bit builds. OSX is the only platform to support hot plugging in-game.
    Joysticks do not work at all in 64-bit builds. (Edit: Fixed in Unity 4.5. Haven't re-tested yet.)
    You can reliably get a joystick ID, but you have no control over how these ID shift and change as other controllers are (un)plugged. This makes it impossible to reliably map players to specific joysticks.

    Linux:
    Hot plugging does not work at all.
    It is impossible to reliably get a joystick ID to map controllers for players.

    Conclusion:
    • Unity's joystick support is incomplete and broken.
    • They've known this for years and refuse to do anything about it.
    • It would take all of a few minutes to expose the joystick IDs to developers so we could at least wrap the mapping issue up, yet I've been waiting 5 years through 3 major versions (paying for pro upgrades each time) and nothing has been done.
    • It is impossible to make an in-game joystick mapping/customization system because of these issues.
    • Everyone considering developing desktop games in Unity needs to be made aware of this.
     
    Last edited: May 28, 2014
    Shkarface-Noori likes this.
  28. TrentSterling

    TrentSterling

    Joined:
    Jan 4, 2013
    Posts:
    71
    You're doing god's work.

    I haven't particularly enjoyed setting up local multiplayer controls- even with the available scripts and configs on the wiki. I wasn't aware that support was this bad, I just knew it was a pain. :sad:
     
  29. guavaman

    guavaman

    Joined:
    Nov 20, 2009
    Posts:
    2,620
    Thanks for your support. I feel like nobody cares about this. But I'd say probably most people don't even know about these problems, which is why I'm trying to raise awareness. Unity's clearly not going to do a thing on their own. I will probably start another thread about this and see if I can get enough interest to do a petition or something. Waiting for them to do something has proven a huge waste of time.
     
  30. Steve-Tack

    Steve-Tack

    Joined:
    Mar 12, 2013
    Posts:
    1,178
    I definitely care about this issue, as my current game is very gamepad oriented. It's single player though, so supporting a single gamepad is good enough. The hot swappable thing is unfortunate, but I doubt many players disconnection/reconnect a lot during gameplay.

    But yeah, some of that seems very basic, especially for an engine as big as Unity. Even if they exposed a way to do that stuff, plenty of folks would come up with solutions.

    Do you have a recommendation on which of the feedback requests to spend votes on? One of them is for multiple joysticks, but not the disconnect detection. Another is for hot swapping, but also unfortunately mentions vibration, which I assume is a patent issue for Unity to support natively, so I'm not sure that's realistic.
     
  31. guavaman

    guavaman

    Joined:
    Nov 20, 2009
    Posts:
    2,620
    Actually the issue is more than just players disconnecting/reconnecting manually. These issues would even affect a 1-player game:

    Wireless controllers will disconnect when they fall asleep. I've tested the wireless Xbox 360 controller -- after 10 or so minutes of inactivity, the controller disconnects never to connect again. The player is forced to quit the game and start over to use his controller again. This could easily happen if the player pauses the game and walks off for a while. And if you forget to wake your wireless controller before you start the game, you'll have to quit and restart.

    If you plan to sell your game for Linux, you have no idea what joystick ID your joystick will be even if its the only one connected, so you will have to always use "Get Motion from All Joysticks" in all your input buttons/axes.

    If there is more than one joystick/steering wheel/other device that counts as a joystick plugged in even if your game is 1-player, you will never know which joystick is which. Therefore it makes it impossible to reliably create pre-defined controller configurations for popular joysticks in this situation. (Incidentally, this is a really big problem with multiplayer setups -- since you can never know which joystick name aligns with an ID, you can't have pre-defined layouts for popular controllers at all. Nor can you even save user-defined layouts specific to certain controllers.)

    Definitely 1-player games have fewer issues than multi-joystick games, but its still enough to make the player's experience potentially frustrating.

    No I don't. I think that's a big part of the problem is that its all disorganized. People vote in different topics none of which get many votes individually. Ultimately, Input needs a big revamp. We need joystick disconnect/reconnect events, reliable joystick ids, hot-plugging, access to more joystick KeyCodes beyond the first 4 sticks (Unity supports 11 sticks), scripting access to assign/change joystick axis information, consistent results across all platforms, etc. My calls for exposing joystick IDs is a bare-minimum plea for something barely workable that I can wrap and make somewhat usable. So I wouldn't know if I would want to vote for a topic that calls for a full revamp knowing it would be less likely than voting for something that puts a Band-Aid on the current system. I'm not sure...
     
    Last edited: Mar 24, 2014
  32. guavaman

    guavaman

    Joined:
    Nov 20, 2009
    Posts:
    2,620
    I thought I'd share with anyone stumbling on this thread a partial workaround for one of my problems.

    The goal: Allow players in a multi-player, multi-joystick game to assign joysticks and configure their controls in-game.

    ----------------------------

    Every time a controller is plugged in or unplugged:
    • Pause the game.
    • Clear all player/joystick assignments.
    • Display a message informing player that they must reassign all joysticks in the options screen because of limitations of the game engine.
    • Force user to re-assign all joysticks manually.
    • Inform them that hot-plugging is not supported and let them know they must exit the application, plug in all joysticks, and restart the application.

    To determine if a controller was plugged in or unplugged:
    • Check Input.GetJoystickNames() every 1-2 seconds for change in array size or change in string contents of the array.
    • This check must be done at all times, including during the gameplay, in menu screens, etc. since a controller can be removed at any time.

    When re-assigning controllers to players:
    • "Assign Joystick" button which brings up a window which instructs user to "Press any button on the joystick you want to assign to player 1"
    • Read the button pressed by checking ALL possible buttons on all joysticks using Input.GetKey(buttonId). (This will only work for joysticks 1-4. Otherwise, you must set up all joysticks in Input Manager and check that way to capture input from every possible joystick.)
    • Based on what button shows a true result, we can determine which joystick id pressed the button
    • If a joystick is already assigned to another player, confirm and disable joystick for conflicting player.
    • Now we use a custom input manager to map the player's controls to the right buttons/axes in Unity's InputManager.

    Disadvantages:
    • Highly inconvenient for user to have to reassign all joysticks every time one is plugged in or unplugged, including when wireless controllers sleep.
    • Increases garbage collection by continually getting string array to check joystick names because there is no event provided for connect/disconnect.

    Problems that cannot be solved:
    • Hot-plugging
    • Impossible to create pre-defined joystick profiles for different types of joysticks because name/id cannot be matched.
    • Impossible to save player custom control configurations for controllers because type cannot be determined to match to saved configuration.
    • Impossible to show players the name of the joystick they have assigned because of name/id mismatch.

    Other notes:
    • Because a user may have any number of controllers plugged in, we must create entries for all 11 possible joystick ids with all possible axes and buttons in Unity's Input Manager.
    • Buttons for joysticks 1-4 can be read using Input.GetKey using the proper key code. Joysticks beyond 4 can only be read with buttons defined in Input Manager.

    -------------------------------

    The key to this whole idea is the "Assign Joystick" button which reads the button press to determine the joystick ID. That is THE ONLY WAY POSSIBLE to actually get the true ID of the joystick in Unity on all platforms. You cannot simply select from a list of plugged-in controllers because of the id/name issues. Polling for the joystick allows you to get the actual ID. You can know no other information about this joystick, but at least with the ID you can map controls to the proper entries in Input Manager.

    The second key is de-assigning all joysticks every time one is unplugged/plugged. By doing this, you eliminate the issue of shifting joystick ids on some platforms.

    This system has a number of serious flaws still that ultimately make for a terrible, substandard input experience for players, but its the only way possible to even get something halfway workable for multi-joystick games in Unity.

    I hope this helps someone.
     
    Last edited: Mar 24, 2014
  33. playemgames

    playemgames

    Joined:
    Apr 30, 2009
    Posts:
    374

    Wow, great post! I was looking into this for joystick problems with my game, and trying to find solutions. I was wondering why I'd have to reassign joystick inputs every time I played the game, even though I have a config.ini file that saves the codes for the button mappings. This needs to be flagged as super important, as a lot of games nowadays are being made with the joysticks in mind. I was trying to figure out a way to autodetect if a player has an XBOX 360 controller hooked up so I can have the default buttons mapped to that, and I have up to 4 player couch co-op for my game so I would want it to remember all 4 controllers. This is definitely a serious issue, especially with games being made for Steam Box! Unity devs if you are reading this please open up the input so that we can handle this ourselves at least!
     
  34. guavaman

    guavaman

    Joined:
    Nov 20, 2009
    Posts:
    2,620
    Thanks for your support! The more people who get behind this the better. I'm going to repost a few messages in a new thread and hopefully try to get some more attention to the issue.

    Your situation is just about worst-case being that you're making a 4-player game! I'm only trying to do 2-player and it's bad enough.
     
  35. sloankelly

    sloankelly

    Joined:
    Jul 6, 2012
    Posts:
    9
    I'd just like to echo the sentiments of others; thank you for the research you've put into this issue. We've been struggling for weeks to find a solution and I only stumbled across this thread by accident today.

    If ANYONE from Unity reads this, please, please, please fix this as soon as possible. Not all of us are targeting mobile / touch and we need first-rate controller support. I'll be petitioning Unity about this issue suggest others do as well.
     
  36. Tsailen

    Tsailen

    Joined:
    Feb 13, 2010
    Posts:
    2
    Thanks, guavaman, for keeping this issue alive for all of out there that are just now discovering it..I too had a 4player game in mind, but this may change things...
     
  37. tamberlain

    tamberlain

    Joined:
    Jun 16, 2013
    Posts:
    19
    Thanks, guavaman. As I stated elsewhere, I share your pain and desire to see these problems fixed. I have no faith that Unity 5 will mean fixed joystick and controller support especially as they have never really mentioned it in the 3 years+ that this issue has been discussed. All I need is to be able to map more than 20 buttons to have my faith restored! The hot plugging and device sleep issues are another huge barrier that you have made me aware of. I was developing a flight sim / space sim in Unity but this has stopped me dead in my tracks. Now I'm looking at other game engines and Unity is about to lose one indie game studio as a customer. It's a shame because I like just about everything else about Unity so far.
     
  38. fffMalzbier

    fffMalzbier

    Joined:
    Jun 14, 2011
    Posts:
    2,546
  39. guavaman

    guavaman

    Joined:
    Nov 20, 2009
    Posts:
    2,620
    @Tsalien, @tamerlain, @sloankelly: Glad to help! And I'm happy to see more people are coming across this and realizing what a broken mess Unity's input system is. I've been dealing with this and waiting for over 5 years for a fix, and was even promised one 1.5 years ago by Creative Chris in this very thread which was supposed to show up in "an early version of 4.x" and now 5 is nearly out. Unity knows about it. I've even emailed the CEO David Helgason and the development team on the Unity Asset Store developers email group (which is read by David and a number of devs) about this problem, and all I received was chastisement from head of the asset store for using the group for an improper purpose. They know, yet they ignore it. If I provided this level of support and bug fixing to my asset store customers, I would have a zero star rating and rightfully so.

    I've tried both options (and mentioned so above even posting these same links). XInput is Windows only and works only with the Xbox 360 controller and no other gamepads that I've tested. The first link uses XInput for Windows, but obviously cannot not for Mac/Linux so they still have the same issues they had before with plain old Unity input (see above or see https://github.com/JISyed/Unity-XboxCtrlrInput/issues). With this external library, your users cannot use any of the plethora of joysticks, gamepads, wheels, flight yokes, etc. out there, only the Xbox 360 gamepad. I don't consider this a viable solution. At present the only solution would be to rely on external libraries, but I've not found any libraries that work on for generic controllers (and we'd need 3 different libraries, 1 for each platform).
     
    Last edited: May 15, 2014
  40. aitchest-of-dees

    aitchest-of-dees

    Joined:
    Dec 28, 2013
    Posts:
    37
    Wow, I'm just getting caught up in this problem. My needs are slightly different but I'm just as stuck as everyone else seems to be. I'm working on Windows-only for the moment, but I can't use XInput because I need more than 4 players and I want to support more than just Xbox 360 controllers. If we need 3 libraries to support Linux, Mac and Windows, I'm all for it and if I can crack the nut on Windows I'll be happy to share my code. I just can't find any working examples. All I want is basic and reasonable functionality: to be able to loop through all connected devices and check their inputs.

    I'm attempting to get this working in visual studio on its own: http://yal.cc/c-sharp-joystick-tracking-via-winmm-dll/ but it isn't detecting any Xbox controllers (which is all I have to test with currently) so either I'm doing something wrong or winmm doesn't support Xbox controllers. Then I'll have to come up with an Xinput + Winmm solution. It's hard to say at this stage as I haven't made any definitive progress.
     
  41. guavaman

    guavaman

    Joined:
    Nov 20, 2009
    Posts:
    2,620
    I hadn't seriously dug into trying to use native external libraries (mainly because I think it would be a huge task for me to try to get Linux and Mac working with my knowledge of those platforms), but your post got me looking around again. I got the winmm code example working with Xbox 360 and other controllers, but ran into some other odd behavior with hot plugging. I've attached the Visual Studio project for you to test if you want.

    Hot plugging support was oddly broken:
    1) If unplugging and replugging a controller into the same USB port it started in, hot plugging worked correctly and maintained the proper joystick ID _except_ for the Xbox 360 controller, which required unplugging and replugging twice every time to get it to reappear.
    2) Starting with no controllers plugged in, hot plugging works fine on the first plug-in.
    3) Once a controller that was previously connected is unplugged, it cannot not be moved to another USB port or it will not show up again in the device list.

    However, I was pleased that device ids were consistent.

    About winmm:

    http://stackoverflow.com/questions/4416994/joystick-key-capture
    Makes me wonder if using Direct Input directly would fare any better on the hot plugging problem.

    However, here's another big monkey wrench:

    http://msdn.microsoft.com/en-us/library/windows/desktop/jj151916(v=vs.85).aspx
    It seems Windows Store apps only support Xbox 360 controllers?? They can't be serious. What about racing wheels, flight sticks, etc.?

    EDIT: Hmm, it seems the XInput library does have support for things other than 360 controllers. The description is "XInput Game Controller API enables applications to receive input from the Xbox 360 Controller for Windows." However reading through the docs it describes racing wheels, guitar controllers, etc. http://msdn.microsoft.com/en-us/library/windows/desktop/hh405050(v=vs.85).aspx The question is, if it does support other devices, why doesn't anything but X360 work in the .Net implementations people have made for Unity. This may call for some investigation into those implementations. Perhaps they're incomplete...

    EDIT 2: I see. Racing wheels, guitar controllers, etc. that are made for Xbox 360 will work. Other controllers (legacy controllers) will not. Back to square 1.

    http://msdn.microsoft.com/en-us/library/windows/desktop/ee417014(v=vs.85).aspx
    So it seems for proper Windows support, both Direct Input and XInput must be implemented to support the widest range of devices. I'll try to do some Direct Input tests soon and see if its any better than winmm. Existing implementations of XInput for Unity always fall back on Unity's input system for non-XInput controllers, which is a major problem. I don't doubt Unity just uses Direct Input (on Windows), but hopefully hitting it directly will make it possible to get a proper device id and possibly even add some connect/disconnect events...
     

    Attached Files:

    Last edited: May 26, 2014
  42. aitchest-of-dees

    aitchest-of-dees

    Joined:
    Dec 28, 2013
    Posts:
    37
    Awesome, thanks for looking into this!

    I was having problems with Direct Input, namely I ran into what is described in this post: http://social.msdn.microsoft.com/Fo...-input-in-ref-class-of-form1-?forum=vcgeneral

    My googling from there led me to winmm, but maybe I took a step in the wrong direction. I will take a look at what you have done tonight; I have a pile of random USB controllers at home.

    The Windows Store thing isn't too surprising, but again, any solution on any platform at all is better than none. We will almost certainly have to use both XInput and something else for legacy.
     
  43. FuzzyQuills

    FuzzyQuills

    Joined:
    Jun 8, 2013
    Posts:
    2,835
    Anyone that is trying to use a PS3 controller for PC... Shouldn't. it will BSOD on you because MotionInJoy drivers are super buggy on windows 64-bit!

    Anyway, DirectInput probably isn't a good idea for fixing a bug. take one look at Sonic Generations' bug list and you will see why... ;)

    And hot plugging would be nice. UT, listen carefully... the Nintendo 64, despite stating that games could go unstable if you swapped controllers, supported hot-plugging. So perhaps there is a way to do it...
     
  44. fretnoize

    fretnoize

    Joined:
    May 24, 2013
    Posts:
    14
    Guavaman, I'm the author of InputPlus on the asset store, and while writing it I came across many of the problems you mentioned. I also discovered there are so many inconsistencies between gamepads (which is the problem InputPlus focuses on), but that's no reason for Unity to have a sub-par input system. As you might expect, since InputPlus is essentially a wrapper, like some of the other input solutions out there, it can't go beyond some of the limitations you talk about, like hot-plugging. You seem to have dug much deeper than I have, with trying outside APIs, not to mention you seem to be pretty proactive about trying to get a change made. So I wanted to throw my support behind you. I was inspired to write InputPlus after plugging into my tv and using Steam's Big Picture mode using PS2 controllers with a USB adapter. It was a general disaster trying to play PC games that way. To be fair some games worked well, but a lot were just good examples of what not to do. (Once I used a 360 controller things went a little more smoothly, but even though they're popular, not everyone has one or wants to use one)

    Anyway, as I said, I just wanted to support your efforts. There's no reason you should have to warn players not to unplug their controllers or to make sure all their controllers are plugged in before they run the game... or in the case of the 360 wireless controllers tell players to connect them all and then reconnect them in a certain order... (Although I *think* the 360 wireless weirdness might be on MS and not Unity, it seems a little difficult to track down.) Anyway, the way things are now, it's just NOT professional.

    Thanks for posting your findings!
     
  45. aitchest-of-dees

    aitchest-of-dees

    Joined:
    Dec 28, 2013
    Posts:
    37
    @guavaman I'm testing the WinMMtest project now. I played with it some late last night but I wanted to go through it more thoroughly.

    Test 1:
    Xbox 360 (wired)
    Sega Saturn knockoff USB

    Both controllers show up. Xbox is id 0 and Sega knockoff is id 2. Both respond on x y axis and Xbox responds on the z as well. They both report 0 dwButtons. X and Y axis values change a bit on the Xbox controller when pressing buttons, oddly.

    I unplug the Xbox and plug it back in. Windows detects it but the program does not. I unplug the Sega knockoff and plug it in and it shows up in the program again, id still 2. I unplug both and swap USB ports, the Xbox does show up again as id 0, but now the Sega Knockoff is gone. If I try putting it in another USB slot, it is still gone. I unplug the Xbox and replace it with the Sega knockoff (now in its original USB slot) and it shows up again with id 2. I plug the Xbox controller into a new USB slot (not the original one or the one the Sega is using) and miraculously, it shows up again, joy id 0.

    Test 1-B
    Air Flo swap for Xbox

    I unplug the Xbox and plug in a trusty old Nyko Air Flo USB controller. It shows up in joy id 4 acting normally. I can un/replug it and it works fine. I try swapping the Sega and Air Flo and I get a crash in the program:

    Test 2
    Xbox 360 (wired)
    Air Flo USB
    Sega Saturn knockoff USB

    With all 3 controllers plugged in I restart the program and all 3 are reporting in: Xbox id 0, Sega knockoff id 2 and Air Flo id 4. I test replugging all controllers: Air Flo and Sega knockoff work fine, Xbox predictably does not. Program crashes again. This time:

    I relaunch with all 3 plugged in. All fine. Replug all: Again all fine but Xbox. I swap Xbox and Air Flo: Xbox works id 0, Air Flo triggers an installation pop up oddly and I am prompted to restart my computer (???). I swap Air Flo for Sega knockoff, and neither work. I swap Sega knockoff for Xbox and nothing works. I replug Xbox and it's back. I swap Sega knockoff and Air Flo and they are both back as well.

    Test 3:
    USB hub
    Xbox 360 (wired)
    Air Flo USB
    Sega Saturn knockoff USB
    PS3 Street Fighter RF pad
    Sega Genesis via USB adapter

    All work, the buttons all still report 0. Replugging works for everybody but Xbox. I try swapping the Xbox and vshost-clr2.exe crashes again. I restart it, replug Xbox and it's gone. I swap the Sega and Xbox and both are back and working.

    So it looks like it's wildly inconsistent when swapping USB ports, though that sometimes coaxes the Xbox controller to work. Also interesting is that the joy ids seem to stick no matter what: Xbox is always 0, Sega knockoff always 2 and Air Flo always 4. All the controllers alwyas show up in the Windows Game Controllers control panel after un/replugging. So there is a way to reliably detect these.

    EDIT: I was playing with the code a bit and both jsx.dwButtonNumber and jsx.dwButtons both report 0. However I accidentally pasted js.wButtons in the if(joyEx) code block and that actually works. It reports the number of bits based on the button number (button 1 is 1, button 2 is 2, push them both and you get 3, etc). I'm not sure what the difference is between joyEx and non-joyEx but using non-joyEx gets the button states correctly for Xbox and regular USB controllers.
     
    Last edited: May 26, 2014
  46. Shkarface-Noori

    Shkarface-Noori

    Joined:
    Jun 10, 2013
    Posts:
    825
    that post was in 2012, it's 2014 now and we're on 4.3.7 (the editor is 4.3.4 but the runtime is 4.3.7), I'm curious that what 'an early version' means to you? still hoping it may come in 4.5 though..
     
  47. guavaman

    guavaman

    Joined:
    Nov 20, 2009
    Posts:
    2,620
    @FuzzyQuills: I'd be interested in seeing the bug list for Sonic Generations you're talking about. I looked it up but didn't find anything that listed a bunch of joystick problems except for the standard weird default config issues people ususally have with pc pads.

    @fretnoize: Thanks! I appreciate the support. I hope at some point there will be enough visibility of this issue to prod them to action. And I agree, it's not professional. It makes the games released with Unity for use with joysticks seem buggy and half-baked.

    @aitchest of dees: Thanks for testing it and giving your detailed report. On the 360 controller, I found that I had to unplug it and plug it back in twice in a row in order for it to reappear in the list. So it only appears back every other plug in. Odd.

    As for the issue with buttons being 0, you're right I didn't notice that on joyEx. I'm almost certain it's due to the dwFlags setting in the JOYINFOEX not being set properly. If you see my notes, I was unable to set the flags properly based on the flags listed here http://msdn.microsoft.com/en-us/library/dd757112(v=vs.85).aspx because I don't know how to access the flags in the DLL from C# or get their int value since its not listed in the docs. According to the docs, the flags MUST be set, yet I found if I set them to any value I tested, I'd get no results. Only by not setting the flags at all did I get even the axes to report correctly. As to the difference, from what I understand joyEx supports more information (more buttons, axes?) and should be used for any joystick that supports it.

    On to other news:

    I'm utterly baffled... Unity 4.5 just came out today and from the release notes, they applied the band-aid for Input.GetJoystickNames() I've been asking for for 5 years (with a major catch)...

    Unity 4.5 release notes
    However, it is only on the Android platform. Seriously?? (Yes, I tested Windows, editor and build. The array still collapses.)

    Also, they seem to have fixed bug with joysticks not being supported at all in OSX 64-bit builds.

    So it seems the waiting game continues...

    I haven't had time to get into DirectInput yet but I'll try to play with that soon.
     
    Last edited: May 27, 2014
  48. aitchest-of-dees

    aitchest-of-dees

    Joined:
    Dec 28, 2013
    Posts:
    37
    @guavaman Thanks again for looking at this stuff. Let me know if I should keep messing with the WinMM project or wait for your look at DirectInput. Also anything I can do to help on my end to help out I'd be happy to.
     
  49. guavaman

    guavaman

    Joined:
    Nov 20, 2009
    Posts:
    2,620
    NP. If there is a solution to be found, the more eyes looking the better. You mentioned before you ran into a problem with DirectX in managed code. So if that blocks that path, WinMM would seem to be the only (Microsoft-based) alternative.
     
  50. aitchest-of-dees

    aitchest-of-dees

    Joined:
    Dec 28, 2013
    Posts:
    37
    Well in the post I mentioned, the reply was "The only real option these days is to use the native versions of these libraries through interop." I'm not sure what this means. Looking into what the native libraries were is what lead me to WinMM. If there's something more basic we could use we should find out what it is.

    My take is, if Windows can reliably tell me all these controllers are connected in the control panel and test all their buttons, there has to be something we can use to do the same.