How to use Unity's Input System

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
today i'll be showing you how to use unity's input system it's been out for a while now but it's only available through the package manager so make sure you have it installed by going to window package manager and then go to packages unity registry and here we can see all of unity's packages that we can download and if you scroll down to input system or you can search it in the search bar here then on the bottom right there's an install button you can also open it up and see other versions so you can download previous versions or if you want you can go to the settings icon up here advanced project settings and you can enable preview packages if you'd like to download a preview package so if you enable that you'll see that now all the preview packages appear and these are the latest versions that may contain some fixes or extra features so if you see that something's not working in a previous version you might want to upgrade and it might be fixed on the newer version you can check the docs for more information about version features so i'm just going to install 1.0.2 install then here click yes it's going to restart the editor and restarting the editor installed the input system into our project one last thing i want to mention is that in the input system they have some samples you can download if you want to see how something's done rebinding ui on-screen controls custom device simple multiplayer so exit out so there's a couple ways how to use the new input system i'll be showing you some of them and then i'll be telling you which ones are the recommended ones so what's good about the input system is that it separates the input and the device so it's kind of an abstraction layer for your input and that makes it easily transferable between different pieces of your code so in this case you can have your input in a central place and then have other scripts access it easily if they need that input information and what's great about the input system is that it's very cross-platform friendly which i will show you in a moment why it's so cross-platform friendly so there's a couple ways to use this first i'm going to make a scripts folder right click create scripts and then what we can do is right click and create an input action asset and then we can name this so this is where our controls are going to be and our actions so let's say we want our player controls here and so if you press enter or double click that this will pop up so we have some stuff going on here the first thing that you need to do is add an action map and an action map will be a grouping of actions that we want our player to perform so let's say we add an action map here and let's say we want this player to perform certain actions on the land so you'd have an action land map and let's say you have different controls for another area such as a water area then you can create another action map and call that water and then you can switch between these two action maps depending on what section of your game you're on so i'm just going to right click and cut this so if you click your action map you see your actions so this is what you want your player to be able to do so for example if i press f2 here i can rename this move we want the player to move if we add an action up here with the plus button let's say we want the player to jump and let's say we also want this explosion effect explosion so these are our actions that our player can perform however we need to bind controls to it or else unity won't know what control will activate this action so if we click the jump one for example on the right you see we have this action type there's three that we can choose from currently it's a button which this is perfect for jumping because it's just one button that we want to press however there's also value and pass through if you click value for example you'll see we have a control type here and value lets you select any one of these types so let's say you want your action to be a type of vector 2 which will need to do that for our move then you can select vector 2 and this action will return a vector 2. so you can choose any one of these that you may need integer quaternion touch for touchscreen axis and this returns a range of numbers on one axis so in this case let's just choose button and if you're wondering between value and pass value does a disambiguation process which if you have multiple controls connected at the same time it basically selects a main one while passthrough doesn't select the main one and just receives all the input so in most cases you'd want value because the player would only be using one control at a time usually and would need to select a main control so for now i'm going to select button and if we go to no binding here we can select a path for our binding so here we can type it in directly let's say i want the space bar i can type it there or i can press listen and then i can press it on the keyboard and click it there as well now this is one binding so whenever this is pressed this action will be executed and what's good about the input system is that it's event based so the input system supports certain events it has a started performed and cancelled so when your action has been started so in the case of a button when we start to press it down it'll issue this callback called started and performed and we can subscribe to those callbacks basically we listen to the input system and listen to when these are executed and then we can do certain things depending on when these are executed and then there's cancelled which means we have lifted our finger from the button and so depending on the type so value button or pass through these callbacks might be issued at different times i have a whole video on this if you're interested the link is in the description but what's cool here is that if we go back to the jump we can add another binding here and now for example we can add a gamepad binding and let's say we want the button self of the gamepad to trigger our jump so gamepad is just like a controller or if you want to be more specific with your gamepad instead of a gamepad you can add an xbox controller directly so you can add the a key of the xbox controller if you'd like and pressing any one of these will trigger this action so if we go to move now we can set the action type to value and the control type to vector 2. so this is a little bit different for keyboard for example if we want to use a wasd or arrow keys there's no real way to bind one key to a vector 2. so what unity has done here is that you can press this add binding here and we can add a 2d vector composite and i can just name this wasd i'm just going to cut this one so a composite here is a grouping of different bindings and you can add in a binding for each one of these so if i press listen w on the down i can go to path listen s left path listen a and write listen d so now i have the wasd mapped here and what it does is that the composite sources these values so it listens to every one of these and then combines them into a vector 2 so that you can access it correctly so in this case up and down is on the y-axis of the vector 2 and left and right is on the x-axis if you're pressing left it returns negative one on the x and if you're pressing right it returns one on the x of the vector two and if you're not pressing anything it returns zero so what's cool here is that you can add another 2d vector composite and let's say you want arrow keys to also activate this move action then you can bind these arrow keys here listen up arrow down arrow left and right and once again we can add another binding and this time we can go to gamepad and add in one of the sticks which is one of the joysticks on the controller so let's say we also want to move with the controller left stick so what's great about the input system is that we can easily support cross-platform controls by just quickly adding a binding to our existing actions and the code that we'll use to access these doesn't change whether it's on pc or on playstation or controller unless you want something specific for platform and for the explosion i'm just going to add a binding of e here one thing i want to mention before we continue is that there's something called interactions and processors with interactions let's go to the jump here the interactions will basically determine when these actions are executed so for example we have a hold interaction we'll have to hold down the key for a certain time so hold time which is 0.4 seconds and only then will this be performed you can deselect the default value and put in how long you want there and the press point is basically how hard you need to press it which for a keyboard button doesn't really matter because it has no sensitivity but for a controller you'd have to press it past the halfway point of a trigger for example in the back of the controller there's the triggers that change values from zero to one in this case you have to press it more than point five additionally you can open the input settings and create your own settings asset to set some default values for the input system here and then for processors these just process your values after they've been received so for example if you want to clamp invert or normalize this value or scale it or add a dead zone to it then you can do so here and i have some videos on this if you're interested and last thing i want to mention is that if you have a float value for example a button is a type of float since it only returns one value you can add a axis here or you can add a modifier which i have a video that goes over this but it's kind of like a combo where it'll be executed if you press both of these buttons at the same time the button and the modifier and once you're pressing both of these it'll give you the value of the button so this would be good for sprinting with the button being w and the modifier being shift as an example additionally if you want more control over the different devices you can go up here no control schemes click it and add a control scheme so let's say for example you're making your game for mobile and for desktop you can have a desktop control scheme and here you can add in the required devices that you need for this input to work so for desktop i need a keyboard and i'd also need a mouse and you can make these optional or required and once you save your control scheme you can access it there and then you'll see that once we click a binding there's an extra option here use in control scheme so for our space which is on our desktop we can click that there and this can be used to require certain devices for certain control schemes so the player needs to have a mouse to play this game then you can set a required field on the mouse for a control scheme corresponding to desktop or pc for now i'm just going to delete this control scheme so up here click save asset to save your asset and now we have this asset with our input and now we actually need to use this input so there's several ways that we can do this so the first way is generating a c class of our input so if we click here this will generate a c sharp class that we can access here you want to make sure your file name and your class name are the same so by default it's the same you'll see playercontrols.cs and our class name is playercontrols and you can add in your own namespace here if you'd like so just click apply here and you'll see that a script will appear down here if you double click it you'll see that there's a lot of stuff going on here these are the actions that we made in our input action asset and they've been converted to a type of json format so you can see we have our map our land map and our move and jump and explosion here and here are the bindings wasd up down left right and it just goes on and here they're just accessing some of your action maps and actions and these are some of the functions that you can call if you have a reference to the script you see you can also add a mask to your binding which you can use if you want your binding to be visible in a certain action or map or asset but you don't want it to be visible in another map which you can find more information on the documentation for this but you see here that it's just constructing these functions that we can use these are the events that we can subscribe to started performed and cancelled and it just uses some sort of wrapper on the input system additionally they create this interface that you can inherit from and you can derive these functions that will be called when you move jump or on explosion so i'm going to show you this way and then i'm going to show you how to use the player input component which is the recommended way but it's very similar the code so if you right-click create c-sharp script let's call this playercontroller alright so here i've erased the functions and we get a reference to our player controls so first we need a reference to our player controls private player controls player controls so make sure you pay attention to the capitalization here then i want to instantiate these controls because this is just a reference to the class but we don't have an instance of the controls so we can do player controls equals new player controls so you'd think you're done but you need to actually enable the action map if you want to read the input from it so what i usually do is that on the on enable function in mono behavior here you can do player controls dot enable and then in the on disable function we want to make sure to disable these controls so on enable is called when the script is enabled and this is called when the script is disabled all right and so i want to make sure that these are enabled before i use them so i'm not going to put any of the actual logic in awake but instead i'm going to put it on the start function so there's two ways that we can use this we can either subscribe to the events and listen for when our input is being triggered and do something when it's triggered one time or we can continuously read our input as you usually do with the old input system so in the update function here we can do player controls dot and you can see all of the stuff we can access here so we can access the asset itself which it shows you what it returns here the binding mask of our player controls the control schemes available devices and our action map land so we can do playercontrols.land dot and then you see we have our different actions here explosion jump and move so for example if we want to know our move values we can do dot move and then we can do dot make sure to read all of these to see what you can access here you can access the bindings with the map but what we want is the read value dot read value and then what we're going to do here is we're going to put this bracket and we have to put in the type of what we're expecting so we're expecting this to be a vector 2 so we have to do read value and then vector 2. and then here we can just do vector 2 move and then we will receive our move input so we can do debug.log here and move for example similar to the move we can do playercontrols.land dot jump and then we can do either read value float this is a button so it's gonna be a float or i'm gonna copy this if you wanna check if it has jumped so if we have jumped dot jumped we can do the same thing dot read value float equals one so if it equals one it has jumped or here we can do triggered so triggered will be true on the frame that it has been performed so when we press our jump key down this will be triggered all right and you can test that out if you'd like you can do a simple debug.log and print out jump and i'm just going to comment these out here and you can make a new game object create empty then you can right click and create a empty game object on the scene gonna call this player and then we can add in our script which is our player controller script so in the console you'll see that our moves are being printed which there's a lot of values so we can collapse that so if i press wasd you'll see that now the input is changing and if i press jump you'll see that the jump is printed out alright so that's how you can read the value directly if you want to subscribe to an event and have something happen one time or maybe start a curtin when something has happened so you can have more control over it and not populate the update function too much we can do playercontrols.land.move similar to that but now we can do dot started or we can do dot performed or we can do dot cancelled let's do our explosion here dot explosion and let's do perform so when we have performed our explosion this is an event so we have to subscribe to it basically an event sends out something and then this script needs to tell the event that it's listening and to do that we do this plus equals basically saying we're listening here and then we can just call an explosion function that we need to make so whenever this has been performed then call the explosion function which we can do right here private void explosion and you'll see it's still red error because this requires a parameter input action.callback context so we can just copy that or type it here so this is the input and we can just call this context which you'll see that this doesn't recognize it and it's because we have not imported if you go up you can do using unity engine dot input system and so now we can use the functions available in the input system and the objects by importing this namespace and we can erase these two namespaces since we don't use them right now unless you use them so here this will be called when our explosion is performed here you can do whatever you want debug.log explosion or if you want to read the input you can do context dot read value similar to how we did before float since this is of type float or you can do context dot read value as button so this will return a boolean if it is pressed so if you wanted to compare this to a boolean you'd have to do equals equals one since it's a float but we can just call this function instead and additionally you can access some other parameters like when the action was started the current time the control associated with it it's duration if it has an interaction on it etc so i'm just gonna copy this here just to show you the different callbacks here and put this here so these are the three callbacks that you can use and you have to make sure to unsubscribe to this when you're done so for example here plus minus you have to make sure to minus equals somewhere in your code so you have to just make sure to do this once you're done somewhere on your code so you can do this probably on the on disable function here and if you'd like you can also put this after the on enable function it's up to you so that's basically how to use the input system you can either subscribe to the events or you can read the values directly if you'd like however this is not the recommended unity way because the problem with this is that since it generates the script the script can't really be changed easily so for rebinding keys it might be a little difficult to do it this way you could definitely do it but unity has a component that they've already made to make life easy for us so under the player we can add a component player input component and here we can add in our input action asset that we made so you can click this circle here and double click your player controls you can select the default map which is land so you see there's already some stuff built in you can select a default map this is more for if you're doing a local multiplayer game and each player has their own ui or camera you'd assign it there and the input system has a player input manager that manages the different players which is good for local multiplayer which i have a video on so this player input component has some nice functions that we can use off the bat you see that we can access this directly from our script with get component player input or we can use a behavior so currently with send messages it'll check each script attached to this game object and if it has these functions defined on move on jump on explosion either one of those it'll be called whenever these actions are being performed however i recommend doing invoke unity events instead so you can open this up and i actually don't even recommend doing this but i'll explain to you in a moment so if you open up events and land you'll see that we have move jump and explosion and we can add here a function so you can drag in your player controller as an example and then you can call a function on your player controller so make sure that this function takes in a callback context similar to how i showed you that the event takes it so in my case nothing shows up because i didn't make it public so you have to also make it public which is another thing i don't like you're exposing functions that don't need to be exposed so in the function now you can see explosion is at the top and it'll call this explosion when this is activated so just to test it out i'm going to put it under explosion and i'm going to remove these two context read values and i'm going to show you what happens in this case when i press the explosion button so if i press e you'll see it prints it six times why is it printing it so many times well it's because you may notice we have a started performed and dot cancelled callback however here we only have one event so this is calling it for the started and the performed and also the cancelled callback so i'm just gonna print out the value you see it's one four times and then it's zero two times when i press the button so you can see this can get pretty crazy pretty fast so i'm just going to remove that from the events here and instead in our script we're going to change this up a little bit to use the player input component so i'm just going to comment this out and let's get a reference to our player input private player input player input and make sure for this to have this unity engine.input system namespace then on our awake function we can get a reference to our player input component get component player input and from there it's a little different we do playerinput.actions to access our actions and then we need to refer to it with a string so in this case we need to refer to our jump action with a jump string and then we can do dot read value float as usual or we can subscribe to those events dot started performed and canceled however you'll see that using a string can lead to some unnecessary bugs if you mistype it many times so what you can do is do a private input action and we can name it jump action and this will kind of store our controls so here we can just do jump action equals our dot actions jump so now we can store our jump action and from there we can do jump action dot read value flow etc and if you're interested with the player input provides you can do dot player input and see what functions it has here so you see we have a switch current action map that isn't available with the c generated script so here we can easily switch our action map and if we have the id of the action map or the name we can just input it there and it'll switch to that action map you can also switch the correct control scheme if you'd like and it just makes it easier to do rebinding of the keys so if you want the player to be able to rebind their keys depending on what they like it's just easier if it's more dynamic instead of having to access this complicated json values and i have a video on rebinding if you're interested so one other thing i want to mention i'm going to uncomment this script is that on some of my videos i show subscribing to events as so so this is a event lambda and what i'm doing here is that instead of subscribing directly first i'm subscribing and then this is the value that's passed through so if you want the context or you can call it context whatever you want so if you want the actual value you can add a variable there and then here then you can call explosion and then pass in your context as per usual however if you don't really need the context you can add an underscore there and you can call a function that doesn't need it so if i have an explosion function here i can just call the explosion without anything or if you don't want to call a function you can just do some brackets and do like debug.log or whatever you want here the issue with this is that it's a little hard to remove the lambda event handler which means unsubscribing from it since you don't have a direct reference to the function that you're calling in this case you do when we do it this way and so i'll link this in the description if you're interested and want to tackle this issue but a lot of people just have this helper function that takes in the context but doesn't actually do anything with it and so yeah that's the basics of the input system last two things i want to mention is that if you have ui so let's add a text for example make sure to go to the event system and replace the old input system module with the new one you just click that button there i have a more detailed video on that and there's also this window analysis input debugger that comes with the input system that you can easily debug your input with for example if you click the keyboard you can see all the keyboard keys and it'll show you when they're being pressed which i have another video on this additionally if you're migrating from the old input system unity has this nice documentation that you can see where they show the previous and how it translates to the current and what you might find interesting here is if you want to check if there's any key being pressed you can do keyboard.current.any key is pressed or if you want to check if a current mouse button is checked you can do mouse dot current dot the button and then dot is pressed and you can see that if we just put this here directly we can access that value additionally if you want to tell when any key has been pressed down you can just use was updated this frame instead of is pressed if you're interested in touch input i have a whole other video that covers that that i'll link in the description as well and so some other questions i get are how to use dot get access raw with the new input system unfortunately you can't do that instead you can just read the unprocessed value by doing dot read unprocessed value and then you can do whatever you want with it you also have this button control that is pressed which you can use if you don't have a binding and you just want to check if a certain button is pressed right here and you also have this was pressed this frame along with was released this frame so for example mouse dot current dot left button dot and then you can do was press this frame or you can do was release this frame or is pressed and two other things i want to mention is that if you don't want to use the input action asset there's two other ways you can make input actions then here you can actually reference an input action or an input action reference so for example on our player controller if we reference an input action so this is called action in the action you can add right here a binding an axis composite vector or a modifier so this is similar to the input action asset but if you only want one control or one binding and you don't necessarily want to make a whole map or a whole asset for it then you can make one here and it's a similar process you can double click there to make a binding and assign whatever binding you'd like there along with the interactions and processors additionally you can get an input action reference which is similar to an input action but instead it just references an input action that you've already made which you can click this circle here and these are the ones we made in our input action asset so if you want to get a reference to a specific action then you can use an input action reference additionally if you don't want to use any one of those and you want to make your own actions dynamically via code then luckily the input system supports that so right here for example we can do new input action and then we can assign a binding to our input action so you just do binding colon and then you have to have a string of your binding so in this case we have to have a device within these brackets so in our case we want a gamepad so we do bracket gamepad close bracket slash the actual name of the action so the button self or the south button of the gamepad and you can find these out by making either an input action asset and creating a binding and you'll see in the paths it has all the names so for example we have a left stick here so if you want the left stick you'd have to use camel case notation so left stick as an example or you can use the input debugger which is under window analysis input debugger and then you can go to a certain device so let's say keyboard and then you can see all the names of the keys that it supports additionally you can add more bindings by calling action right here dot add binding so this is a type of input action so you can do add binding and you can add another binding if you'd like and if you'd like to add an interaction you can do with interactions the name of the interaction in string format along with its parameters so in this case is a duration of 0.8 seconds and there's also a dot with processor function or dot with processors and i have videos covering each one of these if you're interested and make sure to enable your action another way to dynamically make an input action is similar to the first example we enter a binding but we can also add in a processor within this new input action call so you just do processors colon and then your processor name along with the values so in this case it's an invert processor which inverts a value and in the case of a float this doesn't have any values so we can just leave it blank and don't forget to enable your action additionally you can add a name here if you want it to be easily recognizable my binding and just to show you that it works i've replaced this with mouse left button i've enabled it and i've subscribed to the perform callback however i don't need the context so i put an underscore and then i just do debug.log clicking and you'll see that now it clicks and you'll see that when we go to window analysis input debugger which helps debug your input and you press play under actions now my binding appears which is what we have named the action along with the actual binding however i don't really like this method because you can make a lot of mistakes with the string names and it can potentially not work and you might not know why for a long time so i recommend using either an input action an action reference or an action asset so my recommended path is using the input action asset and using the player input component so yeah i hope you enjoyed this video and you found it useful hopefully you'll be using the input system in the future it's really cool and easy to set up it might take a little while to get used to it but once you do it's very much worth it so if you enjoyed the video please make sure to like and subscribe it'll help me out a lot and i'd like to thank all my patrons for the support thank you so much for your support it makes videos like these possible and with that i'd like to thank my new patrons in the enthusiastic tier we have brand au allen andy ansu revenge tech and matthew wright and i apologize for some pronunciations that i might have gotten wrong thank you so much again for all of your support it is really appreciated and if you're interested the link is in the description i offer source code early access and an exclusive discord channel along with some other perks and if you're not already on the discord channel you can join the link is in the description you can chat post memes or ask for help so thank you so much for watching once again and i'll see you next time [Music] [Music] [Music]
Info
Channel: samyam
Views: 15,907
Rating: undefined out of 5
Keywords: input system, input system tutorial, input system unity, input system 2020, input system 2021, input system unity 2020, input system unity 2021, how to use input system, how to use new input system unity, how to use input system unity, unity new input system, unity input system, samyam, unity input system script, playerinput component, playerinput, unity player input, unity how to input, unity input, how to use the input system, input debugger, input action, generate script
Id: m5WsmlEOFiA
Channel Id: undefined
Length: 31min 46sec (1906 seconds)
Published: Fri Aug 27 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.