Unity Input System: Keyboard, Mouse & Gamepad Controls - Your Step-by-Step Guide
Your game world is beautifully crafted, your characters are animated, and your scripts are ready to bring them to life. But how does the player actually control anything? How do they tell your magnificent hero to jump, swing a sword, or navigate a menu? This is where the Unity Input System comes into play. Forget the old ways; Unity's modern Input System package provides a robust, flexible, and future-proof way to handle all player interactions – from keyboard presses and mouse clicks to complex gamepad configurations.
For beginners, diving into a new input system might seem daunting, but once you understand its core concepts, you'll unlock unparalleled control over your game's user experience. This guide is your ultimate step-by-step tutorial on Unity's new Input System, covering everything from installation to setting up Action Maps for keyboard, mouse, and gamepad, and writing the C# code to make your game respond. Get ready to empower your players with seamless, intuitive controls!
1. Why the New Input System?
While Unity's older "Input Manager" (e.g., Input.GetAxis, Input.GetKeyDown) still works, the new Input System package offers significant advantages:
Platform Agnostic: Easily configure controls for PC, Mac, Linux, consoles, mobile, and VR from a single setup.
Action-Based: Instead of checking for specific keys, you define "Actions" (e.g., "Jump," "Move," "Fire"). You then bind various input devices to these actions. This makes control remapping much easier.
Contextual Input: Create different "Action Maps" for different contexts (e.g., "Player" controls during gameplay, "UI" controls for menus).
Local Multiplayer: Built-in support for multiple players.
Customization: Full control over input processing, dead zones, and more.
2. Installation and Setup
First things first: you need to install the package!
Step-by-step guide to installing the Input System package:
Open Package Manager: In Unity, go to Window > Package Manager.
Select "Unity Registry": In the dropdown menu at the top left of the Package Manager window, select "Unity Registry."
Find and Install Input System: Scroll down or use the search bar to find "Input System." Click on it, then click the "Install" button in the bottom right corner.
Enable New Input System: After installation, Unity will likely pop up a dialog asking you to enable the new Input System backend. Click "Yes." Unity will restart.
If it doesn't pop up: Go to Edit > Project Settings... > Player. Under "Other Settings," find "Active Input Handling." Change it to "Input System Package (New)" or "Both." Unity will restart.
Confirmation: Once Unity restarts, the new Input System is active!
3. Creating Input Actions Asset: The Heart of the System
The core of the new system is an "Input Actions" asset. This is where you define your actions and their bindings.
Step-by-step guide to creating your Input Actions asset:
Create an Input Actions Asset:
In your Project window, create a _Input folder (Right-click > Create > Folder).
Right-click inside your _Input folder > Create > Input Actions.
Name it PlayerControls.
Open the Input Actions Editor:
Double-click the PlayerControls asset to open the Input Actions Editor window. You'll see three columns: Action Maps, Actions, and Properties/Bindings.
4. Defining Action Maps, Actions, and Bindings
Let's set up controls for a basic player character (movement, jump, look).
Step-by-step guide to defining Action Maps, Actions, and Bindings:
Create an Action Map (
In the "Action Maps" column (left), click the + button.
Rename the new Action Map to Player.
This map will contain all actions related to controlling the player character during gameplay.
Define Actions within the
Select the Player Action Map.
In the "Actions" column (middle), click the + button.
Action 1:
Rename it Move.
Change its "Action Type" (in the Properties column) to Value.
Change its "Control Type" to Vector2 (for 2D directional input like WASD or joystick).
Click the + button under Move (in the Actions column) > "Add 2D Vector Composite." This will automatically create bindings for WASD, Arrow Keys, and Left Stick.
Optional: Expand the composite, then expand each binding (e.g., "Up"). You can see the W key is bound to Up. You can change these.
Action 2:
Click + in the Actions column. Rename it Jump.
Change its "Action Type" to Button.
Click the + button under Jump > "Add Binding."
In the Properties column, click "Path" > "Listen" button. Press the Space key on your keyboard. It will bind to Space [Keyboard].
Click + under Jump again > "Add Binding." This time, click "Path" > "Listen" and press the A button on your gamepad (e.g., Xbox A, PlayStation X). It will bind to Button South [Gamepad].
Action 3:
Click + in the Actions column. Rename it Look.
Change its "Action Type" to Value.
Change its "Control Type" to Vector2.
Click + under Look > "Add Binding." Click "Path" > "Listen" and move your mouse. It will bind to delta [Mouse].
Click + under Look again > "Add Binding." Click "Path" > "Listen" and move your right joystick on your gamepad. It will bind to Right Stick [Gamepad].
Save the Asset:
Crucial! Click the "Save Asset" button in the top left of the Input Actions Editor window. If you close the window without saving, your changes will be lost.
Now you have a fully configured input map!
Comments
Post a Comment