InControl requires a very specific set of input settings in Unity. You can generate the proper setup for through the editor menu:
InControl > Setup Input Manager
This will regenerate the ProjectSettings/InputManager.asset
file. If you require some settings of your own, you can add them to the end of the newly generated list and InControl will do its best to preserve them.
Note: InControl contains an editor script that will automatically check this asset. A warning will appear in the console letting you know when it needs to be regenerated.
Next, you'll need a script attached to a GameObject to initialize and update InControl. For convenience, there is a manager component included. You can add it to the hierarchy through the editor menu:
GameObject > InControl > Manager
You need not adjust any of its settings for now.
The project is isolated under the C# namespace InControl
. The primary API entry point is the InputManager
class.
Note: It is a good idea to alter the execution order of the InControlManager
component so that every other object which queries the input state gets a consistent value for the duration of the frame, otherwise the update may be called mid-frame and some objects will get the input state from the previous frame while others get the state for the current frame.
By default, InControl reports the Y-axis as positive pointing up to match Unity. You can invert this behavior by setting the Invert Y Axis
option in the InControlManager
component.
Now that you have everything set up, you can query for devices and controls. The active device is the device that last received input.
InputDevice device = InputManager.ActiveDevice;
InputControl control = device.GetControl( InputControlType.Action1 )
Query an indexed device when multiple devices are present like so:
var player1 = InputManager.Devices[0];
Note: This is not a good way to assign player order, though. Please read the section on Assigning Devices To Players under “Limitations & Best Practices” for more information.
Given a control, there are several properties to query:
control.IsPressed; // bool, is currently pressed
control.WasPressed; // bool, pressed since previous tick
control.WasReleased; // bool, released since previous tick
control.HasChanged; // bool, has changed since previous tick
control.State; // bool, is currently pressed (same as IsPressed)
control.Value; // float, in range -1..1 for axes, 0..1 for buttons / triggers
control.LastState; // bool, previous tick state
control.LastValue; // float, previous tick value
Controls also implement implicit conversion operators for bool
and float
which allows for slightly simpler syntax:
if (InputManager.ActiveDevice.GetControl( InputControlType.Action3 ))
{
player.Boost();
}
The InputDevice
class provides several shortcut properties to the standardized controls which can make for more pleasantly readable code:
if (InputManager.ActiveDevice.Action1.WasPressed)
{
player.Jump();
}
It also provides a few properties that each return special directional controls aggregated from individual controls for ease of use:
OneAxisInputControl LeftStickX;
OneAxisInputControl LeftStickY;
OneAxisInputControl RightStickX;
OneAxisInputControl RightStickY;
OneAxisInputControl DPadX;
OneAxisInputControl DPadY;
TwoAxisInputControl RightStick;
TwoAxisInputControl LeftStick;
TwoAxisInputControl DPad;
TwoAxisInputControl Direction;
OneAxisInputControl has identical behavior to other controls, but its value is a combination of two directions (left and right or up and down).
TwoAxisInputControl behaves similar to other controls, but provides a Vector2
value, which a combination of the four directions it is made up of. It can also implicitly be cast to Vector2
and Vector3
for convenience.
The Direction
property is a combination of the D-Pad and Left Stick. This is a often a useful simplification since not all controllers have both.
Finally, you can subscribe to events to be notified when the active device changes, or devices are attached/detached:
InputManager.OnDeviceAttached += inputDevice => Debug.Log( "Attached: " + inputDevice.Name );
InputManager.OnDeviceDetached += inputDevice => Debug.Log( "Detached: " + inputDevice.Name );
InputManager.OnActiveDeviceChanged += inputDevice => Debug.Log( "Switched: " + inputDevice.Name );