Unity Twin Stick Controller Tutorial (Gamepad and Keyboard) | Top Down Shooter Controls

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello and welcome to today's video in today's video i'm going to be showing you how to use uh cinemachine and unity's new input system to make a top-down uh twin stick shooter uh this uses the wasd keys and the mouse or if we swap over to a controller so i'm on my controller now use the left stick to move around and the right stick to aim it we're just stop still see with the mouse this is back on the mouse if i move over here it kind of snaps automatically like it it does rotate around but it does it very fast the right stick here on the controller if i'm pushing left now then i suddenly press right you see that we rotate through that the whole position it doesn't just snap and i think that just makes for a bit of a nicer kind of feel to the control scheme so yeah with that said let's just jump straight into it okay so here i am within unity and i've just got some packages installed already this is just a blank 3d project uh it's not a urp project or anything it's just the built-in 3d renderer and i've got cinemachine installed pro builder installed and the unity input system and so just make sure you come up to your package manager and just make sure that's in a machine the input system and pro builder are installed pro build is not a necessity um but i'm going to be using it just now so the first thing i'm going to do is i'm just going to make a new sort of plane here 20 by 20. this is with pro builder and i'm going to select our plane i'm going to center its pivot and then i can just close this and center that out in the middle and i'm just going to swap over to my preferred editor layout okay so i'm just going to right click and create a object i'm going to make a capsule i'll just call this our player i'm just going to set this to 0 0 0 and then i'll hold down control and just snap that up there one unit so that's now sat there and then i'm going to right click 3d object add in a cylinder i'm just going to rotate this this way 90 degrees and i'm just going to shrink this down and this is just going to be some eyes on our capsule here just so we can see that we're facing the right way ctrl d duplicate them i'm going to right click uh here and make a material i'll just call this black make it a dark black choose the smoothness and then drop this on the eyes and then i'm gonna duplicate this i will rename this to uh blue or whatever color you want it to be like a nice blue and i'll drop that on oh bean here so we've this is just so we can see the direction that our capsule is facing what you may want to do and what you need to make sure is that the eyes are on the side that's pointing where the blue arrow is so they're facing forward on our bean and then i'm just gonna set up our camera so for now let's just zero out our camera and i'm gonna go up to cinemachine create a virtual camera and i will press f2 and call this the player cam and i'm just going to drag our player into the follow target and drag it into the lookout target under body is going to be transposer in binding mode i'm gonna select world space and for aim i'm gonna choose hard look at then i'm just gonna move our camera into sort of the top down position that we want so there for example i'm going to turn off x damping and yeah we'll just leave that there for now okay so let's just make a folder here called materials drag these in and then i'm going to right click create a folder and call this scripts i'm going to come in here i'm going to right click create c sharp script and i'm going to call this twin stick movement and let's just open up this script we've got our script here and we're going to be controlling our player through the um character controller so i'm going to do a requires component type of character controller up here so when we drag this onto a player it will automatically add that and i'm just going to make some space up here and i'm going to do a couple of serialized field private floats i'm going to do the first one is our player speed which is our how fast our player is going to go i'm going to do a float gravity value and this is going to equal negative 9.81 f uh this is going to equal five f just gonna press ctrl d to duplicate that line uh we're gonna have our controller dead zone and just set this to equal to 0.1 f and then i'm going to set this to a thousand and it's going to be our rotate smoothing uh actually this is this is gonna be called the gamepad rotates moving and then we're gonna want a i'm gonna you don't need to keep this serialized but i'm gonna keep it serialized just so i can see it in the inspector easier and i'd have to keep printing out to the console and we're going to keep track of our current control method is a gamepad or not and then we need to get a reference to our character controller which i'll just call controller and then in the awake function we'll do controller equals get component type of character controller and we're gonna need some private vector twos to keep control of our input so we'll have a private x2 movement and the private vector2 of aim we need a private vector 3 of our player velocity and that'll do for now um now we need to just jump back to unity and i'm going to right click create and choose input actions i'm just going to call this um player controls and we'll just open up our player controls here i'm going to add two control schemes i'm going to add one is controller which is going to use our gamepad and i'm going to add a control scheme called kbm which is keyboard and mouse and we'll add in our keyboard and we'll add in the mouse and i'm going to choose all control schemes up here just so we can see that and i'm going to do control call this action map controls we'll have uh this action is movement and this can be type pass through and it's going to be affected too i'm just going to duplicate that and rename this to aim so for our binding our first binding here we're going to do the gamepad and the left stick and then we'll do the gamepad and the right stick then i'm gonna under movement i'm gonna add a 2d vector composite and we can call this wazt and then up it's going to be the w key downs the s key left is the a key and d is right and then under our aim i'm going to add a binding and this is going to be the position of our mouse this is the screen position of the mouse and so with our right stick gamepad i'm going to select controller as the control scheme and position is keyboard and mouse all of these are keyboard mouse and our left stick is the controller so i'm just going to save that and close this down i'm going to select it in the um [Music] project browser and i'm going to come up here where it says generate c-sharp class i'm going to check that and click apply and that's generated this player control script here which we can access in our twin stick script here so i'm going to do a private player controls called player controls and if you want to learn out like much more about the input system i really recommend a youtuber called samyam i'll put her link in the description below but she does a lot of really good videos covering uh everything you could think of with the input system yeah i'll link that below i highly recommend her channel to go watch that and so as long as our player controls we need a private player input called player input and i'm going to duplicate this line up here and say player input and then we need to do using unity engine dot input system for those to work so in the awake function we've got our controller we can also say player controls equals new player controls player input equals get component and this will be our player input component and because we're making some player controls here in our on enables function we need to enable them so player controls dot enable and in our on disable function we need to do player controls dot disable and we get rid of our start method because we're using awake and then in our update method to keep this organized i'm going to have three methods called handle input handle movement and handle rotation and we'll make these down here so we'll have a void handle input void handle movement and void handle rotation so in our handle input script um going to do movement equals player control start controls dot controls dot movement dot read value vector 2 so player controls is the name of our asset so we've got player controls is the name of our input action the actual map is called controls and the action name is movement so in your script here you've got play controls controls movement if you named your action map something different you didn't change that here if you named this like move instead of movement you type move here um and then the vector2 and the vector2 is his this is the control type so um that's where that naming sort of comes from in the script and we're going to do the exact same thing here with aim so we'll do aim equals player controls dot controls dot aim and this is also a value uh a vector two so we'll do read value vector two and we've got our input coming in here so now we can move our character i'm gonna do vector 3 move i'm going to set this equal to a new vector3 and we'll do movement dot x r in the x we'll do 0 on the y and movement dot z dot y in our z of this vector three so as we push up on the stick or wasd uh we're gonna read this into a vector3 called move and then with the character controller in unity we can just do controller dot move and then we can pass in where we want to move and the time dot delta time times with our player speed so where we want to move and how fast we want to move there and then just so we if we do get hit up in the air or anything uh this isn't going to cover jumping you don't you don't always jump in um twin stick shooters so this isn't going to cover jumping but if you do end up in the air like if you fall off a level you'll want to fall down so we can do play velocity.y plus our gravity value times with time dot delta time and then we want to at the end of this movement update we just want to do controller.move and we want to fall down uh the logic here for this movement script comes from um the unity documentation character controller.move a lot of this stuff's from here but changed so it works with the input system um so yeah if you just google for character controller dot move if you want to learn how to jump you can do that here where you add stuff to the velocity on the y before applying it uh but i've just got used these two lines to make the character fall down so that's going to handle our movements let's just make sure this is all hooked up and working if we go back to unity we've got our player here and if we drag in our twin stick shooter script onto our player you can see that's added the character controller it's added the player input which here we can choose our player controls and we've got our twin stick movement um script let's just hit play and now using wasd uh we're moving around and if we use the left stick on a control pad we will also move around there so now we need to use our mouse or the right stick on the controller to actually rotate and like look where we want to go so the first thing we want to do is we need to figure out whether we are using a gamepad or a control or the mouse and keyboard because if we're using the control pad the rotation is different than if we are using a mouse we have to do two different things to sort out the turning um so the first thing we need to do is do a public void on device change and this is going to take in a player input and i'll just call this pi for now and our uh is gamepad bool that we made earlier we can do is gamepad is equal to pi dot current control scheme dot equals gamepad if it does equal gamepad then our is gamepad is true and if it doesn't equal gamepad our gamepad ball is false so we go back to unity i'll show you this working so i grab a controller and turn that on but our player input here and we want all the switch to be on i'm going to change um behavior here to invoke unity events and i'm going to expand events and we've got our controls changed event here which passes through player input we can just hit the plus here drag in our twin stick movement script come down to interstick movement and do our on device change here so if we hit play now you can see here control scheme is keyboard and mouse so if i'm using the keyboard here to move around and then if i press the left stick on the controller we can see that swaps to controller over in that right hand corner under debug and because that says controller uh we put equals to gamepad we want it to check whether it's called controller um that's because in our control schemes i named it controller so what i'm going to do is i'm going to select our controller edit controller and i'll just rename that to gamepad so it matches our device save our asset and what you'd if if you do for some reason want your control scheme to be called something else or if you're detecting whether it's keyboard and mouse you need to reflect that here but i want it to be gamepad so i've changed it um within unity here i've renamed it to gamepad so make sure that's saved it should recompile once we click back into our project and we'll try that again we select our player over on the right hand side you can see control scheme keyboard and mouse but we want to be gamepad and then you can see that our gamepad ball here comes true when we're using the stick and it's false when we're using was d and this is really good here so you can fire off an event for example that your ui can subscribe to so when you swap so when you swap control schemes you could the ui buttons could change from uh like showing keyboard keys to xbox controller keys or vice versa okay so that's working there so now we need to handle our rotation now the first thing the first one i'll do is um [Music] our gamepad rotation so we can do if his gamepad is true then we need to uh rotate our layer i'm just going to copy and paste the stuff in i'm going to copy and paste the code in here and then walk through it so here we've got a controller dead zone and you can see that's uh 0.1 f which we've set up here and we're going to take the absolute value of our aim.x so with the left stick it can be on the x-axis it could go from negative one to positive one and taking the absolute value it just means that we don't have to care about the sign before it so we don't have to do uh negative aim dot x is less than negative control the dead zone or and then make it uh like you'd end up having a really long if statement so we just take the absolute value of aim.x so if it's negative one it'll be one and if it's one it'll still be one it just basically drops the negative sign if that's greater than our controller dead zone or the y is greater than our controller dead zone then we're gonna make a vector three called player direction and our right vector is going to be times by our aim.x and then we're going to add to that vector3.4 times by aim.y and then if our if the player direction dot square magnitude is greater than 0.0 f so if we've changed and we're moving then we're going to make a new rotation and it's going to be quaternion.look rotation and we're going to look in the direction that we are wanting to point with our stick here i'm going to rotate around the up vector and then we're going to set our rotation you can either set this directly to the new rotation if you want it instant or if you want a slow kind of like pan um so the character has to actually like turn around itself it doesn't just snap automatically you do um quaternion.rotate towards transform.rotation so our current rotation to our new rotation and then we've got our gamepad rotates moving here you've got to set it to a thousand to make it um fast i found it's just a good value that i found you may want to tweak it uh more so that should just work so if we go back to our twin stick shooter if we go back to unity and hit play so you can see here while we're walking around i'm using the mouse we're not looking if i use the right stick on the controller can you see what i mean about you have to rotate all the way around the axis so i'm pressing right now if i press left on the stick i don't automatically just snap to face in that way like you do with the mouse um i just feel this feels better on a controller because then you're a bit more direct con you're directly controlling it with the stick and it feels a bit more sort of a bit more natural if you wanted it to snap directly then like i said you'd get rid of you just set the rotation to the new rotation and that should do that or you could even make the gamepad rotate smooth in just even faster so it still goes through but just a lot quicker but i found a thousand is quite a nice um like a nice turn in speed so that's our gamepad handled and then down here we can do else so if we're not using our gamepad then we're going to need to shoot array through our camera to our mouse position to see where exactly we're aiming in the world so we could do ray ray equals camera dot main dot screen point to ray and then we'll pass in our aim vector which is our in this case it would be our mouse position um it wouldn't be the sort of vector from the stick and then we want to make a new plane and we'll call this the ground plane we'll set this equal to a new plane which is orientated upwards so the normal is pointing up and we will just do vector3.0 and we'll do float ray distance and we'll do if ground plane dot raycast with our ray i'm going to pass an array and we're going to pass out how far away the distance to that point on our plane and then we're gonna get the point from where our we're gonna get the point where our ray hits this uh plane so ray dot get point pass an array distance and then we're gonna look at the point which is gonna be a new function we're gonna make now so we'll do private void look at and this is gonna take in a vector three called look point and now because this point um could be kind of on any y level uh it causes some crazy kind of rotation so if you hit like a box for example the character with tip to look upwards at the box uh you want the character to not rotate to look up so we just need to correct the point so we're going to do a vector3 height corrected point and we're going to look at the positions x and the look points said but we're going to use our y position as the y of this and then we're going to do transform.look at height corrected point i did get this from a forum i can't remember exactly where it was because this is from an old project so this doesn't make sense to you it's me not explaining it very well because it's one of those where i know how it works but it's hard to explain how it works um it's kind of beyond the scope of the video but you can just copy and paste this and this and then it works it's i know that's not the most exciting kind of explanation if you wanted to learn more if you did want to learn more about it uh the unity documentation is the best place to do that so for example if you want to learn more about the plane um that we're using you can do that here this explains all about it same with the rays and the ray casting and the screen point array if you just look for that on the unity documentation you'll kind of figure out exactly what this is all doing here okay so with that said and done if we go back to uh unity and hit play so we've got our mouse and we're now looking towards where our mouse is hitting and we are looking on our controller where we want to look with the right stick and again we've got our wsd and we can move our mouse and look directly where our mouse is swap over to the controller and it works exactly the same way so that's it for this video i hope that was useful like i said if anything didn't make sense either you can even leave a comment below or go to the unity documentation and kind of read up more about it there the project files for this will be available on my patreon which is linked in the description below if you want to just download the project and get started with it that way you can do but in the meantime thanks for watching and i'll catch you in the next one bye
Info
Channel: Dan Pos
Views: 27,383
Rating: undefined out of 5
Keywords: unity tutorial, unity twin stick shooter, cinemachine, input system, unity input system
Id: koRgU2dC5Po
Channel Id: undefined
Length: 25min 40sec (1540 seconds)
Published: Mon Sep 27 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.