How to use Unity's New INPUT System EASILY

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
so unity's new input system has been around for a while now a couple years but it's not immediately obvious how you access it i mean i have the 2020.3 version of unity installed and it's still not included in the default settings so whether you're starting from scratch or you have a project already using the old input system i'm going to show you how you can go ahead and convert to the new one so let's take a look at this project here so right now in this project i just have a player you can move in the x and y axis and here's the simple script for it we're just getting the x and y inputs and then we're basically just moving our rigid body in those directions but i want to focus on these two lines here where we say input dot get axis horizontal and vertical this is the old input system and we're passing in this string of horizontal and vertical and if we go to our project settings in the editor and go to input manager we can expand this axis carrot and you'll see a horizontal and vertical inputs here and if we expand horizontal you'll see it has a d left and right and vertical as ws and up and down arrow keys so this is how it was done before you would configure these axes you would give it a name and in script you'd have to use these strings verbatim if you spelt it wrong or use the capital wrong well you were screwed and if you're new this could be an annoying reason why something is breaking so as they say out with the old in with the new the first thing we want to do to move over to the new input system is to get the package for it because like i said before it still doesn't come with unity so what we can do is on the top we can go to window package manager so depending on what version of unity you're on it might default your packages to in project if you expand this drop down in the top left but the input system is found under unity registry so we can go to unity registry and we can type in input and you'll see the input system comes up we just want to click this and hit install and when it finishes installing you'll probably get this warning telling you more or less that it's going to disable the old unity input system and it will set it up in the back end so you can use the new one because you can only use one at a time so here i'll say yes and it'll restart quick and when it launches back up you'll see that it is now installed this might vary from person to person but this is an important check if you go into a script and at the top you write using unityengine.input and nothing comes up with input system or anything like that then you need to regenerate your cs projects in visual studio so in unity if you go up to edit preferences and then external tools you'll see this section generate cs proj files for and in my case you notice registry packages is unchecked which is where we downloaded the input system from so i need to make sure that it's checked and i need to regenerate project files then i should be able to go back in my script and type using unityengine.input system and that should come up so back in our editor let's take a look at a few ways we can go about implementing the new input system we can actually click on our player and if we go to add component we could type in player input and here you'll see there's a bunch of options most importantly it says that the actions field is empty and so we could go ahead and hit create actions and just save it as something like player controls and you'll notice this asset it's now in our folder and if we double click this and open it up it actually pre-populates us with a bunch of different things so we have action maps for a player and a ui which could be like enemies or something like that and if we click on player we can expand let's say the move and there's a bunch of different types of things here there's you know the left stick if you're using a gamepad you can expand wasd and you have your up key your w key you know like your standard keyboard movements here you can expand fire and you see right trigger left button primary touch tap etc so you get a whole bunch of baked in common controls inputs for free more or less when you build this out and i encourage you to take your time to look through this but if you think that might be overkill for you or it's a little overwhelming at first you don't need to start with that there's actually much simpler ways of doing this so i'll go ahead and delete those let's focus on just converting the simple movement script into using the new input system so with our using statement at the top of the script we can go down here and say public input action and call it whatever you want maybe player controls and back in the editor if we go to our player controller script you now see player controls as a variable and so if we hit this plus sign there's a bunch of different things you can make but in terms of capturing a horizontal or vertical movement what we want to do is add a 2d vector composite because this translates to a vector2d and similar to the example i can call this wasd for keyboard controls and we can now go ahead and configure these different commands so for up we can double click this and in the path i can press w and you'll see w keyboard or you can also make this whatever you want like the up arrow key i'll go ahead and just use wasd for now i'll set these up really quick and there we go we now have our wasd controls set up so back in our script here the first thing we want to do is initialize this input action so to do that what we can do is void on enable which is a built-in unity event and in here what we want to say is playercontrols.enable and then conversely what we want to do is void on disable and say playercontrols.disable this is a requirement of the new input system if you don't put these two you'll get a lot of issues so now let's talk about how we actually read the inputs we can go ahead and i'll just comment this out for comparison sake but we don't need that anymore and this move direction here is just a vector2 so what we can say is playercontrols.readvalue of type vector2 and the input system should be smart enough to understand we're trying to pull that wasd value in fixed update i'm just saying the velocity to the x and y of move direction times the move speed which in this case is five but without making any other changes just this one line when we play the game i'm now able to move my character again using w a s and d so with this approach it is much simpler all you need is an input action but an action is really like movement or interact or fire these are very specific actions so this would more accurately be called player movement and then you would have another introduction for say player attack or player interact and you would make separate input actions for each of these which is a totally fine way of doing it and you'll have more control but it is a limited way of using the input system this will get more complicated but they do have an entire event system built in with the new input system so let's just cover that pretty quickly so let's go back in the editor and you could right click on your assets folder and go to create input actions and double click it and you'll see this familiar setup from before but i'm going to go ahead and leverage the one that unity already populates for you and so to do that i'll go to my player i'll add a component i'll do player input and i'll select create actions i'll call this player input actions and then i'm just going to remove this player input component for now okay so if i open up this player input actions this is where you have the player in the ui from earlier and it has everything pretty much set up for you i'm just going to use this for the example you'll notice if we click this on the top right there's a generate c-sharp class option we want to select this and hit apply and it will create a c-sharp class wherever your input actions were located so now in our player controller what we could say is instead of input action we could say player input actions which is the name of that script that was just generated and in here what we want to do is add a void awake method which is another unity event so in this awake method awake gets called as the game is loading and in here what we want to do for now is say player controls equals new player input actions which will just initialize the script so if you actually open up this player input actions and scroll through it's pretty scary if you don't know what you're looking at here but the good news is you don't really have to mess with it so if we open up this input map you'll notice we have a few actions we have move look and fire i'm just going to focus on move and fire for now so what we can do is make a private input action called move and a private input action called fire and enable what we can say is instead of we could say move is equal to our playercontrols.player conversely you could also do ui as those are the two action maps in our player input actions but we want the player and we can say move then we can say move that enable in our on disable we can simply just say move dot disable instead of setting the move direction to the player controls we can set this to move and once again our game is back to working i can make a private method here private void fire and in here what we want to say is input action dot callback context and you can call it whatever you want but context seems to be conventional and just to test this right now all i'm going to do is debug.log we fired but this could be like your shooting logic or maybe your interact logic to pick something up this is where it would go back in on enable what we can say is fire is equal to playercontrols.player.fire well you can initialize this by enabling it and now we want to register that fire method to the event and to do that is very simple we can simply say fire dot performed plus equals fire and then on disable we simply want to say fire fire.disable as well and within this fire action we see that the left mouse button should trigger it so if i play the game and i click we notice in the console it says we fired so at this point it is registering every single time we click the left mouse button and this would be true if you hit space or any individual button as well as long as you had it configured in this input map but that's not all if you keep clicking the left mouse button over and over and you actually just go to my youtube channel and click on the subscribe button well then you subscribe to me do it right now [Music]
Info
Channel: BMo
Views: 1,978
Rating: undefined out of 5
Keywords: bmo, unity, tutorial, unity tutorial, input system, input, control, controls, player, player controls, player input, c#, event, beginner, game development, game dev, video games, unity input system tutorial, unity new input system, unity player controls, adapt new input system, unity new input system tutorial, unity input system, unity controls
Id: HmXU4dZbaMw
Channel Id: undefined
Length: 9min 31sec (571 seconds)
Published: Mon Nov 22 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.