How to use the New Unity Input System

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments

I think its best to wait until its stable though

👍︎︎ 3 👤︎︎ u/DolphinsAreOk 📅︎︎ Jan 07 2019 🗫︎ replies
Captions
[Music] hey what's up Jason here from unity3d college recently there have been a lot of advancements to unities new input system I spent some time diving into it and playing around with the system and I think it's really cool so in this video I'm gonna show you what's different about it why I think it's better and how you can use it in your own projects to demonstrate this new system I've set up a sample project where I can control a spaceship using W a s and D I can fire hitting control or hold down space to rapid fire I've even added a self-destruct mechanism where I can hold down Q wait a little while and explode of course if I hold it down and release it stops and all of this works with the new input system so let's see how it's all tied together the first thing we need to do though is stop playing and we need to pull in the input system so if you haven't done this yet you'll need to go to a window and then go to package manager and you need to find the input system this won't show up by default though you need to go to advanced and make sure that show preview packages is on of course once this is out of preview it'll just be there but this will probably be way outdated by then so here we go check show preview packages and install this input system just hit him install I've got a remove option because I've already got it there the next thing you need to do is go to the project settings so we go to edit go to project settings and in our project settings which actually is right here you'll see that there's now an input and an input new under input new this will be empty if you haven't already created one in fact let's just delete mine right now and show you so we delete it and it'll say no asset and we just need to create a new input setting so I just hit new and give it a name it doesn't really matter from what I can tell in since this is a throwaway demo it doesn't matter at all so now I've created this new input test to data input settings without that here it's not going to work so make sure you've done that and then I wanted to point out the supported devices section because this caught me up the first time through here if you read the description which I kind of skipped past the first time you see that if you just leave this empty it'll work with all devices as soon as you add a device to it it's now filtered to only work with devices in that list a blank list just means everything so we're gonna leave it blank the next step is to create an action map so go right click and go to UI or create and go to input actions and we should give this a name like controls or player controls you can see I'd name one player controls previously let's just name this controls and I'm going to delete this old source file that was there now I'll select this and you'll see that there's an edit asset button of course we can also double click on it and it should pop up this little input manager control editing window I'm gonna dock it and I want to point out a couple parts to it first there are three tabs we've got action maps actions and properties and you see that actions I doesn't do anything if I click on it nothing happens over here at all the only thing I can really interact with his action maps so I'm gonna add a new action map and I'm gonna name this ship because I want to control a spaceship I gave it a capital s because I've already got one with a lowercase and these are case-sensitive and they will conflict eventually if you're not careful so I've named mine ship this is gonna be for controlling the ship if we wanted to add another action map well why would we do that perhaps for a game where we're flying a spaceship around and we land on a planet and then we want to go to a different type of control scheme or maybe we have a mini game or something else where we want to change the way input works and then we what we can do is just swap out the action map or call into the other one instead but for this game we only need one so we'll just add a single ship then I'll add an action and under our actions will yeah let's start with fire we'll start with fire and underneath fire well if I select it first I'm gonna show the properties properties shows basically what the inspector would show but just for these actions so if we look here there's a type option it defaults to any when we add a default action like this I started off by putting these as buttons and a little while though we're gonna change it over to an axis and I'll show you why but for now we're gonna make it just a button and I'll go to the binding that was automatically created underneath it and then give this a so if I click right here I can just start typing and search for a button or a key for example so it kept like left ctrl and left ctrl pops up and also hit plus and add another binding just get another basic one here and I can hit this little search box and now I can actually just press a key and it will read that key now I have run into a case a couple of times where I was doing this and it kept reading my scroll wheel as the key so if it's not working you see a scroll wheel something else don't be too surprised it is still very alpha I think and things are gonna be broken things will break and you may run into issues just expect it and kind of figure it out and play around with it so we've got our action set up we've got two bindings set up for it there are also some interactions here I'm going to show these in just a little bit but the way it works right now is that our fire action is going to be called whenever we press left control or press space so let's create a really simple script to just read this see what it looks like and then start turning it into an actual spaceship so to do that I'm just gonna right click go create create a c-sharp script and I'll just call this a fire tester it's just gonna test that our fire button is working real quick and show you how to hook up that fire button so go into here and we can get rid of update or yeah update on start and I'm just gonna add an on enable and then in here I need a reference to this control so let's go back into the unity project for one moment and get an idea of what it is that we want to get a reference to so go to our project and we'll go into this assets folder the root assets and we had our controls right here if you look at controls you'll see that it has a generate c-sharp class option on here I'm gonna check this and then I'm gonna hit apply I'm gonna leave all of these blank it's important though if you do create a class name and a file name make sure that you put the extension on the file name here if you leave them blank it's just gonna create one in no name space with a matching filename and classname to the control object so here's just named controls and we got it a script named controls and I can open this up and you see that we have any reference to our fire or our ship or anything else and the first time this happened to me I was very confused trying to figure out what was wrong then I realized there's a save asset button here and I hadn't remembered to press it once I hit that I'll save and if I go back to the project for you and reselect it let's see you go back in here you'll see that I haven't done anything here but it's just gonna auto update so let's go look at the script right here we've got the new version of the script we've got our ship we've got our fire and well it should all work so now I need to take this controls class name and I want to use this in my fire tester so I'm gonna add a private field here first I had to serialize field attribute no private controls whoops not what I wanted controls and I'll just call this underscore controls and I'm just gonna assign the reference to this now in a bigger project where we have controllers that are being turned on and off or multiple players coming in and out we probably wouldn't want to just do a reference right here in fact there's an entire system set up for detecting new players assigning controls to players and oh there's a lot in the works a lot of it's still pretty alpha and still kind of in development and how it's gonna end up I don't really know but it looks really exciting and I highly recommend you check that out so don't go in trying to create like a player one player two player three player four setup without diving really deep into their documentation and looking and seeing exactly how they're doing it because it looks like it's coming soon and it's gonna be really cool and partially works now but I didn't want to go too deep into that since it's gonna change so let's go back to our little crappy way of doing it where we're just going to assign the controls right in here and it's a crappy but realistically this will work totally fine it'll work just fine in any game where we're not just changing out the players or anything like that so we've got our controls reference right here and we'll assign it in a moment but then in on and Abel I want to hit Duke underscore controls dot and then just take a look at what I've got the main thing that I care about is ship actions or this ship so I do dot ship and then if I do dot again you should see that I have fire now and this maps up right here to our ship and our fire when we generate this class that's essentially generating this class for the ship and this fire it may be doing strux or something or properties underneath I'm not really sure well let's just hit f12 and go look it yeah it actually generated a ship actions there we go and then underneath the ship actions it must have the fire so here we've got a reference to our fire action and I'm gonna do dot performed right there so this is an event callback that I'll get so what will happen is whenever fire happens performed will be invoked or anything registered to listen to performed will be invoked so to register for it we just do plus equals and then we give it a method name I'm used to visual studio we're just to tap a couple times and it would audit them I've been using Reiter lately so we're gonna actually type out the method name and I'll call this like handle fire put a semicolon there then we'll go back over all enter and just create the method and it's gonna automatically give me a method that takes in a callback context with a terribly named variable for it that I'll rename to you context and it's gonna throw an exception in there but all I really want to do in here is maybe write a log so to debug log fire so this will show that whenever we do the fire action what should happen is that this performed will get called handle fire will be called in the debug dot log of fire will happen what another thing that's important to note too if we're registering for events if this object isn't gonna be all around for the entire life of the game like you know maybe we're loading levels or whatever and it needs to get disposed sometime we need to make sure that we clean these event registrations up so what I do for that is just copy our on and able paste it down here and do on disable and then here we change this plus Q a minus so what's going to happen is it's going to on an able register for this event on disable D register for the event this prevents memory leaks essentially it allows the things to collect and also prevents this handle fire from being laughter this object is supposed to be destroyed so I want to try this out but I also want to note that it's not going to work because there's something missing so let's let's give it a shot though and make sure that I'm right about that so I'll hit clear and I'm just gonna create a let's go with a brand new blank empty scene and I'm gonna put my fire tester I'll just throw it directly onto the main camera we're only gonna do this for a moment and then I'll take the controls and drop that into the control section now if I go to my game view and show the console when I hit play you might expect that when I hit left control or space we'll see something in the log oh I don't want to maximize on play let's uncheck that okay so let's hit control and see what happens smacking it nothing hitting space nothing happens so again this caught me off guard the first time the issue is that we need to call controls ship dot fire dot enable this is going to enable this event and I could even call disable I believe in the on disable if I wanted to so that way the the event won't even be called again so even if I were registered for it it'd still be an issue still wouldn't want to do it but it won't release get fired off once when we don't have our fire test or around so let's try one more time we'll go back in hit play and as I hit control in space we should start to see debug logs up here there we go look at that so I'm gonna hit clear real quick and hit it one more time because I want to show something notice that it went in twice I'll watch me hold it down and then release so there you go it's actually calling this action twice we're getting this event two times which could be an issue because we don't really want to fire two bullets at the same time right why is it doing that well let's take a look if we go to our controls and we look at our left control the interactions list right now is set to empty so if I'd want to only do this when they press the button I'd have to select press if I want to do it only when they do a tap I'd select tap and so on I could even do a press and release so that it doesn't fire off until I release it in fact let's do that so for left control we'll leave it on press and for space which used press and release and now I need to remember to hit save asset also important to know changing leaves while you're playing doesn't do anything or at least it doesn't work it won't update things live okay so let's hit left ctrl go I'm a letting go now nothing and I'll hold space oh I hate it too fast so as I go down nothing release and then we get the fire so that's how we would do like a fire on release or a fire on press down all pretty simple so far right but let's dive into something a little bit more complicated let's do some movement now so I'm gonna go into my action maps or actually not my action map so I'm gonna go to my actions and I'm gonna add an action and I'm gonna name this move now I don't want this default empty binding so I'm just gonna delete it I'll right-click and hit delete and I hit the plus and I want to create a composite d-pad this gives me up down left and right movements so for up I'll bind this to W for down all oh let's use the hotkey 1 will go to s left will go to a and right will go to D pretty cool that gives us our WASD so I'm going to rename this to WASD and then we could also do arrows or game pads or something say we wanted to add in another composite d-pad we could do something like arrows and then set up all the arrows for this I'm not gonna do it right now because I think you understand and it's kind of waste time but we've got our WASD here we've got everything set up now let's make it actually move now to do that I don't want to just go through and write a whole script in front of you because I think it's kind of slow in might waste your time so instead we're gonna go right into this example version that I have set up so we'll go right down here go to the sample scene yeah we can save this off just throw away scene ok so here I've got my spaceship set up and I've got an input tester script on here and this will work fine with our existing action map but I've created a slightly different action man that I want to show you so let's go ahead and save that one off and we'll go into our input actions action map so this one again you see it's pretty much the same here I've got lower case instead of Pascal case on these things I've got a fire that's set up as a button I've got to move something a little bit different about this though and this is something else that also caught me up for a little while my move type is not set to any it defaulted to any I changed it to vector two this is gonna give me a just a nice vector of X and Y for my d-pad which is what you would expect when you normally read like a horizontal axis and a vertical axis we're just getting it in a vector instead of two separate floats so we've got our movement for our it's named d-pad its name this WASD and then we've got some rapid fire and the self-destruct stuff that you saw just a little while ago pretty cool let's see how it all works now in code so we'll go into our player ship this is that ship that you saw moving around at the beginning of the video and underneath it by the way is just the child it's just a model of a ship that it's rotated and scaled down absolutely nothing special there all of the code is right here so we've got our input tester script which probably should be named a spaceship or something but I didn't know what it was gonna be when I started it and I didn't refactor it properly so our input tester script has a couple fields on it it's got a bullet prefab obviously for spawning bullets an explosion particle for spawning our explosion particle a speed for how fast it moves and an input actions for our controls now I named this completely different from our other one and it's for good reason because the name controls here I think could be confusing it might think like this is some static class or some built-in thing it was just the name of the thing that we created in the other example here let's go back to it I named it input actions so we have an input actions in assets again we had controls and so we got a controls script so we have our input actions script let's see if I can get back to that right here and it's just named controls because this is actually our controls really confusing naming right hopefully it may make sense now though so in our on and able the first thing I'm doing here is caching all of these renderers and you can see I've added a note it's for this self-destruct essentially so I can flash those things really fast when they get into self-destruct mode and then we've got a call for our fire dot performed calls them to handle fire so we're just registering that event and calling and able to enable the fire event and if we look at our handle fire method it instantiates a bullet prefab at our position in our rotation now the bullet itself handles just moving forward and literally it just moves forward at a set speed it doesn't do anything else so that's the entirety of our shooting script or our shooting code right there or at least the basic version of it but now we're going on to move so we've got our move performed registered to handle move oh this is not right it should be handle move let's fix that not handle move horizontal initially I was doing them separate and then I realized doing it all wrong and that they should be done together with a vector2 so we've got this ship dot move not performed plus equals handle move and I want to look at this because this is somewhat interesting I think handle move takes in this context which was automatically of course generated so when I did the plus equals that type handle move hit alt enter and it would just Auto generated that but if you look here at the perform didn't look at the tooltip you see that the action type or the the parameter that we're gonna get from this action is a callback context that's what's inside those two arrows so we've got our handle move here and in it we call context dot read value and we read a vector two and we store this in the move access vector too so that's right up here this private field called move axis and then I just log some stuff just logging out the value of that move axis in fact if we go in and play you could probably see that let's do it let's go into play mode and and watch it real quick because I clicking on the wrong thing there let's see if Jason can learn how to click okay so here we go we hit play and we move around look at the log right here let's go to the console log let's clear this kind of make it bigger you see I'm holding up and left it hold up and right or just right we get one hold just down we're getting negative one just left we're getting negative one here on the X down it was on the Y and so on so you can see that we're getting the right values for the axis and logging them out right here and then we're also moving the ship so where are we moving the ship because you probably didn't see us actually moving it well we're just setting the move axis and then in update when realistically we'd probably want to just move this in fixedupdate do some physics based movement or something else that handles all of this stuff instead we've done a very sloppy just quick dirty implementation of just moving the position by a new vector3 that is the amount of the here let's separate these parameters out it's the x value of the move access times speed times delta time and then 0 for the Y and then for the Z we just move on the y axis of our move axis because we're only getting a vector two back and then uh well what's this next part here let's look at this lion 60 if fire down instantiate a bullet prefab oh you might be wondering what this is why is there a fire down we already handled fire this is that auto fire the rapid-fire stuff that we're getting from holding down space let's go back into game real quick hit play I just want to show it and then show you how I'm doing it and how it works in again I'm not sure it's the best way to do it but it's the way that I figured it out so here you see all the bullets flying off going by the way very important to note that if you're doing this in a real project you don't want to just spawn all of these bullets of course you'll want to use some sort of a pooling system I think that's way beyond the scope of this video and I've got plenty on it so if you're interested in that just do a quick search so we've got our rapid fire how is this working well our Rapid Fire performed calls handle rapid fire let's take a look at it handle rapid fire reads a float from this context here it's named Baddeley again let's rename this to context so we've got our context and we read a float and then we just set fire down to true if the value is greater than or equal to 0.9 we'd be like if the values 1 but imagine we have a trigger or something we mo may want to add a little bit of dead space in there in fact I think we can do that outside of here we don't necessarily need to do that in code but I wasn't quite sure how the right way to do it was so I just did it right in here so we just check and we set fire down to the value being greater than 0.9 so basically if it's on so how does that work though let's take a look at the action map it's set up a little bit different for our rapid fire so if we go to our input actions and go to rapid fire look at the type here so fire is set to button rapid fire is actually set to an axis so it's gonna give us back a value from 0 to 1 depending on whether or not the button is pressed down so it really it's gonna be 0 or 1 because it's a keyboard but imagine it was like a thumb stick or something like that or thumb stick a bumper button or the trigger buttons where we could get back a value that's on an axis we want to do the same thing here because our event is gonna call both when we pull it down and when we release it because remember let's go right here we don't have an interaction setup here so it's not only filtering to press or press and release whenever we press or release it it's calling and it's gonna give us back the value there in that axis so we're setting fire down to true or false and then in update if fire down is true we just spam out those bullets all pretty simple right the last thing that I have in here is this self-destruct and I thought this was interesting because it uses the hold mechanism it uses a start and a cancel and to perform so if we look at our self-destruct it's got started cancelled and performed so in start we just call into this start destruct method which just kicks off this blink co-routine it runs on forever and then turns all the color or the colors child renderers red waits a tenth of a second makes them white that's how they're blinking that is blinking on and off so when we start destruction we start that co-routine when we cancel that's well when we call this method right here which finds the co-routine if it's not no stops it and then changes all the renderers back to white in case they had happened to end on a red flash now if you're not used to this co-routine syntax and you're used to doing it by string I recommend that you make the switch and go this way try to avoid string co-routines in general just store them as a co-routine reference let's go to f12 on this blink routine you see that it's just the type co-routine and we're just assigning it a value it will be null by default so until we've actually assigned it a value this code won't happen and we don't have a problem and then when it's got a value it's fine it stops it and it finds the right thing the final thing in here is the destruct which is pretty simple it just instantiates an explosion particle stops the blinking and sets it to white the cooler part though is right down here the only thing that makes this special or makes it so that I have to hold it or hold it for some amount of time is this hold interaction right here with a duration set to 3 it'll just work so as long as I hold it for three seconds it'll explode if I hold it it'll start and if I release before the three seconds I'll get that cancel to call now I'm not a huge fan of the idea of having the duration right here in the controls for a spaceship self-destruct it may not make a lot of sense like that seems like it'd be a ship specific setting but I think that there are a lot of cases that you could use this hold for in fact basically anything where do the durations not gonna vary by some state of the game plus we could also I think set up these interactions in code and adjust them in code if we needed to as well alright so this input system is pretty cool I really like the way that it abstracts away the controls and puts everything into a nice simple mapping system and that it's way better than the old default but there are some things that well might be a little bit harder to use and you may be wondering how do I get to them luckily if you look at the documentation on the github page for this you'll see that we've actually got code access to just about everything you want to access something from the keyboard and call keyboard current dot the actual key name and see if it was pressed for instance you can check to see if anything was pressed you can find all of the gamepad's you can read the mouse the gyroscope and everything else just start to go through this documentation maybe look at the migrate from old input system stuff as well it's got the new way to do things or an event-driven way to do a lot of things that you would normally do by just constantly polling so if you're looking at implementing this into your own game I would probably hold off a little while maybe experiment with it try it out a little bit and just wait until its final and shipped or at least until 19.1 when it's into an actual release build and pretty close to steady and stable which i think it's not too far away so I'm excited to start using this in my projects coming in 2019 I'm curious to see if you guys have any thoughts on this system as well if there's something that you think is missing or that I should have covered that I didn't I'm sure that there are plenty of things in the system that are really awesome like the new player mapping that system seems like it's gonna be amazing and really cool it's just not very documented and I think it's still very alpha right now but like I said I'm super excited for it anyway don't forget to subscribe please I really appreciate it and share with your friends and also a special thanks to all the patreon supporters it's been growing over time and really appreciate it you guys are awesome thanks again for watching if you have requests for videos or anything else just drop them in a comment below but you
Info
Channel: Jason Weimann
Views: 97,695
Rating: undefined out of 5
Keywords: input system, unity tutorial, input manager, unity input, how to make games, unity (software), unity tutorial 2018, new input system, unity input system, new input system unity, input manager unity, input system preview unity, wasd keys, unity3d, unity, brackeys, input, tutorials, howto, game development, indie game development, unity tutorial for beginners, unity 3d, unity input tutorial, unity input manager tutorial
Id: zIhtPSX8hqA
Channel Id: undefined
Length: 28min 34sec (1714 seconds)
Published: Sun Jan 06 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.