Unity new Input System : Player Input Component (Part XII)
This is the last article on the new input system, and we talk about PlayerInput component.
The reference to the PlayerInput component is here:
The part we intersted in for this article is this :
As we can read above, this component doesn’t do different things, it’s just an “integrated method” to create the same function as when we code.
But it’s good to know that it exists and how to implement it.
As always we need the Input System from Package Manager
Window -> Package Manager -> Input System
P.S. Remember to change “In Project” to “Unity Registry” or you not find it.
Ok, now we create a simple Cube in our Scene and called it Player
Now, what we need to do is simply add the Player Input Component to our Player
Inside Scripts folder, I create another folder and I called it InputSystem.
Now, in the Player Input component, we select Create Actions
After we press it, we select InputSystem folder as destination to save it (I called it as always, PlayerImputActions)
and drag and drop it into Player Input component
Double click to open it.
As we can see, with this path when we open it we have already some things set up.
For example, if we go to Player -> Move, we can see that we have already some implementations
And even the UI
The next step it, and it’s really simple, is going to Player Input component, select Default Scheme -> KeyboardMouse
Then go to Behaviour, and change
“Send Message” to “Invoke Unity Events”
Now we need need another script for Player.
We can call it PlayerController, PlayerActions, PlayerInput, as we like
And we need to add using UnityEngine.InputSystem
Now let’s write some line of code:
first we declare a Vector2 moveDir.
Then we need two method :
We create OnMove() with InputAction.CallbackContext parameter to read the value of Vector2, and Move()
Inside it we have directly implementation of moving with transform.Translate(), and we call it in Update
And we need a float speed variable to multiply in Move()
Now we are ready to go, in the next article we finish this implementation!