Gamepad support
- Game Development course
- Advanced
- Lesson 59/71
- 4 min
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.
Practice 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").
Starter code
using UnityEngine;
using UnityEngine.InputSystem;
public class GamepadDestek : MonoBehaviour
{
void Start()
{
// Move adinda, tipi Value olan bir InputAction olustur
// 2DVector bileseniyle W, A, S, D tuslarini bagla
// sol analog cubugu ayri bir baglama olarak ekle
// eylemi etkinlestir ve hazirlik mesajini yazdir
}
}Sample 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");
}
}Expected output
Move hazir
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 What is the path of the left analog stick on a gamepad?
- <Gamepad>/buttonEast
- <Gamepad>/dpad
- <Gamepad>/leftTrigger
- <Gamepad>/leftStick Correct answer
Explanation: The left analog stick is leftStick; leftTrigger is the left trigger, buttonEast is the B/Circle button and dpad is the direction pad.
2 Bind the A button
jumpAction.AddBinding("<Gamepad>/button____");
jumpAction.AddBinding("<Gamepad>/buttonSouth");
Explanation: In the Xbox layout the A/Cross button is named buttonSouth; buttonEast is B/Circle and buttonWest is X/Square.
3 Gamepad terms
| Term | Meaning |
|---|---|
buttonWest |
The X/Square button |
leftTrigger |
The left trigger button |
Control Scheme |
The key layout of a device set |
SwitchCurrentControlScheme |
Changing the active scheme |