LibraryInput Manager

Input Manager

Learn about Input Manager as part of Game Development with Unity and C#

Understanding Unity's Input Manager

The Input Manager in Unity is a powerful tool that allows developers to abstract and manage player input from various devices. Instead of directly coding for specific keys or buttons, you define 'Axes' and 'Buttons' that represent abstract input actions. This makes your game more flexible, allowing players to remap controls or support different input devices without code changes.

Key Concepts: Axes and Buttons

The Input Manager organizes input into two main categories: Axes and Buttons. Axes typically represent continuous input, like joystick movement or mouse look, while Buttons represent discrete actions, such as jumping or firing.

Axes represent continuous input values.

Axes are defined by a name (e.g., 'Horizontal', 'Vertical') and can be mapped to one or two input devices (like a joystick's X-axis and a keyboard's A/D keys). They provide a float value between -1 and 1, indicating the direction and intensity of the input.

When setting up an Axis, you define its 'Gravity', 'Dead' zone, and 'Sensitivity'. Gravity determines how quickly the input value returns to zero when the input is released. The Dead zone is a range around zero where input is ignored to prevent drift. Sensitivity controls how quickly the input value reaches its maximum. You can also set 'Invert' to reverse the input direction. Multiple 'Alternative Axes' can be assigned to a single named Axis, allowing for flexible control schemes.

Buttons represent discrete on/off actions.

Buttons are defined by a name (e.g., 'Jump', 'Fire') and are mapped to specific keys, mouse buttons, or joystick buttons. They return a boolean value: true if the button is pressed, false otherwise.

Unlike Axes, Buttons don't have continuous values. They are simply pressed or not pressed. You can assign multiple keys or buttons to a single named Button, enabling players to use different inputs for the same action. For example, 'Jump' could be mapped to the Spacebar and the A button on a gamepad.

What is the primary difference between an Axis and a Button in Unity's Input Manager?

Axes represent continuous input values (e.g., movement from -1 to 1), while Buttons represent discrete on/off actions (true/false).

Accessing Input in Scripts

You can access the values from the Input Manager in your C# scripts using the

code
Input
class. This class provides static methods to get the current state of axes and buttons.

To get the value of an axis, use Input.GetAxis("AxisName"). This returns a float between -1 and 1. For example, float horizontalInput = Input.GetAxis("Horizontal"); will retrieve the value for the 'Horizontal' axis. To check if a button is pressed, use Input.GetButton("ButtonName"). This returns a boolean. For a single frame press, use Input.GetButtonDown("ButtonName"); which returns true only on the frame the button is first pressed. Input.GetButtonUp("ButtonName"); returns true on the frame the button is released.

📚

Text-based content

Library pages focus on text content

It's highly recommended to use the Input Manager for all player input to ensure your game is easily configurable and supports a wide range of input devices.

Configuring the Input Manager

You can access and modify the Input Manager settings through Unity's editor. Navigate to 'Edit' > 'Project Settings' > 'Input Manager'. Here, you can add new axes, configure existing ones, and define the keys or buttons associated with them.

FeatureAxisButton
Input TypeContinuous (float)Discrete (boolean)
Typical UseMovement, aiming, steeringJumping, firing, interacting
Value Range-1 to 1True/False
Script AccessInput.GetAxis()Input.GetButton(), Input.GetButtonDown(), Input.GetButtonUp()

Best Practices

Always use descriptive names for your axes and buttons (e.g., 'MoveForward', 'Jump', 'PrimaryAction'). This improves code readability and maintainability. Consider using the 'New Input System' package for more advanced input management, especially for complex projects or cross-platform support, though the legacy Input Manager is still widely used and effective for many scenarios.

Learning Resources

Unity Manual: Input Manager(documentation)

The official Unity documentation explaining the Input Manager, its settings, and how to configure it.

Unity Learn: Input System(tutorial)

A comprehensive learning path covering both the legacy Input Manager and the newer Input System package.

Unity Input Manager Tutorial (Brackeys)(video)

A popular video tutorial demonstrating how to set up and use the Input Manager in Unity.

Unity Scripting API: Input Class(documentation)

Detailed reference for the Input class, outlining all methods and properties for accessing player input.

Understanding Unity's Input System (GameDev.tv Blog)(blog)

A blog post that breaks down the concepts of Unity's input systems, comparing the legacy and new approaches.

Unity Input Manager - How to Use It (CodeMonkey)(video)

A practical video guide showing how to implement input handling in Unity using the Input Manager.

Unity Input Manager: A Complete Guide(blog)

An article providing a thorough explanation of the Input Manager's features and best practices for implementation.

Unity Input System vs. Legacy Input Manager(video)

A comparative video discussing the pros and cons of Unity's legacy Input Manager and the newer Input System.

Unity Input Manager Explained (Catlike Coding)(tutorial)

A detailed tutorial from a respected Unity educator covering the intricacies of the Input Manager.

Input Manager - Unity Wiki (Archived)(wikipedia)

An archived but informative resource that provides insights and examples related to Unity's Input Manager.