Query and control gamepads in code
You can use the methods in the Gamepad class to access information about the gamepads connected to your application.
Discover all connected devices
There are various ways to discover the currently connected devices. To query a list of all connected devices (doesn't allocate; read-only access) use the following:
var devices = InputSystem.devices;
To get notified when a device is added or removed:
InputSystem.onDeviceChange +=
(device, change) =>
{
if (change == InputDeviceChange.Added || change == InputDeviceChange.Removed)
{
Debug.Log($"Device '{device}' was {change}");
}
};
To find all gamepads and joysticks:
var devices = InputSystem.devices;
for (var i = 0; i < devices.Count; ++i)
{
var device = devices[i];
if (device is Joystick || device is Gamepad)
{
Debug.Log("Found " + device);
}
}
Access gamepad buttons
To access a gamepad button, use its accessor property on Gamepad, such as Gamepad.leftShoulder:
var leftShoulder = Gamepad.current.leftShoulder;
Gamepads have both Xbox-style and PlayStation-style aliases on buttons. For example, the following properties all retrieve the same north face button:
var northButton = Gamepad.current.buttonNorth;
var sameButtonXbox = Gamepad.current.yButton;
var sameButtonPlayStation = Gamepad.current.triangleButton;
Add a deadzone to a gamepad
Deadzones prevent accidental input due to slight variations in where gamepad sticks come to rest at their center point. They allow a certain small inner area where the input is considered to be zero even if the input is slightly off from the zero position.
To add a deadzone to gamepad stick, put a stick deadzone Processor on the sticks, like this:
{
"name" : "MyGamepad",
"extend" : "Gamepad",
"controls" : [
{
"name" : "leftStick",
"processors" : "stickDeadzone(min=0.125,max=0.925)"
},
{
"name" : "rightStick",
"processors" : "stickDeadzone(min=0.125,max=0.925)"
}
]
}
You can do the same in your C# state structs:
/// <summary>
/// Example state struct for a gamepad with a deadzone on each stick.
/// </summary>
public struct MyGamepadState
{
/// <summary>
/// Left stick value, with a stick deadzone processor applied.
/// </summary>
[InputControl(layout = "Stick", processors = "stickDeadzone(min=0.125,max=0.925)")]
public Vector2 leftStick;
/// <summary>
/// Right stick value, with a stick deadzone processor applied.
/// </summary>
[InputControl(layout = "Stick", processors = "stickDeadzone(min=0.125,max=0.925)")]
public Vector2 rightStick;
}
The gamepad layout already adds stick deadzone processors which take their minimum and maximum values from InputSettings.defaultDeadzoneMin and InputSettings.defaultDeadzoneMax.
Using gamepads for mouse input
To use a gamepad for driving mouse input, refer to the sample called Gamepad Mouse Cursor. To access the sample, open the Package Manager window, and select the Input System package. Then select the Samples tab. The sample demonstrates how to set up gamepad input to drive a virtual mouse cursor.
You can also use the VirtualMouseInput component to control the hardware or software cursor. For more information, refer to VirtualMouseInput component.