How To Use Unity's NEW INPUT SYSTEM: SUPER EASY [Gamepad Joystick & Buttons!]

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
are you tired of trying to create a player controller with unity's new user input system by digging through the convoluted documentation and scouring through 30 minute to hour long videos well this tutorial is meant for you since i'll be showing how to install the input system how to set up the input system and show movement with a joystick and an action with a button via script in the shortest time possible so no more time wasting let's get moving upon oh so intended have your unity project open i'll be showcasing the process within the 2020 lts version but this should be applicable in the 2019 lts version and 2021 beta versions as well to install the new user input package click window package manager select the packages drop down choose unity registry find input system then click install once the package is finished installing it will ask if you want to enable the back ends which is needed so select yes and unity will restart automatically the package manager should be automatically opened up once unity restarts it is okay to close out of it i'm going to create a folder called input system in this folder right click create input actions then i'll give it a name input system just name it something that makes sense to you now it is time to create our action map so double click the input action item we just created a new dialog window will open with three columns a quick breakdown of these columns is as follows the action map is essentially the container of actions that the player will have at their disposal depending on which map is active a player may only have one action map active at a time this is useful for example in a game like grand theft auto where you can run around jump shoot guns but then you can get in a car and drive around if you use the same action map for both scenarios you could end up with user inputs that overlap such as the jump button would be the same as the accelerate button and that would cause conflicts actions are just a container that holds the input bindings for each action and the properties can be used to determine how the inputs can be interacted with and what type of outputs are generated by the user inputs if none of this made sense to you that is okay once we create our action map it should make more sense let's create our action map click the plus sign in the action map column which will immediately ask for a name and i'll call it player on ground this can be renamed by double clicking on the name an action will automatically be populated which i will rename to move which can be renamed by double clicking the name or right mouse clicking and selecting rename you'll notice next to the action's name there's a small green rectangle this green rectangle will be next to all the actions inside of your action map with our action selected you'll notice the properties column has new options for us action type is fairly important to understand and the nitty gritty of it is a button is meant for cases where an action occurs with a single button pressed and is only triggered once passthrough will accept all inputs and update according to all the input values and finally value attempts to take the strongest value from all the inputs and once it determines the strongest input will ignore the other inputs this is useful if you have a game that can be played with either gamepad or keyboard and mouse and you want to avoid the player from being able to use both at the same time to get an advantage i personally use the action type value for most actions and for the control type drop list you'll notice many options which is essentially asking you what type of output do you want the user input to give in our case for movement i want the output to be stick which just means it will track the stick's value that we bind to this action and give us values accordingly we don't need interactions or processors at this point it is time to bind or assign our input to our action again we are focusing on using a gamepad so make sure you have a gamepad connected to your computer you should notice that there is a arrow next to your actions name click this a new options drop below and you'll notice there is a blue rectangle next to this this is known as the binding option select it and notice new options appear in the properties column the path drop down can be used to manually search what type of binding you want this action to have if it has not given you a drop down selection option you need to click the letter t icon on the right hand side a nice feature included in this package is if you click the drop down button then select the listen button you can push any button or move any stick and it will auto populate inputs that match what you touched in this case i want my left joystick and once it populates it gives me two options the left stick for gamepad or left stick for xbox controller i have noticed that selecting the left stick for gamepad generally works well for a generic option and easily carries over for multiple controllers save the asset found at the top of the dialog window honestly i'm sure most of you already knew how to do that portion since most tutorials cover that section very thoroughly however this is where the difficulty spikes a bit and that is because you now have a minimum four options about how to handle the receiving of the input data and how to manipulate it i plan on showing you the easiest option of handling this in my scene i have a 2d sprite acting as my player but a 3d object could just as easily work i have a 2d rigidbody component attached and a 2d collider i'll make sure my rigidbody component has gravity set to 0 and i'll check the freeze rotation option this is just for my example you can have these settings how you like for your project if you know what they do we are now at the point where we need to decide how we want to collect our user's input which for us we are going to use a component called player input on our player attach a player input component click the drop down arrow to see all the options available for the action's input field click it and select our input action item currently we do not need to fill out the ui input module or the camera field for the purpose of this tutorial the ui input module will be important when you start including ui elements to your game which if you'd like to see a tutorial incorporating this let me know down in the comments as for the camera input field this is important for multiplayer which we don't need you may have noticed the drop down options for the behavior field we are going to keep this as send messages this is essentially acting as a unity event and will make our lives much easier there are three other options but they start to scale up in complexity which is what we are trying to avoid the method i'll show you will be more than sufficient to be able to get the project up and running i want you to notice the small box full of text below the behavior field these are method names we can use in our code click off the player object and then click back on it you should have noticed a new text appearing in this box it should say on move which should sound familiar since our action was called move the player input component automatically creates an on method for any action we have in our input action item remember this method name since this is what we will be using in our code to get player input data time to go over how to actually code the controls so start off by creating a c sharp script which i'll call mine player controls with the script open i'm going to delete the start and update method since we don't need these for our script we need to access a new library so at the top of the script add in using unity engine dot input system since i'm going to be using unity's built-in physics to move our player around we need to gain access to our player's 2d rigid body we can achieve this by creating a serialized field tag then creating a private rigidbody 2d variable which i'll call rb2d i want to be able to easily adjust our player's speed so create another serialized field tag then a private float called speed we need one more variable which can be a private vector 2 called move input value this will store the value we get from our player's input remember that method name i asked you to hold in your brain well it is now time to use that let's create a new method called on move and in the parentheses we need to create the parameter input value with the name and value the name can be anything but i prefer to use value this method we created will now be invoked every time the player is moving the stick on our gamepad which will pass the vector2 values through our parameter that we called value so to store the value type out our private vector2 we created earlier and set it equal to the value dot get less than vector2 greater than parenthesis and semicolon we can put a debug.log in this method as well to quickly check to see if it is working now we have the data from the user input which is a vector 2. a vector 2 gives us two values an x value which would be the horizontal position of the joystick and a y value which is the vertical position of the joystick these values can now be used in your typical movement method in this case i will be showing how to do movement with rigid bodies create a new private method which i'll call move logic method let us create a local vector2 variable which i'll label result we will set this equal to move input value multiplied by our speed value multiplied by time dot fixed delta time we multiply by speed to give an easy method of controlling the speed via our inspector and we multiply by time dot fixed delta time to help smooth out the movement since we will be calling our method in fixed update which is meant for physics calculation we now have a vector 2 which can be used to set our player's velocity to do this call our rb2d variable dot velocity equals result the last portion of the script is to create our fixed update method which is meant to update after a predetermined number frame so the more frames your game is able to produce the more times this update method will be called with this created call our movelogic method function and save the script go back to your unity window apply the script to your player object set the speed value and drag your player into the rb2d field start your game and test the left joystick should be moving your character around if you are using this in 3d remember that you are dealing with three dimensions and the x and z are typically the axes that player movement is based around we now know how to collect input data from a joystick but how about a button well this is a very similar process to what we just did double click our input action item and create a new action by clicking the plus sign in the top right corner of the actions column i'm going to name this new action button regular with our new action highlighted in the properties column change the action type to button let's create another action and this time call it button hold for this button hold action in the properties make the action type button and now click the plus sign next to interactions you'll see five different choices which i'm going to select the interaction type of hold all this option means is the player has to hold the button for a specified time frame before the action occurs the interaction types i have seen the most used are hold multi-tap and tap these are pretty self-explanatory and have decent unity documentation i'm going to leave the properties of the hold button as default values but feel free to mess around with these lastly assign a binding to both of our new actions which i'll make mine both the gamepad south button and then save the asset for further clarity click on your player object and go to the player component review the text box that holds the different method names and you should notice both of our new actions receive their own methods go back to our player control script create two new methods based on our new actions which in my case will be private void on button regular and private void on button hold since a button can be used from anything such as jumping all the way to shooting we will just create a generic demonstration of how the different button actions will work let's create a debug.log in both methods and i'll put a message that makes sense in both logs that was all that was needed for the scripting so save and go back to unity start your game and do a simple press of the south button of the gamepad and you should notice in the console that our debug message appears now hold the button you should notice that the debug message for the regular button press shows then after the determined time frame the hold button message appears it is also good to take note that these actions only fired once while holding the button down that means the button needs to be released and pressed again for the action to occur again well that should get you all on the right track on using the new unity user input system there are a multitude of ways to achieve the effects that we created with the possibility of even more control on how the inputs can work however with more control comes more complexity and this is meant to get you up and running as soon as possible with as little headache involved and honestly the amount of control this offers is something i find very appealing for such a convenient package easily binding multiple inputs to a single action with this all said if you found this useful please leave a comment below letting me know what you were able to achieve using this new user input package i want to thank you for watching and as always stay safe [Music]
Info
Channel: Seth Funk
Views: 12,715
Rating: undefined out of 5
Keywords: how to use unity's new user input system, new user input with joystick and button, tutorial, easy and simple, Unity
Id: cWAdUrw2bXQ
Channel Id: undefined
Length: 12min 17sec (737 seconds)
Published: Wed Sep 29 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.