Scripting Actions (New Unity Input System)

Suleiman Abdullah
4 min readFeb 12

--

Objective:Scripting Actions (New Unity Input System).

In the First step select our PlayerInputActions Assets ,then go to the inspector < tik a check box called Generate C# Class.

By doing the above step ,it will create A C sharpt script which contain all the logic behind action map and Action with all of its property.To show you i will select this generated C# class.

Note:We dont need to understand this code in this class ,and every time we modify or add another action map and action this class will be updated.

How to Acess our input binding from this new class.

1. first step we need to create new script ,mine I will rename as PlayerInput with the same folder level as our PlayerInputActions Asset.You can put this script were you like.

2. Second step ,We need to create a reference of this Player InputActions,then we need to create an Instance of PlayerInputAction.So open PlayerInput script and do this steps.

Note:To access this class we need to use a name space called

using UnityEngine.InputSystem”,to create Instance we use new keyword.

3.We need to enable Action map,this need a specific action map for me I will enable dog action Map.We do this using “_input.Dog.Enable()”

If we look into our action map we can see have more than one so I Enable Dog Action map.

4 we need to register perfom function,this perform function is an event so we will assign to function with the same signature ,go to learn about event to understand how event are registered /assigned to function with same siganature as evemt.So now I will assign an action then call perfom event then assign this event to a method which will impement a logic whether a dorg to back or back and charge toward enemy.

Note:This event will send like notification to object attached this scrip that player has press space/or any button then by assigning event to that function it will run logic in that function after press space bar.Got to learn about event and delegate to understand more.

Note: InputAction.CallbackContext is an event which we can use to tell the script whcih button pressed and we can do any logic depend on what type of method we registered from perfomed event.

Note-3:If we pass our parameter in debug we will go information of which button pressed.

Note-3
To auto create the method plus tab after typ plus equal sign.

Now Create empty object called player and attach this scrip then play the editor.

Playing the editor.

To understand when we release barking button or if we release space key,we do this using canceled event .

Playing the editor

This is how we access Input Map and action in C# script,see you in the next one.

--

--

Suleiman Abdullah

Self taught Unity Developer, who is passion about making games ,and he loves Math For Game Development