Vibration Controller for Xbox One Unity3d

In Unity3d a universal controller support through the class Input. After setting up the axes and buttons, you can achieve a tolerable job of any controller. Unfortunately, while there will be no support for gamepads vibration Xbox 360 and Xbox One. You can remedy this situation by using the plugin XInputDotNet, but as it turned out, this plugin is not ready for use in the Windows Store. We can use XInput which extends in this embodiment.

Creating a plugin for Unity3d fairly simple process. To do this, create a Class Library project in Visual Studio 2013, and do not forget to switch the mode of using the .NET Framework 3.5.

The resulting assembly should be moved to the directory Assets, and all the classes that you implement will be available from Unity. It is also convenient to place your Solution plug directly into the catalog,  in which the project is located Unity, and specify Output the catalog of Assets:

To the same plug-in works in application mode Windows Store, in the same Solution for our plugin create a project Store Apps / Class Library:

Plugin code can use the same, in this case do not even have to make any  conditions compile source files for both assemblies are the same and they can be just linked:

Option to build Windows Store must be placed in the directory Assets / Plugins / Metro:

Of course, the name of the plug-in file for Windows Store must be the same one for Desktop:

If done correctly, then Unity option when creating applications for Windows Store «picks up the" replacement and final assembly of the application in Visual Studio 2013 will take place correctly.

The plugin itself is simple, and is essentially a wrapper over functions XInput:

        public static extern int XInputGetState
            int dwUserIndex,  
            ref XInputState pState        

        public static extern int XInputSetState
            int dwUserIndex,  
            ref XInputVibration pVibration    

All the necessary structures prepared in related classes.

Using the plug-in to work with a gamepad

As already mentioned, it is now sufficient to place the assembly plug into the appropriate directories / Assets and / Assets / Metro, then the function will be available when developing an application Unity:

Usually work with a gamepad performed during queries (polling) before rendering each frame. In Unity, each object has a method void Update () in which it will be possible to interrogate the state of the buttons and axes, and depending on them to make changes in the behavior of game objects.
As an example, look at the finished project Unity who works as a desktop mode and can create an application for the Windows Store. This helicopter which flies in the mountains. Most certainly not fly, and "floats" as the full realization of the physics of the helicopter is beyond the scope of this article.

At the same time it responds to input from the gamepad on all axes, DPAD and by pressing the right trigger "revs" forward acceleration and unwinding the main and tail rotor:

void Update () 
        XInputWrapper.XInputState st=new XInputWrapper.XInputState();

        XInputWrapper.XInput.XInputGetState(0, ref st);

        if (Mathf.Abs(st.Gamepad.sThumbLY) > XInputWrapper.XInputConstants.XINPUT_GAMEPAD_RIGHT_THUMB_DEADZONE)
            float rotX_norm = (float)((float)st.Gamepad.sThumbLY / 32767.0f);

            transform.RotateAround(transform.position, transform.right, rotX_norm * 20.0f*-1.0f * Time.deltaTime);


On the subject of the helicopter associated Box Collider and RigidBody. This allows you to receive notifications of collisions with other objects, and then actually implemented vibration. Once the helicopter is facing mountain gamepad vibrates third of a second:

void OnTriggerEnter(Collider other)
        if (!vibrating)

            XInputWrapper.XInputVibration vibr = new XInputWrapper.XInputVibration();

            vibr.LeftMotorSpeed = 65535 / 3;
            vibr.RightMotorSpeed = 65535 / 3;

            XInputWrapper.XInput.XInputSetState(0, ref vibr);

            vibrationStartTime = Time.time;

            vibrating = true;



All data posted on the site represents accessible information that can be browsed and downloaded for free from the web.


User replies

No replies yet