Class Gamepad
An Xbox-style gamepad with two sticks, a D-Pad, four face buttons, two triggers, two shoulder buttons, and two menu buttons that usually sit in the midsection of the gamepad.
Inherited Members
Namespace: UnityEngine.InputSystem
Assembly: Unity.InputSystem.dll
Syntax
public class Gamepad : InputDevice, IDualMotorRumble, IHaptics
Remarks
The Gamepad layout provides a standardized layouts for gamepads. Generally, if a specific device is represented as a Gamepad, the controls, such as the face buttons, are guaranteed to be mapped correctly and consistently. If, based on the set of supported devices available to the input system, this cannot be guaranteed, a given device is usually represented as a generic Joystick or as just a plain HID instead.
// Show all gamepads in the system.
Debug.Log(string.Join("\n", Gamepad.all));
// Check whether the X button on the current gamepad is pressed.
if (Gamepad.current.xButton.wasPressedThisFrame)
Debug.Log("Pressed");
// Rumble the left motor on the current gamepad slightly.
Gamepad.current.SetMotorSpeeds(0.2f, 0.
Properties
this[GamepadButton]
Retrieve a gamepad button by its GamepadButton enumeration constant.
Declaration
public ButtonControl this[GamepadButton button] { get; }
Parameters
Type | Name | Description |
---|---|---|
GamepadButton | button | Button to retrieve. |
Property Value
Type | Description |
---|---|
ButtonControl |
Exceptions
Type | Condition |
---|---|
ArgumentException |
|
aButton
Same as buttonSouth. Xbox-style alias.
Declaration
public ButtonControl aButton { get; }
Property Value
Type | Description |
---|---|
ButtonControl | Same as buttonSouth. |
all
A list of gamepads currently connected to the system.
Declaration
public static ReadOnlyArray<Gamepad> all { get; }
Property Value
Type | Description |
---|---|
ReadOnlyArray<Gamepad> | All currently connected gamepads. |
Remarks
Does not cause GC allocation.
Do not hold on to the value returned by this getter but rather query it whenever you need it. Whenever the gamepad setup changes, the value returned by this getter is invalidated.
See Also
bButton
Same as buttonEast. Xbox-style alias.
Declaration
public ButtonControl bButton { get; }
Property Value
Type | Description |
---|---|
ButtonControl | Same as buttonEast. |
buttonEast
The right face button of the gamepad.
Declaration
public ButtonControl buttonEast { get; protected set; }
Property Value
Type | Description |
---|---|
ButtonControl | Control representing the B/Circle face button. |
Remarks
On an Xbox controller, this is the B button and on the PS4 controller, this is the circle button.
See Also
buttonNorth
The top face button of the gamepad.
Declaration
public ButtonControl buttonNorth { get; protected set; }
Property Value
Type | Description |
---|---|
ButtonControl | Control representing the Y/Triangle face button. |
Remarks
On an Xbox controller, this is the Y button and on the PS4 controller, this is the triangle button.
See Also
buttonSouth
The bottom face button of the gamepad.
Declaration
public ButtonControl buttonSouth { get; protected set; }
Property Value
Type | Description |
---|---|
ButtonControl | Control representing the A/Cross face button. |
Remarks
On an Xbox controller, this is the A button and on the PS4 controller, this is the cross button.
See Also
buttonWest
The left face button of the gamepad.
Declaration
public ButtonControl buttonWest { get; protected set; }
Property Value
Type | Description |
---|---|
ButtonControl | Control representing the X/Square face button. |
Remarks
On an Xbox controller, this is the X button and on the PS4 controller, this is the square button.
See Also
circleButton
Same as buttonEast. PS4-style alias.
Declaration
public ButtonControl circleButton { get; }
Property Value
Type | Description |
---|---|
ButtonControl | Same as buttonEast. |
crossButton
Same as buttonSouth. PS4-style alias.
Declaration
public ButtonControl crossButton { get; }
Property Value
Type | Description |
---|---|
ButtonControl | Same as buttonSouth. |
current
The gamepad last used/connected by the player or null
if there is no gamepad connected
to the system.
Declaration
public static Gamepad current { get; }
Property Value
Type | Description |
---|---|
Gamepad |
Remarks
When added, a device is automatically made current (see MakeCurrent()), so when connecting a gamepad, it will also become current. After that, it will only become current again when input change on non-noisy controls (see noisy) is received.
For local multiplayer scenarios (or whenever there are multiple gamepads that need to be usable in a concurrent fashion), it is not recommended to rely on this property. Instead, it is recommended to use PlayerInput or InputUser.
See Also
dpad
The 4-way directional pad on the gamepad.
Declaration
public DpadControl dpad { get; protected set; }
Property Value
Type | Description |
---|---|
DpadControl | Control representing the d-pad. |
leftShoulder
The left shoulder/bumper button that sits on top of leftTrigger.
Declaration
public ButtonControl leftShoulder { get; protected set; }
Property Value
Type | Description |
---|---|
ButtonControl | Control representing the left shoulder button. |
Remarks
On Xbox controllers, this is usually called "left bumper" whereas on PS4 controllers, this button is referred to as "L1".
leftStick
The left thumbstick on the gamepad.
Declaration
public StickControl leftStick { get; protected set; }
Property Value
Type | Description |
---|---|
StickControl | Control representing the left thumbstick. |
leftStickButton
The button that gets triggered when leftStick is pressed down.
Declaration
public ButtonControl leftStickButton { get; protected set; }
Property Value
Type | Description |
---|---|
ButtonControl | Control representing a click with the left stick. |
leftTrigger
The left trigger button sitting below leftShoulder.
Declaration
public ButtonControl leftTrigger { get; protected set; }
Property Value
Type | Description |
---|---|
ButtonControl | Control representing the left trigger button. |
Remarks
On PS4 controllers, this button is referred to as "L2".
rightShoulder
The right shoulder/bumper button that sits on top of rightTrigger.
Declaration
public ButtonControl rightShoulder { get; protected set; }
Property Value
Type | Description |
---|---|
ButtonControl | Control representing the right shoulder button. |
Remarks
On Xbox controllers, this is usually called "right bumper" whereas on PS4 controllers, this button is referred to as "R1".
rightStick
The right thumbstick on the gamepad.
Declaration
public StickControl rightStick { get; protected set; }
Property Value
Type | Description |
---|---|
StickControl | Control representing the right thumbstick. |
rightStickButton
The button that gets triggered when rightStick is pressed down.
Declaration
public ButtonControl rightStickButton { get; protected set; }
Property Value
Type | Description |
---|---|
ButtonControl | Control representing a click with the right stick. |
rightTrigger
The right trigger button sitting below rightShoulder.
Declaration
public ButtonControl rightTrigger { get; protected set; }
Property Value
Type | Description |
---|---|
ButtonControl | Control representing the right trigger button. |
Remarks
On PS4 controllers, this button is referred to as "R2".
selectButton
The left button in the middle section of the gamepad (called "view" on Xbox controllers and "share" on PS4 controllers).
Declaration
public ButtonControl selectButton { get; protected set; }
Property Value
Type | Description |
---|---|
ButtonControl | Control representing the left button in midsection. |
squareButton
Same as buttonWest. PS4-style alias.
Declaration
public ButtonControl squareButton { get; }
Property Value
Type | Description |
---|---|
ButtonControl | Same as buttonWest. |
startButton
The right button in the middle section of the gamepad (called "menu" on Xbox controllers and "options" on PS4 controllers).
Declaration
public ButtonControl startButton { get; protected set; }
Property Value
Type | Description |
---|---|
ButtonControl | Control representing the right button in midsection. |
triangleButton
Same as buttonNorth. PS4-style alias.
Declaration
public ButtonControl triangleButton { get; }
Property Value
Type | Description |
---|---|
ButtonControl | Same as buttonNorth. |
xButton
Same as buttonWest Xbox-style alias.
Declaration
public ButtonControl xButton { get; }
Property Value
Type | Description |
---|---|
ButtonControl | Same as buttonWest. |
yButton
Same as buttonNorth. Xbox-style alias.
Declaration
public ButtonControl yButton { get; }
Property Value
Type | Description |
---|---|
ButtonControl | Same as buttonNorth. |
Methods
FinishSetup()
Perform final initialization tasks after the control hierarchy has been put into place.
Declaration
protected override void FinishSetup()
Overrides
Remarks
This method can be overridden to perform control- or device-specific setup work. The most common use case is for looking up child controls and storing them in local getters.
public class MyDevice : InputDevice
{
public ButtonControl button { get; private set; }
public AxisControl axis { get; private set; }
protected override void OnFinishSetup()
{
// Cache controls in getters.
button = GetChildControl("button");
axis = GetChildControl("axis");
}
}</code></pre></example>
MakeCurrent()
Make the gamepad the current gamepad.
Declaration
public override void MakeCurrent()
Overrides
Remarks
This is called automatically by the system when there is input on a gamepad.
OnAdded()
Called when the gamepad is added to the system.
Declaration
protected override void OnAdded()
Overrides
OnRemoved()
Called when the gamepad is removed from the system.
Declaration
protected override void OnRemoved()
Overrides
PauseHaptics()
Pause rumble effects on the gamepad. Resume with ResumeHaptics().
Declaration
public virtual void PauseHaptics()
See Also
ResetHaptics()
Reset rumble effects on the gamepad. Puts the gamepad rumble motors back into their default state.
Declaration
public virtual void ResetHaptics()
See Also
ResumeHaptics()
Resume rumble affects on the gamepad that have been paused with PauseHaptics().
Declaration
public virtual void ResumeHaptics()
See Also
SetMotorSpeeds(float, float)
Set the motor speeds of the low-frequency (usually on the left) and high-frequency (usually on the right) motors.
Declaration
public virtual void SetMotorSpeeds(float lowFrequency, float highFrequency)
Parameters
Type | Name | Description |
---|---|---|
float | lowFrequency | Speed of the low-frequency (left) motor. Normalized [0..1] value with 1 indicating maximum speed and 0 indicating the motor is turned off. Will automatically be clamped into range. |
float | highFrequency | Speed of the high-frequency (right) motor. Normalized [0..1] value with 1 indicating maximum speed and 0 indicating the motor is turned off. Will automatically be clamped into range. |
Remarks
Note that hardware will put limits on the level of control you have over the motors. Rumbling the motors at maximum speed for an extended period of time may cause them to turn off for some time to prevent overheating. Also, how quickly the motors react and how often the speed can be updated will depend on the hardware and drivers.