LibraryHandling Keyboard, Mouse, and Controller Input

Handling Keyboard, Mouse, and Controller Input

Learn about Handling Keyboard, Mouse, and Controller Input as part of Game Development with Unity and C#

Mastering Player Input in Unity: Keyboard, Mouse, and Controller

Player input is the lifeblood of interactive experiences. In Unity, efficiently handling input from keyboards, mice, and gamepads is crucial for creating responsive and engaging gameplay. This module will guide you through the core concepts and practical implementation of input management using Unity's Input System.

Understanding Unity's Input System

Unity offers two primary ways to handle input: the legacy Input Manager and the newer Input System package. While the legacy system is simpler for basic setups, the Input System package provides a more robust, flexible, and modern approach, especially for cross-platform development and complex control schemes. We will focus on the Input System package for its advanced capabilities.

Core Concepts of the Input System

Input Actions map raw device inputs to logical game actions.

Input Actions are the bridge between physical device inputs (like pressing a key or moving a joystick) and the abstract actions your game understands (like 'Jump', 'Move', or 'Fire'). This abstraction makes your game adaptable to different input devices.

The Input System uses a hierarchical structure: Input Devices (e.g., Keyboard, Mouse, Gamepad) generate Input Signals (e.g., Key Press, Mouse Delta, Stick Axis). These signals are then bound to Input Actions (e.g., 'Move', 'Jump', 'Fire'). An Action Map groups related Input Actions. This separation allows you to define control schemes independently of the underlying hardware, making it easy to support multiple input devices and allow players to rebind controls.

What is the primary purpose of an Input Action in Unity's Input System?

To map raw device inputs to logical game actions.

Handling Keyboard Input

Keyboard input is typically used for movement, menu navigation, and activating abilities. With the Input System, you define actions like 'Move' (often a 2D Vector) and 'Jump' (a Button). You can then bind these actions to specific keys or key combinations.

Handling Mouse Input

Mouse input commonly involves looking around (mouse delta) and clicking for actions like firing or interacting. You can define actions for mouse position, button presses (left, right, middle click), and scroll wheel movement. Mouse delta is often used to control camera rotation.

Handling Controller Input

Gamepads offer analog sticks for movement and aiming, buttons for actions, and triggers for nuanced input. The Input System automatically handles the diverse layouts of different controllers. You'll typically map analog sticks to Vector2 actions for movement and aiming, and buttons to Button actions for jumping, shooting, or interacting.

Implementing Input Handling in C#

To use the Input System, you'll typically create an 'Input Actions' asset. This asset defines your Action Maps and Actions. You then generate a C# class from this asset, which provides a convenient way to access input events and values in your scripts.

The core of input handling in C# involves subscribing to events fired by your Input Actions or polling their current state. For example, to react to a 'Jump' action, you might subscribe to its 'performed' event. For continuous input like movement, you'd poll the action's value each frame. This approach decouples input logic from game logic, making code cleaner and more maintainable.

📚

Text-based content

Library pages focus on text content

Here's a conceptual example of how you might use the generated C# class:

Loading diagram...

Best Practices for Input Design

Prioritize player comfort and accessibility. Ensure controls are intuitive and consider offering remapping options.

When designing input schemes, consider the target platform and genre. For PC games, keyboard and mouse are standard. For console games, gamepads are essential. Mobile games often rely on touch controls. Aim for consistency across different input methods where possible. Provide clear visual feedback to the player when an input is registered.

Key Input Actions to Consider

Action TypeCommon Input DevicesTypical Game Usage
ButtonKeyboard, Mouse, Gamepad ButtonsJump, Fire, Interact, Menu Select
Vector2Keyboard (WASD), Analog Sticks, Mouse DeltaMovement, Camera Look, Aiming
AxisMouse Scroll Wheel, Gamepad TriggersZoom, Throttle, Volume Control

Learning Resources

Unity Input System Documentation(documentation)

The official and most comprehensive guide to Unity's Input System, covering installation, setup, and advanced features.

Unity Input System Tutorial: Getting Started(video)

A beginner-friendly video tutorial that walks through setting up and using the Input System package in Unity.

Unity Input System: Action Maps and Input Actions(video)

This video dives deeper into the concepts of Action Maps and Input Actions, explaining how to structure your input bindings effectively.

Unity Input System: Player Input Component(video)

Learn how to use the Player Input component to easily manage input for player characters and game objects.

Unity Input System: Handling Different Input Devices(video)

A tutorial focused on how to handle input from various devices like keyboards, mice, and gamepads seamlessly.

Unity Input System: Advanced Input Handling(video)

Explore more advanced techniques, including custom input processing and device management with the Input System.

Unity Input System: Best Practices for Input Design(video)

This video discusses important considerations and best practices for designing intuitive and accessible player controls.

Unity Input System: Rebinding Controls(video)

Learn how to implement control rebinding functionality, allowing players to customize their input schemes.

Unity Input System: Input Action Callbacks(video)

Understand how to use input action callbacks to react to specific input events like 'performed', 'canceled', and 'started'.

Unity Input System: Input Debugger(documentation)

Information on using the Input Debugger tool within Unity to inspect input devices and actions, aiding in troubleshooting.