Unity new Input System (Part IV)
--
In this article we see how set it up everything we seen before into the code!
First we need a C# script.
To do this, left click on PlayerInputAxtions
Toogle “Generate C# Class”, and press Apply
The Class we just created is directly synchronized with the PlayerInputActions we have been working on so far. Any changes we make will automatically update the Class.
Before we move on, I create a simple cube in te scene, called “Player”
Now, how can we interact directly with the Input System after we made this set up?
First, we need another C# script.
I called mine PlayerInput
And open it.
The first thing to do is add
using UnityEngine.InputSystem
Now, we need 3 different steps to use it :
1. Take a reference and start the instance of Input Actions
2. Enable the input action (for a dog in this case)
3. Register perform function
As we can talk about previously, this system is based on events.
Even if you don’t know exactly how works, we try to stay easy and explain every step.
First step, take a reference of InputPlayerActions
Second step, Initialize it
And, always in Start, we enable the Dog control
If we now prove to access the Dog -> Bark and performed
We see the little lightning over performed, that it means is an event.
If we add the += sign
Intellisense automatically understands what we’re trying to do and if we hit Tab we now have a new method called Bark adding to our script.
To better understand what happen here, let’s try a classic Debug.Log
If we press Play and then the Space Bar
In this way we have implemented the “Bark” method, in the next articles we will implement each single action.