XBox Controller Setup.
This is a quick tutorial and demo of setting up XBox One Controllers for Unity. I personally use an elite controller because they are so awesome :).
All the things I have built I have used the following links. With all of this it becomes easier to make the quick demo of working XBone Controller demo.
- Mac OSX Driver
- Triggers and Joysticks
- Trigger Start Values
- Both left and right triggers were returning 0 for me, so I normalize both if on Mac OSX
- Platform Dependent Compilation
The things I am currently programming are single player systems which means there is only ever one xbox controller. Therefore I am simply creating a single Empty GameObject with the attached component XBoxController. This Component will, on every FixedUpdate, update its public values. What this means is that anyone can find the controller and use the values with the following line.
Now I can check, on my Update, FixedUpdate, LateUpdate, whatever update, the values of the xbox controller and react accordingly.
#### Input Manager
First, the Input Manager controls all virtual axes, buttons, and joysticks.
These can be referenced via a string, such as
This makes programming controls a bit easier since
Vertical could be
fulfilled by more than one type of device.
So lets setup the Input Manager. First go to Edit > Project Settings > Input. You may have to adjust the size of the Input Manager if you do not want to overwrite any of the current values.
All of the settings have most properties in common, here are the common properties:
Here are all the fields that diverged:
This means I only have 6 inputs in my Input Manager settings. Therefore, Fire1 would not work (currently).
The code for the xbox controller is rather simple. Every Update I check the
values of those virtual axes and update the public properties of my
controller. Pay particular attention to the precompiler
This allows us to program specific logic for WebGL, which acts differently
than Mac OSX Stand alone.
Here is a simple demo app that will use the xbox controller values to color a black piece of material. There are no buttons considered here.