Determining the Interaction Type of an Action with the New Input System in Unity 2021
In this brief article, I’ll show you how to differentiate between different Interactions of the same Action (or keypress) with the new Input System.
First of all, here is the Action Map being used for this example.
We’ll be looking at whether the Punch Action (F key) is being held or multi-tapped.
Currently the Multi-tap is set to trigger when two quick consecutive keypresses occur and the hold will trigger after being held down for ½ of a second.
We have the Using statement in our class script as well as a variable to reference the Action Maps of our InputActions generated script.
Additionally I have a string to contain the Interaction type once we get our hands on it.
In our Start() method we assign the _inputActions variable, enable the Character Action Map, and create a subscriber/listener method for when the Punch Action is performed.
The subscriber method, PunchOnPerformed(), is passed in a “context” variable.
Now, by default this will be named “obj”, but I’ve renamed it to fit the…context…
We can see that referencing this passed variable, context is the key to determining what interaction occurred.
With context.interaction.toString() I can easily do a compare statement and print out the result to the console as shown above.
And that’s it!
I told you this article would be brief!