In this lesson, you will learn how to use Control Schemes to automatically transition your game’s input logic between different hardware devices like keyboards and gamepads.
Why Use Control Schemes?
When you design a game, you often want the same action—like "Jump"—to be triggered by the Spacebar on a keyboard and the South Button on a gamepad. If you only map inputs manually, the game won't know which device the player is currently using. Control Schemes act as a filter in your Input Action Asset, telling Unity: "Only look for these specific bindings when the player is using this specific device."
Setting Up Schemes in the Input Action Asset
To implement this, open your Input Action Asset window and locate the Control Schemes dropdown (usually at the top left). By clicking the + icon, you can create new schemes, such as "Keyboard&Mouse" or "Gamepad."
- Define the Scheme: When you create a scheme, you specify the "Required Control Layouts." For example, select
KeyboardandMousefor your first scheme, andGamepadfor your second. - Assign Bindings: Once created, every action you define will have a list of bindings. You can now select a binding and check the boxes for which Control Scheme it belongs to.
- Automatic Switching: Because you have assigned the
PlayerInputcomponent (which you learned about in lesson 23), Unity will now automatically detect the device being used. If you press a button on your controller, Unity instantly switches the active "Control Scheme," ignoring the keyboard bindings and prioritizing the gamepad bindings you’ve linked to that scheme.
This approach keeps your C# logic clean. You don't need to write code to check if a controller is plugged in; your InputAction simply responds to the controls that are active under the current scheme.
// You continue to use the same logic regardless of the device:
void OnJump(InputValue value) {
// This method is called automatically whether the user pressed Space or 'A'
rb.AddForce(Vector3.up * jumpForce, ForceMode.Impulse);
}
Key Takeaway: Control Schemes allow you to map multiple hardware inputs to the same action, enabling Unity to automatically toggle between devices without needing complex conditional code.