Joystick Input in Unity using Xbox360 Controller
This is a tutorial on how you can use Xbox 360 Controller for joystick input in Unity.
If you've ever tried on an Oculus Rift, you will know that it is rather annoying to have to find the WASD on a keyboard. It usually means you have to take the headset on and off. If you haven't tried the Rift yet, trust me when I say that taking it on and off to find WASD is indeed a pain. You can avoid this by having controller input.
First things first you need a Xbox360 controller for Windows. this is the one I have.
You will want to plug that in and make sure that your computer has recognized it by checking in Device Settings
Success. Moving on.
Open Unity project
Edit -> Project Settings -> Input
InputManager should look like this in the inspector
For reference, these are the axes we are going to be referencing when we start changing settings
Let's start with the Mouse X. Expand that tab
Change Type to Joystick Axis
Change Axis to 4th Axis
For Mouse Y
Change Type to Joystick Axis
Change Axis to 5th Axis
WHAT ABOUT ALL THESE OTHER VALUES? We will come back to that.
Next is Horizontal and Vertical. You will note that there are 2 sets of Horizontal and Vertical tabs. WE ARE DOING THE FIRST ONE FIRST.
Change Type to Joystick Axis
Horizontal Axis to X Axis
Vertical Axis to Y Axis
Now the SECOND SET OF HORIZONTAL AND VERTICAL SETTINGS
Change Horizontal to Joystick Axis and X Axis
Leave Vertical as Key or Mouse Button
Now to handle the "jump" button.
Change Positive Button to "joystick button 0". It would have said "space" before that.
Button 0 is the "A" button on Xbox 360 controller.
Now that we have all the basic input settings handled, we can go back and look at some of those values.
the Unity documentation sums them up pretty well.
The one I found most important was "Dead" meaning '"deadband value" and is used to prevent drift if a joystick is slightly off center while in rest.
I found there to be a LOT of drift with the right joystick, the one controlling 4th and 5th axis.
The values above are what work decently for my game, but it does take some fiddling with depending on your game and controller.
However, if you are going to be using the Oculus Rift or some other HMD to handle looking around, you may not want to spend a ton of time on those settings.
The drift was not bad for my horizontal and vertical movement on the x and y axis, but I did change the settings a little bit there.
Resources:
https://docs.unity3d.com/Manual/class-InputManager.html
https://wiki.unity3d.com/index.php?title=Xbox360Controller
Thoughts? Questions? Comment below or tweet at me @stopNgoh
Happy Rifting!
Comments
Anonymous
August 06, 2015
THANK YOU! I've spent all day trying to figure out how to map my PS4 controller, then I found your blog. Lifesaver!Anonymous
December 01, 2015
Honestly a great tutorial! Thanks! I was struggling with this, I wish Unity had better documentation for controller supportAnonymous
March 12, 2016
That's a thank you from me too. Setting this up for the first time on the PC and the deadzone and axis descriptions tripped me up all over again.