Unity 2021: The new Input System: Part 2

This is the second part of our blogs about the new Input System (NIS), in this part we will go deeper into how to use the NIS and we will give an example of how to program the movements of your 2D characters.

If you have not read our previous blog where we explained how to install and how to make NIS respond to our commands via script, go to our blog and review it before starting.


1.What are the actions types ?

2.What does Unity do when we press a button?

3.What kind of interactions does the NIS have?

4.Now we are going to program the movement of a 2D object

1.What are the actions types ?

In the “Properties” section, there are 3 kinds od actions types that Unity processes for each control, which are the following:

  • Value: It is determined by default, in this Unity will be pending of any change that is made and in case more than one control has the same action it will determine which one should produce the action (disambiguation).
  • Button: Similar to “Value” but used when a single button triggers an action, it will not check which controls can trigger the action.
  • Pass-through: In this case, any control will activate the determined action.

2. What does Unity do when we press a button?

Each time we press a button Unity will generate 3 operations which are “Started”, “Performed” and “Canceled”, which mean the following:

  • Started: This operation indicates the frame in which the button was pressed
  • Performed: Indicates when the interaction is complete, which depends on the type of interaction assigned to the button
  • Canceled: It will occur at the moment we release the button, it may be before the “Performed” occurs.

Taking this into account, you must properly configure when you want the action to be carried out by your character and this is what interactions are used for.

3. What kind of interactions does the NIS have?

We are not always going to need an action to be executed immediately when a button is pressed, sometimes we need the action to be activated after a time. For this, there are different types of interactions that can be used with the NIS.

  • Default: the one used if we do not specify any other interaction, in this the operations “Started” and “Perfomed” will be called almost simultaneously and the last operation “Canceled” when the button is released, so you will see three calls in the console each time you press and release the button.
    Press: For button-type interactions. Use the behavior parameter to select whether the interaction should be triggered by pressing the button, releasing it, or both.
    Hold: Requires you to press the button a certain amount of time before activating the action.
    Tap: Requires you to press and release the button in a certain amount of time to activate the action.
    SlowTap: Requires you to hold down a Control for a minimum of time and then release it to activate the Action.
    MultiTap: requires the user to press and release a Control within a specified time a number of times, with no more than a specified amount of time between taps, for the Interaction to fire. In other words, if you want to do an attack combo this is the best interaction for that.

4. Now we are going to program the movement of a 2D object

In the image above you can see how the configuration of the “InputActionsAsset” should look for a 2D character.

First click on your “InputActionsAsset”, go to the “Actions” section, click on the cross and change the name of the new action to “Movement”.

Now in the “properties” section go to the “Action type” and select “Pass Through”

Now in the same section go to “Control Type” and select “Vector2”

The next step is to go to the “Actions” section in our action called “Movement” and we click on the cross in “add 2D Vector composite”

To finish this part we select a WASD key for each option of Up, Down, Left, Right

Now if you can go back to the image of how everything should be

We are almost done, now add the following lines to your script “InputPlayer”

What we did was:

  • We create 3 private variables named: horizontal, vertical and speed
  • We create a variable to capture the Rigidbody2D and call it in the Awake ().
  • Then in the SetVelocity () method we define the horizontal and vertical variables
  • In the Movement () method we call our action “Movement” and using the SetVelocity () method we finish defining the horizontal and vertical variables
  • To finish in FixedUpdate () using the Rigidbody.velocity we define how the player will move according to the horizontal, vertical and speed variables.

To finish, go to Unity and in your object “player” add the Rigidbody2D component and remember to adjust the events of the component “Player Input”

Now to move your object enter “playmode” and verify it, if you perceive it too slow, increase the variable “speed”. I hope this blog has helped you.

As always thank you so much for reading our blogs!!
If you find this blog useful let us a comment, and follow us in our Instagram @tortugamedev and Twitter @Totuas2.
And don’t forget to check out the rest of our blogs about game development and our games!!

Leave a Comment

Your email address will not be published.