Tuesday, May 7, 2013

Xbox Controller Input Wrapper for Unity

After setting up Gamepad input in Unity for around the 5th time, I decided to write a wrapper class to avoid future suffering. The class mimics the Input.GetButton() and Input.GetButtonDown() functions.

I also added a GetState() function, similar to XNA, which returns a snapshot of the controller's state.

  • You will need to replace the InputManager.asset file located in the ProjectSettings folder with the one contained in the package.
  • GetButtonDown() and GetButton() cannot be used to check the up/down/left/right buttons because Unity treats the dpad as an axis.


  1. Great script, thanks for sharing!

    Just a note, the inputmanager for TriggerR_1 set to axis 8, which is wrong, it should be axis 10.

    Great stuff!

  2. I'm trying to map my character movements to these inputs.. but nothing works ? I lcan see all the new inputs in the input settings under Edit> Project Settings > Input. I can also load the demo scene and see reactions to trigger etc...

    1. for example Horizontal & vertical movements are all gone... forward and back have gone. Fire1, Fire2 are gone
      so a brief explanation on setting up movement would be really appreciated...

    2. I have a character controller script + character controller + mouse look scripts applied to an object. Before I simply set Horizontal, vertical input values... but they're all gone in the input settings... DO you have a simple youtube vid. tut showing a controllers setup ??

    3. Hi Michael. The "Horizontal" and "Vertical" axes were overwritten when you replaced the input asset file so unfortunately they won't be accessible. I've never used the character controller before but if you want to move the character with the left analog stick you could do it like this:

      Vector2 leftStick = GamePad.GetAxis(GamePad.Axis.LeftStick, GamePad.Index.One);
      Vector3 directionVector = new Vextor3(leftStick.x, 0, leftStick.y);
      motor.inputMoveDirection = transform.rotation * directionVector;

      as opposed to:

      Vector3 directionVector = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));

  3. Hello,

    I stumbled upon this script while searching like crazy for multiple controller support for Unity. I happen to be making something similar to this which you can see here (https://github.com/JISyed/Unity-XboxCtrlrInput), but I'm stuggling to get it to work.

    Your code works for the most part, but after I unplug a controller (I have two as of now, so that's how many I plugged in) and plug it into another USB port and ran your demo again, it acts similar to my code. The joystick I moved used to respond to controller #2 input, but after changing the USB port and re-running the demo, it responds to controller #4 even though I only have two controller plugged in. My code does something similarly strange, so I know for sure this is a universal problem.

    I'm curious about a few things:

    1) What version of Unity did you use to make your demo? My implementation is currently using Unity 4.2.

    2) How many controllers did you test with this? Did they work as expected (i.e. if there were 3 controllers plugged in, none of them responded as controller #4)?

    3) Did you experience at all the problems I mentioned above concerning inconsistent controller identification (i.e. when two controllers are plugged in and one of them thinks it's controller #4)?

  4. I'm having similar issues to jibransyed.

    On top of that, I'm having big issues with the triggers on the xbox gamepad. It works fine with a single gamepad plugged in, but with more than one, it's completely messed up.

    I'll get some input from the right trigger on gamepad 2, a little from the left trigger on gamepad 1, etc. All of the other buttons and joysticks, and dpads work fine.

  5. The trick is to not remove any controllers while unity is running, and put them in again. Unity should assign controllers to the correct joysticks as long as you a. put in the joystick one after another while unity/the game is running, or alternative have all the joysticks connected when you start unity, but in that case the joysticks will be displaced, so that 1 becomes 4, 2 becomes 3, 3 becomes 2 and 4 becomes 1.

  6. Hi!

    I'm trying the script and it works perfectly on the Unity editor, but once I do a build, no input is detected. Any ideas why this happens? I'm working on a Mountain Lion with the Xbox360 controller.