Scripting Actions (New Unity Input System)
Now that we know how to create action maps, let’s take a look at how we can go about scripting our actions. First let’s create a C# class for our Input maps so that we can call upon it within our scripts:
Next, let’s get it set up so that we can create a debug.log event when we want the dog to bark. Before we are able to do anything with the input system, we need to make sure we have our library enabled in our script:
As we can see, when we are implementing the action and telling it to be performed, it signals us after entering += that we can press tab and it will create the method for us. From here, we can just fill in our debug.log:
With our debug.log set up, let’s play it in our editor and see if we get the interaction we are looking for:
Now, let’s continue with the other actions that we have created and try activating them all within our editor:
As we can see, we have all of our actions being recorded in our console. Next up, let’s see how we can go about creating some actual movement with these actions on our object.
First, let’s clear our the code we have in place and work on strictly the movement method:
What we have done here is set it up so that we can see the exact value when pressing WASD rather than having to read through a lot in our console:
Now that we can see the values for each of the buttons let’s try using the transform.translate method within the movement void we have:
As we can see, our object is moving, but we need to spam click in order to move. In order to fix this, we will want to run our code within update like we have done with previous projects:
As we are doing it this way, we don’t need to enter the performed method within Start. We can just create it’s own void and call that void within Update. With this code in place, let’s take a look at how it acts within our editor:
Now that we have movement along our X and Y axis, if we wanted to adjust it so that our movement was along X and Z instead, we can just alter our code as such:
Now that we have successfully implemented movement within our project, we can continue to play around with this new input system and try creating different actions to see how we would go about coding it into our projects.