Actions and Player Input component workflow
Follow this workflow to connect actions to methods on your GameObjects without code that finds the actions, for event-driven input and local multiplayer.
The Player Input component adds a layer on top of actions. Instead of finding actions and reading their values in your scripts, as in the Actions workflow, you connect actions to methods on your GameObjects, and the component calls your methods when the player performs the actions. The Player Input Manager component uses Player Input components to add players when input occurs on new devices, which helps with local multiplayer. To compare the workflows, refer to Input System workflows comparison.
To respond to input through the Player Input component, complete the following tasks:
- Configure actions in the Input Actions Editor.
- Add a Player Input component to your player.
- Connect actions to your methods.
- Optional: Add players for local multiplayer.
Configure actions in the Input Actions Editor
Define your actions and bindings in the Input Actions Editor, or start with the default project-wide actions. For instructions, refer to Create and assign a default project-wide actions asset and Configure actions.
Add a Player Input component to your player
To add a Player Input component to your player:
- Add a Player Input component to the GameObject that represents the player in your game.
- Assign your action asset to the Actions property.
- In Default Map, select the action map for gameplay, such as Player.
For more information, refer to Get started with the Player Input component.
Connect actions to your methods
The Behavior property of the Player Input component sets how the component calls your methods:
- Send Messages: The default behavior. It calls a method named
Onfollowed by the action's name, on the same GameObject. For example, the Move action callsvoid OnMove(InputValue value), and the Jump action callsOnJump. The method name starts with a capital letter even if the action name doesn't. - Invoke Unity Events: Lets you connect each action to any method in the Inspector window.
For more information, refer to Select a notification behavior. For instructions to connect actions with Unity events, refer to Configure Unity events.
If you access the actions in your scripts, use the component's actions property instead of InputSystem.actions, because the component can use its own copy of the actions.
Add players for local multiplayer
To let new players join when they press a button on their own device:
- Make your player GameObject a prefab.
- Add a Player Input Manager component to a GameObject in your scene.
- Assign your player prefab to the Player Prefab property of the Player Input Manager component.
For example, in a four-player local cooperative game, when a player presses Start on their controller, the Player Input Manager component creates an instance of the player prefab for them.
For more information, refer to Set up the Player Input component for local multiplayer and The Player Input Manager component.