Game Development
Gamepad support
Learn bindings that work with a gamepad.
Gamepad bindings
In the new Input System every device uses the same path format: <Gamepad>/leftStick, <Gamepad>/buttonSouth. You can add both keyboard and gamepad bindings to one action; the action fires the same way no matter which device is used. Button names follow the Xbox layout: buttonSouth is A/Cross, buttonWest is X/Square.
moveAction.AddBinding("<Gamepad>/leftStick");
moveAction.AddBinding("<Keyboard>/arrowKeys");
Debug.Log("Iki cihaz baglandi");
Control scheme
In an Input Action asset you define one control scheme per device set, for example Keyboard&Mouse and Gamepad. Each binding is assigned to one or more groups. The PlayerInput component can change the active scheme with SwitchCurrentControlScheme.
playerInput.SwitchCurrentControlScheme("Gamepad", Gamepad.current);
Debug.Log("Duzen degisti");
This way the game knows which key hints to print.
Exercise Bind the Move action to two devices
Write a MonoBehaviour named GamepadDestek. In Start create an InputAction named Move with type InputActionType.Value. In the chain that starts with AddCompositeBinding("2DVector") bind the keys with With calls: <Keyboard>/w for Up, <Keyboard>/s for Down, <Keyboard>/a for Left, <Keyboard>/d for Right. Then add the left stick with move.AddBinding("<Gamepad>/leftStick"), call move.Enable() and print Debug.Log("Move hazir").
Show example solution
using UnityEngine;
using UnityEngine.InputSystem;
public class GamepadDestek : MonoBehaviour
{
void Start()
{
InputAction move = new InputAction(name: "Move", type: InputActionType.Value);
move.AddCompositeBinding("2DVector")
.With("Up", "<Keyboard>/w")
.With("Down", "<Keyboard>/s")
.With("Left", "<Keyboard>/a")
.With("Right", "<Keyboard>/d");
move.AddBinding("<Gamepad>/leftStick");
move.Enable();
Debug.Log("Move hazir");
}
}
Test yourself
-
What is the path of the left analog stick on a gamepad?
- <Gamepad>/buttonEast
- <Gamepad>/dpad
- <Gamepad>/leftTrigger
- <Gamepad>/leftStick
Show answer
<Gamepad>/leftStick — The left analog stick is leftStick; leftTrigger is the left trigger, buttonEast is the B/Circle button and dpad is the direction pad.
-
Bind the A button
jumpAction.AddBinding("<Gamepad>/button____");
Show answer
jumpAction.AddBinding("<Gamepad>/buttonSouth");
-
Gamepad terms
- buttonWest
- The X/Square button
- leftTrigger
- The left trigger button
- Control Scheme
- The key layout of a device set
- SwitchCurrentControlScheme
- Changing the active scheme
Next lesson: Summary - Advanced input