The Motivation behind XboxCtrlrInput

XboxControllerThe motivation to make XboxCtrlrInput came about from a problem I’ve encountered a few times in Unity3D.

It started with my team capstone project, Fortress Fiasco. One of the important features the team wanted to implement was local co-op play with multiple Xbox 360 controllers. I remember the programmer in our team who was implementing Xbox input was complaining about how awful Unity is about handling multiple joystick input. And he’s not the only one. I looked into his input code and realized that Unity was probably treating joystick input management as an afterthought. Of course I love using Unity, but I think Unity should improve the handling of multiple joysticks.

Flash forward to my Ludum Dare entry 10 Seconds In 10 Seconds, I wasn’t thinking about multiplayer at the beginning of its development. I did implement joystick input with an Xbox 360 controller, but my focus at the time was to make a single player game. Joystick input with just one joystick isn’t terribly hard with Unity. Towards the middle of development, I realized that it was possible to make a multiplayer version of the game because it became a top-down duel. I had to decide between either an AI opponent or a human-controlled second player. I didn’t have two Xbox controllers at the time so I chose to tackle with AI.

When I submitted my game on Ludum Dare, I got a lot of feedback saying that the game would be great with multiplayer, and I agree with them. Considering the control scheme of 10 Seconds In 10 Seconds, I thought using multiple Xbox controllers would be the best way to go, instead of having two players share the keyboard. Since I released a Mac, Windows, and Linux version of the game, it only made sense to me that a post-Ludum-Dare version of the game should continue to work on all those platforms with multiple Xbox controllers.

I know it sounds a little naïve, but I always figured that one of the goals of Unity was to allow people to develop games they can release on multiple platforms. Ideally everything should work on multiply platforms, including joystick input. It’s obviously not as simple as it sounds.

There are two major problems you have to face when implementing such a system. First, you have to get button mapping and axis mapping to “just work” on every operating system. For that, I won’t put much blame on Unity, since that’s a platform-dependent ordeal. But I will put much blame on Unity for the second problem: multiple joystick input support is lacking and difficult work with.

There appears to be a few attempts that exist out there on the internet to try to solve this problem, but none of them are perfect. Somebody with the domain name “uberdruck” showcased a web build that seems to be pretty good at handling multiple controllers, but uberdruck does not explain how the demo’s input system works. It’s only useful as an observation for what is possible. Deciduous Games made a C# input wrapper for Xbox controller input. It’s similar to XboxCtrlrInput in terms of being a static class, but the input mapping was implemented with Windows in mind, and thus will not work on other platforms without tweaking it. The last and most popular solution was a Github-hosted project called XInputDotNet, which is an XInput wrapper to allow XInput to work on .NET and Mono frameworks. This allows XInput to be used in Unity, which will allow for much better Xbox controller input. Only problem is that XInputDotNet relies on DirectX, so it will only work on Windows.

My goal with XboxCtrlrInput is to make a C# wrapper for Unity that makes it easy to get input from Xbox 360 controllers on multiple platforms (Mac/Windows/Linux). The included C# class XCI should be interfaced in the same way as Unity’s Input class. It sounds like a lot, but I’ve made fair progress. I invite anyone to contribute to the project on Github here. It’s free, open-source, and public domain, because I think Xbox controller input should “just work” with Unity. If you want to know the current issues about getting the input to work, be sure to take a look at XboxCtrlrInput’s Issues page on Github. If you don’t use Github, you can download a zip file containing the latest code from Github. Demo project included requires Unity 4.2 because I make use of text asset serialization (which became a free feature in version 4.2).

How to force all newly imported sounds to be 2D in Unity

[This is a cross post from the Fortress Fiasco blog.]

While importing audio into the game, I came across an annoyance. Since Radioactive Lollipop weren’t using 3D sounds in Fortress Fiasco, I had to convert all audio to 2D sounds by unchecking “3D Sound” in the inspector. This is not too big of a deal with small sound effects, but with large music files, Unity ‘re-imports’ the audio file and can take a little while.

So if you’re making a game with Unity and for the most part your sounds will be 2D or you don’t want to waste time converting music to 2D, use this C# script. For it to work, you must name the script “Force2DSound” and put it in the Editor folder. If you don’t have an Editor folder, you’ll have to make one under the Assets folder (so it would be "/Assets/Editor/Force2DSound.cs"). The script looks like this:


using UnityEditor;
using UnityEngine;

public class Force2DSound : AssetPostprocessor
{

    public void OnPreprocessAudio()
    {
        AudioImporter ai = assetImporter as AudioImporter;
        ai.threeD = false;
    }
}

After making this script, just import some audio into the project. It should have “3D Sound” unchecked automatically. Enjoy.

[Update] It seems that if by chance you wanted a 3D sound and you checked “3D Sound” in the Inspector, it will enforce 2D sound. So now I only recommend you use this script if you only want 2D sounds. Delete this script once you want 3D sounds.

———————————————————————

[Update 2] IMPORTANT: It seems this script doesn’t work properly in Unity 4.2. The imported sounds are labeled as not being 3D but when loaded into an AudioSource, it thinks they are 3D and plays them as 3D. I would NOT use this script anymore.

———————————————————————

[Update 3] Changed OnPostprocessAudio() to OnPreprocessAudio() and removed the AudioSource parameter. Works again, but you will still need to delete this script if you want 3D sounds in your game.