Unity 3D - New Input System, First Person Control

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
welcome back my name is limited iran and this is a tutorial on how to set up a first person character in unity with the new import system so first things you first thing you want to do is you want to come and open your package manager which is in your windows package manager sometimes this will be in project you want to change it to unity and then you want to just type in input and you'll see input system version 1.2 you might have a later version if you're watching this in the future and then down on the bottom will be install press that and it will close down your program just say yes and yes just to shut down and restart the program and also save your scene once once that's installed you can continue what we've done what we've done to the world is basically nothing it's a fresh world we've put terrain on here and we've given a a layer called ground which you just come and you go add add layer and you type in the ground there that's for our gravity feature when we add it next thing we want to do is create an empty and we want to call this player i just want to move set its transform to zero and then i want to move it to five and five i just like it to be there no real reason why then we want to add a character controller set this slope offset to about seven and we can change this radius to uh about that that's just to change changing its height and it's with the character controller for the collision so this is a collider as well then we want to add a mesh to it i just want to change this to a couple of little things just to make it look a little bit different move this up again just so it's just above then we want to add a new create empty and we'll call this one um ground well maybe we'll just call it ground check sorry so it's a bit more understandable just changing up camera position so we can move this we want it just to be just addie's feet basically it doesn't have to be perfect but yeah being a bit picky to be honest and that's basically it for the character apart from the camera we want to move that onto the player we want to reset its transform let's go around here and we'll just raise the camera up so it's like that and that's perfect now that's that's the character built we don't need to do anything else apart from out of the scripts so now we can start uh start with the input actions this is where it starts to get fun so i will call it input master we'll open it we can dock this wherever we want we can keep it like this or we can dock it i like to dock it because you know we change stuff here and there and i might just increase the screen so people can see clearly so this is the action so this is um what how you're going to access it your first access i'm just going to call this player so you can have another one called ai if if um if they're different sorry ai if why why is it doing that crazy ai if they're different obviously that's more advanced don't need to worry about that right now just go player and then we want to go into the action part this is where it starts to get a bit more interesting we want to change these to uh change these are how we're going to start performing actions with keys and game pads and whatnot right now i'm just going to show you how to do keyboard and mouse because i don't have a game pad or a controller connected to my computer that doesn't actually matter you don't need a gamepad connected computer you just need to know what they're called the variables i'm going to call them variables because all action types sorry are they called bindings my bad sorry about that they're called bindings so anyway uh this first one we're going to call it movement and we want to change this to a pass through pass through allows i believe it's floating numbers and we also want to change it to a vector 2 of type we want to delete this one because it's not necessary we want to add a add 2d vector composite and we want to change these to what they need to be so we can rename this to wasd for keyboard control and then we want to change the pathing and what a handy feature is the listen function so we can fall up we want to just press w on our keyboard and for down s for left a and for right listen d then you can add your game pad here as well also make sure it says digitalize uh digital normalize that will uh you need to have that that's for so so it doesn't just flick over from one point to another i'm not 100 sure i need to look that one up but i know you have to have it as digital digital normalized anyway i'll show you how to just quickly do one game pad gamepad and then you can if you don't have a gamepad then you can just search it up by doing d dash pad and then you type in what it is up and then you can go d dash pad down you want to go gamepad because that's the generic one then you should be able to use all of them not 100 sure i've never done it before but i believe that's how it's meant to be working uh left and then we can do the dash pad right and that's it that's that's how you do a gamepad very easy that's really all we need to do for movement apart from we need to add our camera so our first person look oh i'm just going to call it look we want to change this to a pass through i believe yeah a pass through with a vector2 we want to keep it as a single binding and we just want to look for a mouse as a delta and then for a gamepad we can look for i believe it's called right stick gamepad yes and that's gamepad so we've set up both both um variants we can easily use mouse or mouse keyboard or gamepad it's not not a problem then we can set up a jump we want to keep it as a button that's fine we want it to listen for a space and uh we can also do a gamepad so we keep a single binding and then we press a is that uh is that a button or is it called south button south gamepad yes that's how you do it that's generally the a button i believe yes so that's all we need right now we can implement a fire and a pause but that's beyond the scope of this tutorial at the moment so that's it that's how you set up the input master it's very easy we save the asset then we click make sure that it's still clicked on and you come over to the right hand side or in your inspector and you say generate class you don't need to touch any of this and just go apply done it creates a script which we also don't really need to worry about i just want to close that sorry yeah so we can open this script so it creates a script we don't need to worry about this we can touch it if if you know what you're doing um i wouldn't it should generate correctly so yeah just leave it as it is we can close that and now we can actually start creating what we need so on the um it's not really recommended to create scripts on the uh on the um object itself it's better to create the scripts in a file so if we go or folder sorry we'll call it scripts if we create the script in this script folder or whatever folder you specified it's easier to you don't have to move it then because you know where it is whereas if you create it on the player object you have to move it out of here which may break it i believe it could break it so it's better just to create the scripts in here so i'm going to create two scripts one called a mouse look that will be attached to the main camera and then another one called a player controller you can change the names to whatever you want there they don't matter you can have it as foo and bar it doesn't matter so now we can add component and we want to go look for the mouse look this is on the camera component done and then on the player component itself we want to look up the player controller done that's it so now we can open up the scripts and we can actually start uh scripting these up it's very very easy very quick i just want to turn that off open this so now we're in the player controller now we want to move to the mouse look instead we'll do the mouse look first it'll be the easier one we want to just remove these at the moment we don't need them right now and we need to add using unity engine that's going on right yes engine dot input system this is so we can actually access that input class and we're going to want to create a private input master controls so this is accessing the input master or it's getting the input master script where it is here and it's uh giving us access to those controls and i will demonstrate that in a second we want to create a private float mounts mouse sensitivity uh sensitivity yes f which we can change to whatever whatever we need to i'm going to create a private v2 or a vector2 sorry called a look or mouse look that doesn't need a it doesn't need an initialization we can create a private float called x rotation we'll just call it put the initialization there and we also want to private transform called player body so this uh so when we rotate the camera it will actually rotate the body of the camera so we can save that and we can actually look at that right now on the camera uh oh because it made it private didn't i yes sorry as a public my mistake there's two ways we could have done that um i i'll show you the other way it's actually a little bit nicer so here's the player body with the looking for the transform so what you can do is you drag the transform of the player body straight in i'm done that's it or the uh the way i kind of like it and so it's all just stays in the code is we want to we want an awake function anyway we go play our body equals transform dot parent done and then we don't need to drag in anything and it's all just in the code it's easier well i think it's nice it's nicer than doing that in the inspector but everyone's different so whatever whatever you want so now we're just going to create a new reference to this input controls we can access it there but it's just this is so we can use it in different functions i believe new input master so yep now we can use that properly in whatever we want and then we want to go cursor dot lock state equals cursor block mode dot locked this is so when the game plays the curse will actually be hidden you have to press the escape key when you're testing to actually hit the pause or stop button that's it for the awake function now we go into now we need actually we need uh my private on private well we can do it this way on enable and we also need a on disable this is to stop memory leaks from happening uh because when you start the action you need to be able to disable well this one is just actually start the actions this one is to stop the actions because if the action doesn't ever get stopped it will cause memory leaks and that will because problems and you don't want nothing to do with that so we go controls dot so accessing our controls controls dot um on on enable oh whoops no not on now let's just enable sorry and we want those we want those brackets we just want to need to remove this done that's it and then same thing for disable disable done now we want to create a update method and we also want to create a um i might create a private void look look method the update method will call the look you could just create it straight in the update i just don't like doing that it feels weird i prefer it just to do this where it calls a function it does is basically if i had written it straight into the update it wouldn't have mattered but for me this is how i like doing it anyway moving on so we need to access the controls as a read value now so when the mouse goes up down left or right or left right sorry all over the place oh we want to access that and put it into a variable and we have a variable right here our vector 2 mouse look so we go mouse look equals controls dot player dot uh move no look sorry yep look uh dot dot dot read value and we want to read a v2 value i'm going to close it off so how how is this getting access so the controls is the input master and then the player in the input master we can just quickly have a glance so there's access in the action maps and then we're accessing the look function and then we're getting values from either one of these devices and we're putting it into the mouse look variable okay so we need a float we need two two new uh variables to actually break these up into to break the mouse look into different parts no is that right yes to break the mouse look into two different parts the x and the y values so they don't get uh so it's easier to read for the for the program so we want to go mousex dot mouse look dot x equal uh no times our mouse sensitivity and then we want to also times that by times our delta time and we want to go float mouse y equals mouse.look dot y times mouse sensitivity times time dot delta time done we captured both those values now so x uh is x x is this way is x horizontal let me have a look x is horizontal y is vertical yes so x is horizontal and then y is our vertical and we also want to clamp that so we want to clamp our y so it doesn't um so we want to clamp it to 90 degrees up and 90 degrees down so the player can't basically bend his neck all the way around it would be a little bit weird but we'll do that actually right now which will grab that x rotation variable that i created we want to go minus equal and then mouse y and that's that's not clamping it yet this is what's going to clamp it now we go x rotation equals math.clamp and then we want to give it the x rotation uh negative 90 degrees and then 90 degrees now we'll give it a nice right angle so you can look straight at his feet and he can look straight at it straight in the air perfect what's next we need to actually get the rotation so we want to go transform.local rotation yes and we want to equal that to a quaternion plutonian dot euler we want to give it the x value so we can we can see float exploit y and float zid z so we want to give it our x rotation and oh what are you doing and then zero on the y and zero on the z they don't need to be rotating yes that's correct and then we want to get our plow body did i do that wrong i didn't do camel case there sorry about that i want to go play body dot rotate we want to give it a v3 dot up times our mouse x so our horizontal yes yes yes yes i believe that's everything for the mouse look i'm pretty sure that that's it so we're clamping the rotation we're allowing the tran uh we're allowing our local rotation and we're also get letting us rotate on the horizontal and these are just the values getting times by our mouse sensitivity and the times at that time so we can adjust that if it's too fast for you and whatever else perfect let's have a look and make sure that it actually works uh let's just change my screens back around because it looks like a mess and we'll have a quick play so if you click on once it once it plays oop so we can look around perfect we'll look up and we'll look down so you can see a 90 degree angle i'm looking straight flat at the floor 90 degree out so that's good as well and we can move it around you can see how fast it is i am on a pretty high dpi mouse as well but it's quite fast you can adjust that there are and then there's also settings where you can um i think that's how i have my game there's also ways that we can adjust it so it's in game when the player pauses they can adjust it but that's out of the scope of this video we're onto the next thing which is the player controller this time so we get some movement going i might just quickly create a couple objects in the scene so we can view uh so we can view a couple things like um when he's moving so just create a cube there and we'll create a another cube sorry all right we're back sorry about that i've just created a quick platform just so we can trial out some gravity and um the walking up and down nothing too special just to hit a whole whole bunch of cubes the only thing you have to make sure is that the ground layer is on when you when you do do this okay so let's get into the player controller now there we go going our scripts player controller that will open the script perfect and we want to basically do the same as we did for the camera unity engine dot input system uh we need the update method so we'll leave that remove that one and then we want to just start adding our variables so i wanna i'm just gonna add all the variables because we don't need to fluff around with it uh what am i doing private master input to access our input master and we'll call it controls then we want a private slow move speed move speed equals uh six f four is a nice number then we wanna private private v3 called velocity and we will give that no initialization private flight gravity everyone says negative 9.81 f privates v2 we'll call this one move that's to access the character controller private float jump height i'll give this a 2.4 f 2.4 units private character controller we'll just call that controller and then we need some publix down here public transform ground uh public floats uh distance to ground distance and that's the actual layer mask that we were using before so i'm just going to call that ground mask or brown layer whatever you want to call it these don't matte their names uh doesn't matter the these light blue bits these do not matter what you call them you can call them anything you want uh private bull is grounded just to check if we're actually on the ground or not done that's all our variables completed we need an awake function we need a on enable function we need an on disable function we want to i just want to move the update function from there to below here let's see and then we want three more functions called i'll just pull a grab for gravity i'm sorry privates void mode player movement that's damn it all right fix that private void jump done that's all the functions we need we'll just quickly do this go to controls dot enable need to do this for every script you use input controls on controls.disable sweet we don't need to worry about that stuff anymore we go into the awake function we want to go controls. controls equals new now that those are done we need to call our three methods below so we want to call graph we want to call plan movement and we want to call jump we can go straight into the grav so we want to check is the player grounded that's the first thing we want to check so is grounded we want to do a physics property we want to check sphere we want to basically create a severe ground just called ground dot position so we want to create a sphere at the ground position yep the distance to ground and the ground mask so if the sphere is touching the position of the ground i'll quickly represent it sure why is this not moving very fast now so it's going to create a sphere basically just at his feet and if it's touching the ground then he's he's grounded if he's not touching the ground that's fear it then he's not grounded that's basically all we're checking for with that line of code there so it's very very easy now we can create an if check is grounded and we also want to make sure his velocity dot y is less than zero uh we want to make then give him a velocity dot y equal to negative one f or negative then uh that was just so that he doesn't fall like a rock he falls a bit lighter now we want to actually set up the great uh the gravity itself plus equals gravity times time diverts time and then controller dot oh sorry accessing our player controller we want the move function in it i want to give it the velocity and then it's going to that's the velocity that's moving basically a move speed we want to times that by time dot delta time sweet save so that's the grav we can minimize that to get it out of sight then we want to move on to our player movement script which is even easier we want to call our move function and i'll move variable sorry so that's our variable that vector3 that we created up here oh vector2 sorry vector3 was the velocity yeah vector2 equals controls.plow dot movement dot read value so we're reading the value like the mouse with the vector 2 so we can get the horizontal and the vertical is it yes uh we'll see so we're reading about values but this time we don't really need to break it up as like we did with the we do need to break it up but we don't need to break it up because of the player controller so we go vector three we'll call this one our movement instead equals move dot y so that's the vertical times so move forward and backwards transform dot board and then we want to go plus move dot x horizontal times transform dot right and that's it easy and then we go control we want to access the controller the move function in the controller again we want to pass it the movement variable plus we times by our move speed and then we times that by times like delta time that's the new input control that's the new input stuff it's super easy it's much easier than writing if get button down w equals to and then you pass in the string horizontal and then you do all that it's a lot neater there's only a few lines of code or basically only really one and then you're doing the exact same thing as you did before uh it jumps even easier so jump we go if controls dot plow dot jump dot triggered uh velocity we'll pass the velocity again dot y so up and down equals math f dot square root jump height our variable up top times negative two times gravity done so that will access our controls our player our jump which is space and as checking to be if it's triggered or not whether the action was triggered ie blah blah blah so you can read them by just looking over the properties when you're in your code and that's it that's our player control so we can test that we shouldn't have any warnings no no warnings uh i just want to move my camera so we can get a bit more of a bird's-eye view in our scene and i also want to look at the camera so we can see where we're looking at in the scene uh what am i getting variable ground of oh that's right sorry about that guys so we want to go to player of course and we want to actually go layer mask is called ground and then we want to grab our ground check and put it in there so you can do the same as the player body but instead of going dot transform dot parent you can go transform dot find is that right yes transform dot find and then it takes brackets and then it takes a string and you can call it um uh ground check so it finds the child of your of your object i can i don't need to show you you guys can look up the documentary for our documentation for that so those areas should bugger off now this should be fine so we can look you can see in the scene that we're looking we can remove our mouse so that's the cursor lock and press w uh s a d you can move all over the place don't want to fall off because the gravity is working so we'll go up my stairs all the way on top we can fall off and we can press space jump so everything's working that's a first pla first person controller with the new input system in unity thank you for watching please hit that subscribe button and that like button if you like this content and let me know in the comments if you want more thanks bye
Info
Channel: Botable Coder
Views: 1,688
Rating: undefined out of 5
Keywords: LimitedIron, unity, unity3d, asset, assets, beginner, easy, how, to, howto, learn, course, tutorial, tutorials, fix, tip, game, development, develop, games, programming, coding, basic, basics, c#, fps, first person, shooter, movement, controller, gravity, jumping, slopes, stairs, ground checking, physics, velocity, move, characterController, fall, sprint, crouch, character, input, input system, new input system, wasd keys, gamepad, intermediate, unity 2021, how to use new input system unity, gamedev, indie, indiedev, free
Id: w4IMYgpqgdQ
Channel Id: undefined
Length: 33min 43sec (2023 seconds)
Published: Tue Feb 02 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.