Using the Xbox 360 Scene it controllers in Unity3d

Today, as a proof of concept I adapted the Xbox 360 Scene it controller code that I had previously written to act as an input controller for my recently completed Unity3d Space Shooter game.

The Space Shooter game is nothing special, it is the result of going through the beginner tutorial on the Unity website.

It took me a good while to figure out how to hack the code I wrote into the Unity system. Keep in mind that this is just a hack and a more robust solution is needed for a production ready system.

Here is the link to the original project. I have updated it so that it is now fully compatible with Unity3d.

github.com

Changes

For some strange reason the Unity system did not like me using asynchronous reading from the USB device. The code just hung there on BeginRead and never got anything back from the device. I had to change the code to do a direct synchronous read from the USB device to get the code working properly in Unity.

I had to make the following changes to my PlayerController class to read the Xbox 360 Big Button Controller successfully

Limitations

The input code is dependent on the Update loop which makes it vulnerable to frame-rate drops and performance issues. Also it is likely that any issues with the controller code will significantly impact the performance of the game.

I ended up using the Loom technique to ensure that all updates coming from the USB device would be marshaled correctly to the main Unity thread.

For performance and speed reasons I ended up using a simple array to hold the state of the pressed buttons in each loop and using simple bit operations to read and unset the values private Buttons[] _state = new Buttons[4];. This makes the code a little hard to read for beginners but don't worry the techniques really are quite naive.

There is no easing when dealing with the Axis values (movement). This means that all movements coming in from the Big Button Controllers will be in discreet steps and not show a nice smoothed motion like the keyboard buttons and other controllers will.

I will have to incorporate this code into a custom Input Manager to actually make this code robust enough to use in non-trivial situations. Currently I have no idea how to achieve this though.

The full Unity script

I simply attached the script to the GameController object like so



Software Developer
For hire


Developer & Programmer with +15 years professional experience building software.


Seeking WFH, remoting or freelance opportunities.