NEW INPUT SYSTEM in Unity

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments

I'm surprised that this video is or so quickly, this system is in early preview without documentation and working demo.

👍︎︎ 1 👤︎︎ u/Wokarol 📅︎︎ Dec 28 2018 🗫︎ replies

This was a fun watch for me - given that I spent countless hours writing a system of my own. I can't use this on the next project.

👍︎︎ 1 👤︎︎ u/unfoldgames_ 📅︎︎ Dec 28 2018 🗫︎ replies
Captions
hello everyone and Happy Holidays Christmas was three days ago it looks silly now I use unity pretty much every day and for the most part I really enjoy it but there's one big exception to that rule and that is the input system the input system has always been fine when it comes to just quickly slapping something together but as soon as you want to start using multiple controllers through customizable input just use more than a few buttons things start to get really cumbersome so today we'll take a look at a very early version of the new input system that unity is working on but first this video is sponsored by expressvpn expressvpn has several locations in over 90 different countries and is the fastest VPN provider out there by using a VPN you increase your security anonymity and it protects you from hackers expressvpn even uses a strong 256 bit encryption to help protect all your data for me one of the biggest perks has been that it lets me stream the office which isn't available in my country so thanks to expressvpn for allowing me to experienced white in HD a membership is less than seven dollars a month and they even offer a 30-day money-back guarantee so take back your internet privacy today and find out how you can get three months free by clicking the link in the description also special thanks to Andrew clink oh Rudy and Yvonne so hard armand john shannon and extrude ski extended blair and infinity PPR for their support on patreon before we get started i just want to say that the new system is still very much a work in progress and so you'll probably notice some weird behavior here and there that being said i think it looks like a very promising upgrade to the old system especially when it comes to interface it's currently available on github and has been quite a while actually so let's jump right into it so first we of course want to install the new input system currently this is done by a github but i imagine that we'll see some kind of beta release through the package manager at the start of 2019 I'll of course have a link to this in the description you can either clone it or simply hit clone or download and download it as a zip file I've just gone ahead and opened up the project here and created a very simple scene with just a main camera a light and this player object which is just a cube and on this cube I have a player script which is currently just completely empty so to start adding input to our game we first need to create an input asset so if we right click in the project here and go great we can go to the bottom where it says input actions if we click this we can give it a name I'm just gonna call it something like input master this will take care of all the input in this example we then double click this asset and it's going to open up in a separate window I'm just gonna dock this by the game view and as we can see this window is made up of three parts action maps actions and then properties we'll go through these one by one let's start with action maps so action maps are basically categories for actions you could create an action map for your player one for your menu one for vehicles and so on let's go ahead and create a new one for our player now under this action map we can create actions and actions are basically like events examples of actions could be shooting it could be movement it could be reloading or bringing up a menu let's go ahead and create a new action here let's call this one shoot now as you can see once I have an action selected we can adjust properties over here in the properties window this is pretty much like the inspector for actions however under each action there's actually one more thing that we can create these are bindings and bindings are basically like triggers for actions so let's create a binding here for a shoot and with this selected we can go to the right here and under binding we can select a key and you can search for anything I'm just gonna search for the spacebar but you can see here that's one called space and this is under the keyboard device let's click that and now we've found the space key to this shoot action so every time we press space this shoot action is going to be called however you often want multiple keys to do the same thing and we can actually really easily do that here if we want another button to do shooting as well we can simply add another binding and we could for example bind this to mouse click so we'll search for a mouse and now we'll get all the mouse input options we'd input stuff like position or scroll here or simply use the left button so now we can either shoot using space on the keyboard or a left button on the mouse let's go ahead and add another action so we'll hit the plus sign on the actions and that's called this one movement and depending on your game you might want to move in multiple directions and a nice thing about the old system was that we could use this input.getaxis to simply get a value for each axis that's specified in what direction we were traveling and how quickly and we can actually set up the same behavior here really is the using multiple bindings however instead of having to go in and create a bunch of bindings we can actually create these automatically using either a composite axis or a composite d-pad the composite axis is going to use one axis so it's going to go between negative and positive on this axis just like you would get axis horizontal or vertical or use create composite d-pad to move in four directions but before we start binding these keys we need to think about what control scheme we are using so currently everything that we're setting up here is for keyboard and mouse however a lot of the time you want to be able to support both keyboard and mouse game pads of different sorts maybe you want to do touch controls and we of course need a way to configure this in our input system we can do that really easily using control schemes in the left top hand corner here you can see it currently says no control scheme let's go ahead and add a new control scheme instead and it's going to open up this window this might be one of those weird behavior things I was talking about because it really shouldn't appear over here I don't think but nonetheless we can go ahead and create a scheme name and this could be keyboard and mouse we then add a device that we want to use with this control scheme and for some reason the list of devices is only turned on my secondary monitor and I'm unable to move it but I promise you it shows a list of devices and we can search for the one we want I'm just going to search for keyboard and add it and we can now see it on the list and we also want to add a mouse here and we can now see that we've added both a keyboard and a mouse device and it's now hit add to create this control scheme if we then go to the top we can add another control scheme this one is going to be gamepad and let's then add the gamepad device that's it add and we've now created two control schemes keyboard and mouse and the gamepad if it's like no control scheme it's just going to show all of them so for our first two actions here we want to make sure to select these as keyboard and mouse and let's also do that for the for movement once we just created and now we should see that when we switch to the keyboard and mouse scheme all of our bindings for the actions show let's also add some keys to these bindings so for up here we'll choose up arrow for down we'll select down arrow same for left and right let's rename this binding here to arrow keys in fact let's create another set of bindings here so let's create another composite d-pad and let's call this one best keys under up let's go ahead and bind W down it's bind s a for left and D for right and let's set all these to use the keyboard and mouse control scheme and we should now see that if we change our control scheme up here to keyboard and mouse everything still looks the same however if we switch to gamepad we still see both of the actions but all the bindings disappear because the bindings aren't set for the gamepad instead we can go in here and create our own bindings for the gamepad so let's go and add a binding for shoot and this could be something like button West and it automatically goes on the gamepad now for movement we could create a d-pad and up here we'll search for a d-pad so d-pad up d-pad down left and right we now see that for each one of these bindings we can add processors and these will help you configure the input so if we want to invert it clamp it normalize it adjust the sensitivity we can do stuff like that here using processors now let's switch back to keyboard and mouse and it looks like we're now ready to integrate this with our game play so let's save the asset here and one thing that is really nice about the new system when working with it through code is that it allows us to generate a c-sharp class based on our input configuration what this saves us from is using a bunch of strings to get the right actions based on their names this is something the old system was definitely suffering from and it's nice to see that unity is working around this and you definitely still have the possibility to use strings if you'd rather do that now to generate a c-sharp class we just have to toggle that here and we don't need to fill out any of these variables we can simply hit apply and as you can see unity is now going to create an input master c-sharp script next to our input master asset we don't need to make any modifications to this we can actually open it up and change it if we would like to this is just generated code from unity now let's open up our players script and as you can see here it's a completely new school let's just get rid of the two default methods here and at the top here we want to be using the Unity engine dot experimental and this is of course going to go away dot input now the first thing that we want is of course a reference to the input ads that we just created and because we've made it generate a c-sharp class based on this acid we can actually just create a variable of type input master so let's create a public input master and let's call it controls now if we just quickly create a function here that's create avoid awake and if you've never used awake before it's just like start except it's cold even before start is called so it's really good to use for setup now in here if we write controls and then Godot we can actually access our player action map and if we press dot again we can see the different actions under this map we can see movement and shoot here if we go shoot and then right dot performed we can choose what happens when the shoot action is triggered so when any of the keys bound to this action is pressed then we can choose what happens and let's go ahead and create a function for that so let's create some kind of void let's call it shoot let's just right now throw out a debug that large saying we shot the sheriff so now we can simply take this method and register it to this button to do that we go plus equals and the syntax here is a bit weird if you've never worked with delegates or events but think of it like we're adding this function to a list of functions that should be called whenever this action is triggered and because the system is so cold it actually gives us some context as to what is happening with this button we're not going to be using this information here but we actually still need to write it let's create some kind of variable called context and again the syntax here is weird we then do equals and then a greater than sign and then we specify the function so in our case this is going to be shoot again bear with the weird syntax here you can call this variable anything you'd like you can call it context or because we're not going to be using it we could just do an underscore here and then it just looks like we're kind of adding this shoot method to this list think of it like that for now and this should actually probably trigger our function however we do need to first in our input master so because the system is so modular and we want to be able to swap in and out different input assets and we want to switch between control schemes and all that we can basically enable and disable all parts of the system in our case here we just want to enable our controls so let's create a void on enable so whenever this game object is enabled let's also enable our controls so we can do controls dot enable and let's do the same thing for disabling so let's create a void on disable and it's just right controls dot disable so now the controls are going to turn on and off together with this object which is fine for now and now we can actually save this go into unity let's select our player and let's drag in the input master asset not the generated script and now when we hit play we should see that if I press the mouse button here indeed it prints we shut the sheriff and the same thing happens if I press space you also see that it triggers twice each time one for when I press the button and one for when I release it to get rid of this we can go under our input master we can find the button and under into actions we can choose press and let's do the same thing for our left button here so now it's only going to trigger on press and not on release let's save that and try replaying and indeed I now have to press each time we want it to display and nothing is going to happen on release awesome but how do we handle the more complicated case with movement where we are able to travel in all kind of directions for one particular action well this is where the context is extremely cool so let's go ahead and create a function here for what we want to happen with movements let's create a void let's call it move and in here let's throw another debug deadlock saying something like player wants to move well we can of course register this in the exact same way as with the suit function so that's two controls dot player dot movement dot performed plus equals let's write context this time CTX the weird syntax here and then move and now if we save that and go into unity and play and I'm just going to use the arrow keys here we can see that the player wants to move each time I press one of the arrow keys and the same thing for the vast keys hardly this has no indication of which one of the arrow keys we're pressing and so we don't know what direction to specify this we can go in and let's first of all add this as an argument to our move function so let's create some kind of vector - and it's called this direction and then when we debug the locking here we can maybe create a bit of space and add on the direction to this message so now we should be printing the direction that we want to move in and the only thing that we need to change up here is that we now need to use our context to specify what direction we want to move in and luckily we can do that really easily if we do context dot we can get all kinds of information about what's happening with this event so we can see the action that got triggered we can see the control that triggered the action the duration stuff about into action the phase and we can also read a value so if we do dot read value here and we specify that we want to read a vector2 value and open and close some parentheses well now we're basically feeding the value of our input into the move function and so we can simply access it down here as direction and if we save that and go into unity and play we can see that if I move to the right here it goes 1-0 if I go left it goes - 1 0 if I go up it goes 0 1 and if I go down it goes 0 - 1 and I can combine these to create kind of smooth input in different directions and this works without the arrow keys and the vast input if I connected a gamepad and tried this out it would work in just the same way awesome so that's how you can create really complex input scenarios fairly easily using the new input system however a lot of the time especially when just messing around you don't want to go through all these steps of first doing something in the UI and then hooking it up through script this is a really good way of doing things but sometimes you just want to check for a button press and to do this really easily we can actually get rid of all this stuff let's just say we want to check for air press in the update method let's create an update method and the easiest way to do this is using input system dot get device and here we could get any device so in our case we want to get something on the keyboard and let's just store this in a quick variable type keyboard and it's just called a KB and then we can simply check if KB dud space key that was pressed this frame well then let's go ahead and debug that lock someone pressed space and let's save that go into unity and play and now every time I press space that message is going to appear so you can see there's plenty of opportunity for using the system through code as well awesome that's pretty much it for this video if you enjoyed it make sure to subscribe so you don't miss a future one also make sure to check out expressvpn simply click link in the description to find out how you can get three months for free and if you haven't already checked out our new store for game dev clothing definitely make sure to do so simply go to the line of code that I owe and grab a shirt you like and that thanks for watching and I will see you in the new year which is going to be fun thanks of the awesome pitch and supporters who donated in November and special thanks to make a cane and your Canon ink Oh art Arman to VR systems extend the player infinity PPR Sybok mummy and Sullivan chef Abdullah Chris faced murphy thanks Allen Lila set fit sentence Cuba shreya ste Ronan Bruins cat no Kiyosaki Gregory Pierce Mary tweet cool Swedish key tips rogers repair car Jackson / Campania Robert punt Erasmus Anthony Patton of breezy James P Timothy bar John Shannon Alec Sharratt ski Travis Dylan Rudy in Toronto and casting Sue Ellen you guys Rock
Info
Channel: Brackeys
Views: 532,287
Rating: undefined out of 5
Keywords: brackeys, unity, unity3d, beginner, easy, how, to, howto, learn, course, tutorial, tutorials, game, development, develop, games, programming, coding, basic, basics, C#, input, system, new, input system, code, wasd keys, intermediate
Id: Pzd8NhcRzVo
Channel Id: undefined
Length: 17min 4sec (1024 seconds)
Published: Thu Dec 27 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.