Unity new Input System (Part V)

Matteo Lo Piccolo
4 min readDec 29, 2021

--

In this article we set every other action of our “Dog”.
It’s really simple, because the process is exactly the same as “Bark”.
First we access to input -> Dog
Then we find Walk and -> performed

I personally change obj to context, just for more readability.
And, as we did for Bark, simple Debug.Log() + context.

Back to Unity, Play

As we can see, every time we press W, A, S, D, the console print “Walking” and the context.
And every button is register in a 2D Vector,
(1.0, 0.0) for D, because we move to the RIGHT,
(-1.0, 0.0) for A, because we move to the LEFT,
(0.0, 1.0) for W, because we move to the UP,
(0.0, -1.0) for S, because we move to the DOWN.

Now we set up the “Run” action
Again, we go down directly throught
input -> Dog -> Run -> performed

Back to Unity, Play again

If we simple press Left Shift doesn’t work, but if we HOLD it for 1 second, the console print “Run” and the context.
In the context, we read
“control=Key:/Keyboard/leftShift value=1 interaction=UnityEngine.InputSystem.Interactions.HoldInteraction”.

If we want to see what happens when we release Left Shift, we can do this :

If we do this and we press Play

In the console we have two events :
when we Hold down the button, and when we Release the button.
In fact we see the value of the Left Shift:
when we hold it down it is set to 1, when we release it it is set to 0

control=Key:/Keyboard/leftShift value=1
and
control=Key:/Keyboard/leftShift value=0

The last step is “Die” Action

Again, same path to create “Die_performed” method.
And comment out Run Canceled, or delete if you prefer.

Go to Unity

And it work perfectly.

Probably you notice that it call the Die method, but it call even the Walking method.
This happen because with this system, we can’t “isolate” completely the functions.
It means if we need to Press F + D Button, when we press F nothing happen, but when we press D, it is already occupied by Walk.
It trigger the function in any case, but it call them both.
So we have both Walk and Die print in our Console.
Honestly, I don’t know if this is a “downside” in a more big and complex mechanism.
For me personally, I think is a great improvement, but doesen’t means you have to use only this and delete the old system.
As everything in programming, we make some test, and we try many many things right?
In any case is good to know what is it and how works, it is a new feauture (not so new in the moment i’m writing this article) but is really cool, and personally I love it.

Especially because it works with event driven system, which is incredible powerful to create a good and clean code.

In any case, in the next articles we go little bit deep in this system!

--

--

Matteo Lo Piccolo
Matteo Lo Piccolo

Written by Matteo Lo Piccolo

Always in love with programming, even if late (I'm already 39 years old) I decided to follow my dream! We will see how far my passion will take me!

No responses yet