In this lesson, you will learn how to replace the outdated legacy input system with Unity’s modern, robust Input System package to handle player actions more effectively.
Moving Beyond Legacy Polling
Up until now, you have been using the legacy Input class (like Input.GetKey) inside Update. While simple, this "polling" method requires you to manually check the status of keys every single frame. This creates tight coupling between your movement logic and specific keyboard keys.
The modern Input System package shifts this to an event-driven architecture. Instead of asking "Is the spacebar held down?" every frame, you define "Actions" (like "Jump") and tell Unity to notify your script only when that action is triggered.
Installation and Setup
To get started, follow these steps in your Unity project:
- Install the Package: Go to Window > Package Manager. Select "Unity Registry" in the dropdown, find Input System, and click Install. Unity will prompt you to restart the editor; click "Yes" to enable the new back-end.
- Define Input Actions: Right-click in your Project window and select Create > Input Actions. Name it
PlayerControls. - Configure: Double-click your new
PlayerControlsasset. In the window that appears, create an "Action Map" (e.g., "Player"), add an "Action" (e.g., "Move"), and map it to a device (like a Keyboard WASD scheme). - Generate a C# Class: With the
PlayerControlsasset selected, look at the Inspector. Check the box that says Generate C# Class and click Apply. This creates a helper script that lets you talk to your inputs without typing long strings.
Why This Matters
By using this system, you decouple the intent from the device. If you decide to change your jump button from "Space" to "Left Mouse Click," you only change the configuration in your PlayerControls asset. Your game script remains completely untouched because it only cares about the "Jump" event, not which button triggered it.
Key Takeaway: Moving from legacy polling to the Input System package allows you to decouple your game logic from hardware-specific keys, enabling a cleaner, event-driven architecture that is easier to maintain and expand.