New Unity Input System
Unity has changed the input system with a new and improved version. Going forward we should use this version when creating an Input system for our games. To get acquainted with it let us head over to the Unity documentation and check out the new unity input system. Follow the link below.
Quick start guide
Note: For information on how to install the new Input System, see Installation. The quickest way to get started in…
Following the quick start guide we will be able to install the new Input Sytem. We must go within Unity to the package manager. To access this go to Window >Package Manager. Select the Input system from the package list and click install.
After the installation you will be prompted if you want to activate the new system. If you had the old system in place it will be disabled and no longer work. Especially in the case if you are working on converting over a project from the old system to the new.
Note: If you would like to use both the old and new you can re-enable the old by going to edit>project settings>player>active input handling> both
.Net framework = .Net framework 4.8+ standard 2.1 when selecting the API Compatability level and IL2CPP for Scripting backend in newer versions of Unity. I’m using Unity 2021.2
First we are going to create a folder called Input under the assets folder. Then under the input folder we are going to right-click>create>input actions and name it (player input actions).
When complete open up your player actions.
Within in this all of our players actions are stored. Here we are going to create our action map.
So where the + is next to action map we are going to click that and name it Player
We added a car and flight as examples of actions that could be taking place during these conditions. For example if you are walking as the player certain keys have certain controls, but if you are flying or driving they would react differently.
We are now going to create our first action for our player. In this case it will be to fire a shot. So we are going to press the + under action and rename it to fire. It will auto create a binding, but we are going to delete that. Next we need to figure out what type of action we are going to have.
If we select button action it would be as assigning the space key to fire.
Now under our action we are going to click + icon and select add binding and select the space bar and if you would like another key to fire as well add another binding. When done editing make sure to click save asset.
Now to get more familiar with working with action maps we are going to imagine we are creating a game that focuses on animal behaviors.
So we are going to create a new action map named dog. We are going to create 4 actions for the dog. 1 the bark action using the space key. 2 Walk action using WASD. 3 Run using the Left shift hold down for more than 1 second. 4 Die using D+F at the same time.
For the 1st one we are going to replicate what we did above. We are creating a new dog action map. From there we are creating a new bark action and we are going to assign the binding to the space bar.
2nd we are going to create value type. This will give us a control type which allows us to use multiple buttons. For this we want a control type of vector 2 as we are going to be walking along the X,Y plane.
You will have the option when you click + to add an up/down/left/right composite. From there you will assign the buttons to each direction.
3rd we are setting up our run action. So we are going to add our run action, and bind left shift from the keyboard to it, but we need to make sure it does not trigger until after it is being held down. This is done by going to the interactions tab and adding a new interaction. Select the hold interaction and change the default hold to 1 second.
Finally our dog has to die (yes sad I know). We are going to create a new action for dying. For this we are going to have to use multiple buttons to complete this action. For this we are going add a button with 1 modifier composite. So are binding is the D key, while are Modifier is the F key.
Now we are going to focus on the scripting side so we can use these buttons in our game. For without direction pressing these buttons nothing will happen.
Now we need to generate a C# class based upon what bindings we created. To do this go to the inspector and select the Generate C# Class. Check the box and click Apply. This will create a player input actions script. If we add to our actions list it will automatically update the script.
Next we are going to add a cube to our scene by going to hierarchy>right click>3D Object>Cube. Then we are going to create a PlayerInput Scipt and we are going add it under the Input folder. When complete open the script by double clicking.
Once opened we will have to add the UnityEngine.InputSystem library to access the input system. To get our actions we will need to complete 3 steps
We are going to create a reference from PlayerInputActions and call it _input. Then we are going to on game start our reference. Then we are going to enable our instance of the dog. Next we are going to say we performed our action. Save and go back into Unity.
Back in Unity make sure you attach your PlayerInput script to your player object. Press play and press the space bar.
Now we can see when you are done pressing the space bar.
To do this we are going to use the cancelled function.
Started let’s you know when you pressed the button.
We are now going to expand upon our actions. So we will be going back to our action list and get our walk input working via our script. It is very much like what we did with the bark action.
Now we are going to work on our run. We are going to use the performed option, because we do not want our action to occur until 1 second has passed.
We also are going to add a cancel to see how long we are holding our shift key.
Finally we have our Die command.
NOTE: If you have a key linked to 2 actions both actions will occur. To avoid this problem do not have 2 keys linked to the same button even if a composite is required to complete an action. We recommend changing the key from D to G.
Now we are going to have our player move when the WASD keys our pressed. We are going to create a new action map for this and name it PlayerInputActions1.
Replicate what you did for walk for the dog and generate a c# script when complete.
Next we are going to open up our PlayerInputActions1 Script and create a reference and start new reference and enable it just like before. We will create the condition that the action was performed.
Now we are going to Debug.Log to get the context, but we are going to use a different format to get the X,Y values.
Save and test and you will see you will get the values for the X,Y using the WASD.
Now to get our player to moved based upon the direction we tell it we are going to create a new variable move and it is based upon the context we read from our keyboard. Then our cube’s position is going to translate based upon the move*Time.deltatime*speed, but that is not the best way to have a smooth movement system.
To move smoothly we want to go into our Update method and get our move variable read the values and do our transform.translate.
Random Color Assignment
On our cube we are going to assign it a random color. To do this we are going to create a new action map. We are going to replicate the steps we did before when creating a new action map and generate a new C# script when complete.
When complete open up your player script and we are going to add a few new references. We will also need access to the mesh renderer of the player. We will reference and start the ColorChanger as we have previously. Then we will acess the MeshRenderer and get the component. When we perform our action a random color is assigned to the render material.
Now we are going to rotate the cube using the A & D keys. If A is pressed rotate to the left and if the D key is pressed we are going to rotate to the right.
We are going to add to our action map and add a rotate action. We are going to give it a Value action type and a Control Type of Axis. We are then going to create a binding of a 1D axis with a positive and negative. For the negative value we are going to use the A key and negative value we are going to use the D key. We are going to create another 1D axis that uses the arrow keys. Left for negative. Right for positive.
In our Update function we want to add the code below. We need to create a variable that tells us our rotation direction and we are going to read that value. Then we are going to use our transform.rotate to rotate our cube at a speed of 30.
Action Map Swap
Now we are going to create an action map for Driving and switch to it via the ‘T’ key. We should be able to move in 3D Space.
First we are going to create a new action map called driving. Under Player we are going to create a new action called driving state mapped to a button and bind it to ‘T’ key and click save when complete. Now we will head over to visual studio.
First we are going to add a reference to us being able to perform our driving state. In the driving state performed class we are going to switch to our driving map. So we are going to disable to Player map and enable the driving map.
It’s now time to start setting up the driving map. So create a new action called movement that is a value 2D vector with an up/down/left/right composite. Set your key values to the bindings for WASD
Next come down to the update function and we are going to create a move variable where we read our vector 2 and then translate that position to our cube.
Now we are going to create a bouncing ball in our level. First we need to create a plane and lower it. Then we are going to create a sphere with a rigidbody component add on to it. Now we are going to create a new input action and call it bouncing ball.
We are going to open up our new input action and call the action map player. Our action will be called jump and our Action property will be a button with a hold interaction with a hold time of 1 for a long jump and for a short jump we will use a tap. When complete save the asset and generate the C# script. Next we are going to create a we are going to create a C# script called BouncingPlayer.
We are now going to attach our script to our sphere. There we are going to get a reference to our bouncing ball and enable the player. We are going to see when the action was performed and canceled. If the action is canceled we are going to do a light jump, but if we hold it we are going to do a full jump. We are also going to check if the full jump was performed.
Using the old input system we would use input.getkeydown(key) to test with in the new version of the input system we would use (Keyboard.current.spaceKey.wasPressedThisFrame) if we wanted to test the spacebar to jump for example.
If you have ever played a mobile game with a virtual joystick on it this will be a quick tutorial on how to create that.
The first thing we are going to do is create an UI Image and change the image to a circle and rename the image to joystick. Then we are going to add a component to our player called On-Screen Stick. The movement range lets you decide how far you can pull in any direction. For control path select joystick>android>stick
Click the EventSystem and replace with InputSystemUIInputModule. Create a new input action called Joystick. The player action map will have an action called movement with a value action type and a vector2 movement type and add a binding joystick>android>stick. Save when complete. Generate the class when finished.
Now create a new joystickPlayer C# script. As before we will get a reference to our action map and action. We will create a move variable that reads when our player moves it performs a translation of the players position along the X axis.
Save your script and add it to the Player. Press play and your player will be able to move along the X using the joystick.
Progress Bar Challenge
For this we are going to create the ability to hold down the space key to charge up a progress bar. When you release the space bar the progress bar goes down. The first thing we are going to do is create UI>SliderUI. You can adjust the height and size if desired. Open up the slider dropdown and delete the handle as we do not need it for this exercise.
In the fill area change the right value to 5 so when we fill our bar it the padding does not create extra space.
Next create a new action and action map called slider and an action called charge bar with a button control and an interaction of press. The binding we will be using is the space key. Save and generate when complete. Create a chargebar C# script and open it up.
Now as before we are going to create our reference and and enable our action. Then we are going to find out when we started and finished our action. We also need to create a reference to our Slider component so we will have to add the the UnityEngine.UI and a slider variable. We will serialize the field so we can add the slider in our editor.
We are also going tp create a bool asking if we charging the bar. We will create a coroutine with a while loop saying while we are charging add 1 to the slider over time.delta time and when we are not charging subtract one and divide by how long you would like it to take to charge and uncharge.
Now we are going to show how we would use this in a practical application of New Input System. We are first going to create a new Input Action called GameInputs. We will have a player action map and a movement action with a value action type and a control type of vector2 with a WASD key binding. Save and generate as before. Create a new C# script called PlayerPract and attach it to your Player. Create a new empty gameobject called PlayerManager and attach player as the child. Create another C# script called PlayerManager and attach it to the PlayerManager. Open the PlayerManager script. We will get our reference and enable input as well as create a reference to our player so we can attach it in the editor. We will then create a move variable and read the movement data input on to the player.
Open the PlayerPract script and create a float for speed. We will call the move method and transform our players position based upon that move data that we read over time and speed;
We are now going to add our fire method to our player. We will go back to our action map and add a new action of fire with a press interaction using the space key.
In our PlayerPract script we are going to serialize a projectile and instantiate that projectile in our fire method.
In our PlayerManager script we are going to make sure our action is being performed
Next back in Unity create a capsule and reduce the scale to .2 for the X,Y,Z. Rename it to projectile and create a prefab out of it. You can create a prefab folder to place prefabs in. Delete the projectile from the Heirarchy and add the prefab to our player under the projectile location.
Next we are going to create a new C# script called Projectile and attach it to our projectile.
We need to do a few things here and the first is give our projectile a speed as it moves up the screen. Next when it moves off the screen it needs to be destroyed.
Third we are going to go back into our GameInput action and on our fire action binding we are going to add the press interaction. Go back into our PlayerPract script. Here we are going to add a fire delay and a can fire. Here the player can fire if the time between shots is greater than canFire.
For this last part we are going to create an ammo system for our shooter. We will only have 3 shots available. In the playerPract we will add an ammoCount =3 and in our if statement we will make sure we have more than 0 ammo and if we shoot we will subtract from our ammo.
This is it guys and this is an intro on how to use the New Unity Input System.