Unity 2020 | Rigidbody FPS Movement Tutorial | Pt. 1

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey what's up guys um so today i'm going to show you how to make an fps movement system and hopefully by the end of this video you should have something that looks pretty much like this so you can jump around pretty smooth movement you can slide um you can slide down ramps and you'll gain speed you can also launch off the ramps and uh yeah so hope you enjoy all right so i've just made a new urp project here i've added some assets to test the movement with and um this this movement system in particular will be using the new uh unity input system um you can however completely adapt this to use the old input system if you if you'd like um but keep in mind that that system it i'm pretty sure it'll definitely be around for uh you know quite a long time however at some point it will be phased out so i'm going to show you how to install the the input system package here so you're going to want to you're going to want to go to window uh and then package manager and then that'll bring you here to your package manager right here you can go to the unit unity registry then you want to scroll down and find the input system right here and then just click install now it will ask you to restart your editor so i'm going to do that really quick and i'll come back all right so i've just restarted my editor and now we have the input system installed as you can see here version 1.02 now uh all right so first things first i'm gonna get started by creating an empty object this will be our player container this is what uh this is what's gonna hold our camera our player object you know everything that relates to the player so we're going to make that first and then reset the transform so it's not off on the middle of nowhere and then i'm going to just create a new 3d object i'll do a cylinder for now i guess and then i'm gonna move this up by one so then we can move it from the base i just find it easier to do it like that um just to be able to pivot it from the bottom there all right anyways i'm gonna name the cylinder player then i'm going to add a rigid body to this and i'm just going to up the mass here a little bit and i will explain this later on why i'm doing this but um yeah i'm also going to turn on interpolate here this will basically just um slightly delay the uh rigid body from from moving if i'm correct um if somebody has a more detailed explanation of that please put it in the comments um but this this helps with uh camera sync camera synchronization issues um which personally i had a lot of when i was first trying to make this so um we're also gonna freeze a rotation here just so you know we don't start rolling around um all right so now we're gonna make a an object to hold our camera so we'll just call this camera um reset the transform there we're gonna also drag the camera to be a child of this camera object i'm gonna reset that transform and this is good for now we'll add some we'll add something here later um now i'm gonna add two empty game objects here and i'll explain what both of these do here in a second so i'm gonna name this one head and basically you just want to position this wherever you want your camera to be so this will basically be the viewpoint that the camera goes to and um you know where you see from so yeah we'll set up the head there this this this is basically the only purpose for it right now and then i'm going to name this one orientation and this will be the orientation of your player um yeah just basically wherever you're looking you know on the horizontal um side of things you know left right um yeah stuff like that all right so now let's set up our controls with the new input system all right so uh do this wherever you want i'm just going to right click here in the file explorer and go to create and i'm going to let me see if i can find it here at the bottom here i'm going to make a new input actions and i'm just going to call this controls then let's open this up by double clicking it all right so these are your contr control schemes right here so i'm going to make a new action map and i'm just going to call this gameplay and i'm going i'm just going to delete this one and just completely make a new one all right i made it i'm making a new one here and i'm going to call this uh movement and i will uh right click on the movement tab here and add a 2d vector composite and i you can name this whatever i don't think it matters that much um delete this binding here too all right now i'm going to put our bindings here this is you know wasd basically you're up down left right so i'm going to assign w here assign s for down a for left and t for right all right so that's done now let's make another one here i'm going to call this one mouse and we're going to have to set this one to be a value of type vector 2 and then here we're going to go to the mouse the mouse you know uh tab here and we're gonna click delta this will just you know this is just your mouse position and relation on the screen um [Music] yeah this this should be good for now we'll come back to this so now i'm just going to press save asset up here at the top and make sure you come up here to the top right and you're exp in your inspector and make sure you click on generate c sharp class and press apply this will just generate a class that you can use in your scripts to basically mention your controls all right so i'm going to make a new c-sharp script here and i'm going to name this the player controller because that's what it's doing is controlling the player all right i'm going to open this up as soon as it compiles all right here we go um already zoomed in all right let's start with our mouse look so i'm going to start by making a couple of fields here at the top and make a start with a header actually because this little this is more organized and this basically just separates your what's going on here header is not valid it's only that i don't feel difficult oh right all right sorry mouse look settings and then we're just gonna make a private transform here um player camera i'm just going to initialize this to null and close field basically just exposes this value to the editor um it's it's like making a public variable however this still restricts the access to the script um so now i'm going to make another one uh serious field private transform orientation equals null um these are pretty self-explanatory this will just be your you know your player camera and this will be the orientation uh the object we just set up a couple minutes ago now i'm also going to create a new instance of our controls here so i'm going to make it private controls now this is referencing that script c sharp script that we just generated with our input actions asset i'm just going to name this controls and then i'm going to just delete these two functions here we'll make them again later and i'm going to make an awake function and in here i'm going to say uh controls equals new controls this will just create a new instance of that script within this scope so we can use it here and to use the new input system we're also going to have to make on enable methods um or on enable now and disable methods so right here we're going to say controls.enable this will just enable your controls and then we're also going to do on disable and do controls dot disable this will obviously disable your controls alright so now we should be completely able to use these yeah all right so first we're going to make our update method and we're going to write a method to gather our inputs so let's call this update inputs and we'll make a new private void update inputs and in here we're going to say um let's let's go up here and create a couple of vector twos so we're gonna we're gonna make a new vector to call this um move input and we'll just initialize that to vector2.0 and then here we're gonna make another vector2 and we're gonna call this mouse input and also initialize that to a vector 2.0 this is basically just an empty vector too all right so in our update inputs method here we're going to we're going to assign our move input to be controls and then we're going to reference whatever our control scheme that we named so in my case it'll be gameplay and then here we're going to reference what we put that 2d movement vector in earlier so i'll show you right here this will be our movement right here right this is where we put the 2d vector so then we're going to open this up and we're going to say dot movement and then we're going to have to this isn't enough uh i don't know why you're going to have to put another thing here at the end dot read value this is a method and this is basically what we want to read what we want to you know read our value as so we're going to do a vector 2. um and this will basically just store your wsd inputs in vector2 and you know move it and now we're going to do basically the same thing for mouse input so we're going to do controls dot gameplay dot mouse dot read value vector 2. all right cool now we have our mouse there um let's also make a sensitivity variable here too private vector2 sensitivity sensitivity equals new vector2 um i'm just going to initialize this to 2020 this is what works for me it'll probably be different for you now here we're going to just we're just going to multiply our mouse input right here by our sensitivity we're also going to multiply by time delta time and um yeah that should be good for now i might do a since i'm gonna do a sensitivity multiplier this is um not needed this is uh personal preference but this is just what i like to do um do like 0.2 f all right then we're also going to multiply this by the sensitivity multiplier all right so this is basically our input set up now we can we can get started on the mouse look so we're going to need a a a variable here to track our x rotation um so we're just going to make a new float here we're going to call this x rotation and just initialize that to zero now we're going to make a new method here and put this in update and call this update mouselook all right and then let's make that here private void update mouse look all right so first we're going to want to store our current rotation of our camera so we're just going to say um rotation equals player camera dot local rotation dot euler angles or euler angles um this will be a vector three i'm just gonna redo this to a vector three for clarity reasons um typically i just put everything in vars because it's just faster for me but um personal preference uh preference completely up to you so we're gonna make another float here and this will basically be the the x position we want our camera to move to so i'm gonna name this x2 and say this will be our rotation uh which is our current camera rotation dot y plus our current mouse dot x mouse input dot x yep there we go all right so now that we have this we can say x rotation minus equals mouse dot y so if you were to do plus equals here uh sorry mouse input dot y uh this will basically invert your uh your mouse input which obviously we don't want so we're gonna we're just gonna subtract it and then um yeah all right let's let's start editing our rotation now for for our objects here so we're going to say player camera dot local rotation equals quaternion dot euler whatever can never pronounce it correctly and quartonians are confusing excuse my language um i i honestly i don't really understand them that well so i i don't um i don't really know how to explain this too much but this is basically here i'll just read this to you returns a rotation that rotates z degrees around the z-axis uh you know whatever this will basically just apply a rotation um in a way that you know we can understand so we're going to say our x you know what x rotation we want is going to be our x rotation and then our y is going to be our x2 and then i'm just going to set the z here to zero and then we're going to do orientation.local rotation we're going to do another quaternion dot euler if i could type and then right here we're just going to do 0 x 2 0 and this should be working now so let's just save and then i'm going to head back into here and let's let's try it out okay so it's not working all right completely sorry about that uh i forgot to do something very important we need to make a script here first we need to name this the follow cam this will this is a very simple script this is basically just gonna follow your player around so we're gonna do sirius field private transform target or player you can name it whatever you want then we're going to make an update function and we're just going to say transform.position equals target position this will just you know track whatever the target is so we're going to go back into our editor here and we're going to throw the follow cam onto the parent camera object and we're going to set the target to be our head on our player so this will follow the head and then here on our player uh me being an idiot completely forgot to put the player controller script on here so obviously it'd be a good idea to put that on there uh now we're going to put our player camera here this will be your parent camera object and we're also going to put the orientation here all right now now it should work all right yep here we go so it's working can't move the mouse isn't locked to the screen which we can fix it's very easy to fix but yeah we can look around but however one issue you may notice is that we don't have a restriction at all to how far up and down we want to look so we're going to fix that too so let's head back into our player controller here and we're going to go to our wake function and we're just going to say um cursor dot lock state equals cursor lock mode locked this will just lock your cursor to the center of your screen so it's not you know flying all over your monitor when you're just trying to move and now we're going to clamp our our angle here so we're just going to make a new serialized field we're going to call this uh we're going to make a float and we're going to call this max angle and i'm just going to set this to 90. and right here we're going to want to clamp our x rotation uh okay so x rotation is basically your up and down um we're just going to clamp our x rotation here up and down to negative max angle our minimum and then max angle as our maximum and this should just fix the problem of you know spinning around uh without any restrictions whatsoever and then we should also have our mouse confined to our screen now so let's see if that worked yep that works so now we have restricted movement on the up and down and we can turn around just fine everything's working normally alright so now we can get started on the movement all right so i'm just gonna start here by creating some movement settings so we're gonna go up here make a new header call this movement settings and i'm going to make a new serialized field of type float and name this uh move speed let's just set this to 500 because this is going to be need to be a decently high value here because we're going to multiply by time delta time and you know what i'm also going to add another float here we're just going to call this move multiplier this is what we'll multiply our move value by just in case you know we don't want to set it to something like 5000 you know to get some decent movement going here um yeah so we'll set that up first uh now let's make a fix update function so down here i'm just going to make a fixed update this is the internal physics uh internal physics update on unity or for unity's um you know script execution order and i'm going to put a method here that we haven't yet created i'm going to call this update movement all right and let's just make that down here private void update movement so first things first we're going to want to make a new vector3 here this will be the the direction we want to move in and this is where our orientation comes in so remember what i said earlier about the orientation basically being where the player is looking uh horizontally um and this will you know define obviously what direction we're moving so we're gonna say orientation.right this will be uh you know relative to the orientation we're going to multiply this by our move input um dot x and then we're going to add orientation.forward times move input dot y this will be the direction we want our player to move in now we should just be able to add force oh i haven't even made a reference to the rigid body all right i'm just going to go up here and make a rigid body reference called private rigid body rb and i'm just going to go and awake here and i'm going to say rb equals get component oops rigid body all right there we go now we have a reference to the rigid body so we're going to say rb to add force i find that this has much more natural movement um compared um to other methods you know is like transform.translate and you know what not anyways um we're going to say direction so the direction we want to move in so this will be the direction of our force now we're going to multiply this by our move speed times our i'm just going to put this in parentheses for clarity reasons you don't have to do this move it uh move speed times our move multiplier so this will be how fast we want to move time times time dot fixed delta time this is um basically delta time but it's for physics essentially that's yup at the interval of in seconds at which physics and other fixed frame rate updates are performed all right so uh yep this this should be good for now so let's let's go ahead and test let's test it out let's see if we move now you're gonna notice immediately that it's slippery it's very slippery and we'll we'll figure this out um i'm gonna explain why this this happens and when i'm well i mean it's kind of obvious we're using add force here so it's adding it over time and we don't really have any drag um and i'm i personally i'm not going to use drag because um in my experience i don't know if this happens for other people or whether i was just doing something wrong because you know i'm not the smartest um uh it caused camera sync issues so it'd be very laggy like choppy when i was moving and whatnot um so yeah what we're gonna do is we're just gonna go to our materials folder here if you don't have this you can just make one pretty easily and i'm going to make a new physics material and i'm just going to call this ground mat we're going to take off all the dynamic friction all the static friction just make it zero zero zero and you wanna put this on everything you want to walk on or that is going to be ground i also recommend putting this on walls because your player might just get stuck in the walls when you're trying to move them um you know stuff like that anyways so that should be good now um it'll still you know have the same slippery movement but we're gonna we're gonna fix that now let's let's make a new all right let's make a new field here which will basically be our drag let's see here drag and i'm just going to set this to 230 this is what i found worked uh works the best for me um and we're gonna make a new function here call this put it right under here call this private void apply drag and we're gonna need to give it a direction in which to apply the dragon so right here we're going to say vector3 direction so this will be an input variable and then we'll just we'll just do we're going to check first if we're moving so if the rigid body velocity magnitude which is just the length of you know the current vector is not equal to zero so we are moving uh in this case we're going to want to add force in basically the opposite direction so this will be the direction we input we're going to say direction times drag times time dot fixed delta time and this will just apply drag in the opposite direction that we're moving and up here in our update movement we're going to call this function we can do it right here and we'll just say apply drag negative rb.velocity dot or no yeah just our b dot velocity so this will send in our rigid body velocity uh so our velocity in the current direction we're heading in and just return it negative so this will be the direction we want to apply and so obviously if we're applying drag we're gonna want to apply force in the opposite direction that we're moving so we slow down um so yeah that should that should start to slow us down a bit then if this if this value here of drag doesn't work for you then you know obviously change it how you wish so yeah as you can see now we got some pretty pretty smooth looking movement for now now i i would if you don't like this obviously play around with the values more the higher the drag you get the more you know the more uh snappy your movement's gonna feel because you're gonna slow down faster um for example if we set this to like 300 you know you stop quicker um yeah so there's that uh what we're also going to do hold on i'll show you really quick what we're going to want to do i'll demonstrate this so right now there's literally no restriction under speed so you can just keep going in one direction and speed up and speed up and speed up and speed up and speed up and speed up um and you know you don't want that yeah you don't want to just move in one direction and hit like mach 2. so let's let's set that up we're going to go in here we're going to say make a new new field up here we're going to call this the max speed i'm just going to put this under the move multiplier here it's going to say private float max speed and personally i like 20. you can change this to whatever you want that's what i found worked work for me and now we're going to go into our update movement function and we're going to um all right so what most people here would do is that they'd adjust you know the velocity of the rigid body um so it doesn't go over the max speed however what i personally like is zeroing out the the input um because in the future when we add sliding yeah i don't want my sliding to be capped by a move move cap i want to be able to build up a lot of speed and go really fast you know while i'm sliding down things so we're going to say here that if our rb dot velocity magnitude is greater than um max speed then direction equals vector3.0 so this will just zero out in the direction um that we're heading here and i'm just going to go and update here you don't have to do this i'm just doing this for demonstration purposes i'm going to write our velocity here magnitude to the console so we can see what's happening so yeah our magnitude you see it's not going really over 18 and this won't be exactly on what your max speed is but however the max speed will be you know applied at 20. but as you can see now we're not like overly speeding up in any direction we're staying at a stable speed um which is what most people would probably want in their case so yeah there's that i'm going to get rid of this line here all right uh so let's get let's get started on jumping here so we're going to go to unity first i'm going to go to our controls i'm just going to clear the console here because it's cluttered and we're going to add a new action and i'm just going to call this jump and we're going to want to add a button here we're going to look for space and yep just assign space to there and save asset this will also update the script that it generated and now that we have that button we can go back into the script here we can say um so i'm gonna i'm gonna show you how to assign input actions i like to do this on start but i think it'll work just fine and awake all right we're gonna say here controls.gameplay.com so this is basically just the same way you reference this however we're not going to be reading a value here because this isn't a vector 2. you can think of this as basically an action right so we're going to want to say dot performed which this is an event and we're going to want to add a um basically a listener subscriber to this event so we're going to say plus equals context to uh on jump that's a method we haven't created yet but we'll create it here soon and um this will basically just call this method uh when you press it when you perform your jump key right when you press space so i'm going to go down here and i'm just going to put right under apply drag here i'm going to put our method here i'm going to call it on jump oops all right and we're we're going to add a ground check here but um i just want to make sure jump is working here so we're gonna we're gonna just for now we're gonna add force and we're gonna do vector2.up um this is just what i found is easier this will be one in the positive y direction and we need to make a couple more variables here and make a new header call this jump settings add a new serialized field call this private float jump force and what's a good value for this 300 is what i've set and we're also going to probably want a jump multiplier that we can change um like i said it's kind of the same thing with the movement here if you want to just increase it by more and we're also going to make a couple more here that i will explain at a later date or not a later date but here in a minute um i'll just skim over them what they basically do so we're gonna make an air movement modifier this is going to adjust how maneuverable we are while we're jumping and then i'm gonna make another float here call this in air drag this will just be the amount of drag we have while we're in the air you know while we're off the ground all right so back to our jump method here we're gonna multiply this by jump force then we're going to multiply this by just the jump multiplier for now i think this should work so we're jumping for now uh however it's barely even noticeable uh why is that oh we need a change of gravity all right um so yeah we'll figure it you just apply just play around with your jump force multipliers and whatnot but what's important is is that we got him jumping let's let's let's up this by a little bit we're also going to need to adjust their gravity settings here in a little bit um let's just let's just throw this up to 600 bucket so yeah however you can tell that the mo it's very floaty and we'll fix that it's it's like moon gravity right now um the the default unity gravity is you know the world gravity negative 9.81 or whatever it is um but we're going to change that i don't know how good this is to do but this is just something i'm going to do you can always change this for yourself um let's see here what am i what am i doing we're gonna go to project settings in our edit tab here and we're gonna go to physics yep that's what i was looking for i'm gonna change the y gravity here to negative 30. um and just make sure that saves control s and this should kind of help our gravity here we might go and add some more yeah that helps quite a bit we might go uh change our yeah we're going to add some more gravity as well on the script so there's that for now we're going to go back into the script here we're going to apply some extra gravity just because i think it feels better in my opinion so we're going to do rb to add for us this will be down so vector through that down times let's do time dot fix delta time and then times let's do 10. you can adjust that you can make that a field up there if you want to adjust it in the editor but what i found is 10 works just fine so we're gonna have 10 here and we're gonna yep that looks like it's helped out a bit so i'm just gonna raise the jump settings here you know five yeah there we go it's quite a bit overboard and we we're still a little floaty um yeah i would just play around with the gravity settings probably um we're also we're also going to be adding our dragon stuff here in a little bit so yeah so just play with play with the gravity if you want all right apply drag so we're gonna be so let's do oh right uh you know what we're gonna do that in just a minute first we're gonna we're gonna add a ground check we're gonna add our ground track here because this is kind of reliant on you know whether we're grounded or not right we need to be able to make sure that we're in the air if we want to apply in our drag so we're to make a bool this will return bull and we're going to name it ground check and we're just going to do a simple sphere cast a sphere cast is basically just a raycast except it comes down to like a sphere uh so it's it's bigger um so we're just going to say physics.spherecast [Music] transform.positions so this is where a player is and then we're going to we're going to have to add some settings here for ground check so i'm just going to add another header here call this ground check settings and here we're going to want to make a serialized field call this private float ground check radius we'll do 0.4 this is basically just how you know you know the radius of your sphere cast so how big you want your circle to be um and then we're going to make another one here this will be the distance at which we right cast so how far we can go so we're going to name this ground check distance just put that at one one unit one meter and we also we're also gonna need a layer mask so we're just gonna say private layer mask ground layer so this is just what's gonna be detected as ground all right so we'll save that and then we'll go back down to here and we're going to say let's say our ground check radius sorry that's the distance ground checker radius then um the direction in which we want to raycast so vector through that down and then we're going to need to output the hit info um we're not going to be using this right now at least actually yes we will be using this because we're going to need to we're going to need to detect the slope currently that we're on for later on for sliding so right now we're going to we're going to say out raycast hit hit this will make a new variable of type broadcast hit named pit and then right here we're just going to put our max distance or ground check distance and then we'll put in our layer mask ground layer all right so there's that um then we're just gonna return grounded um yeah this is this should be a ground check um so uh in our jump function now where do we put it right here um i'm just gonna say if ground check so if we're grounded then we jump because we obviously don't want to be able to jump you know over and over and over again without touching the ground first so we're gonna do that go back in here keep forgetting to turn up the multiplier and stuff so we'll just set that to five here and then we'll add our dragon stuff here in a minute so but just so you can see that you can't oh right completely forgot to make a ground layer anyways uh all right so you're gonna want to make whatever you want to be ground you know in the ground layers so we'll just come to here we'll select all this and we're going to go to add layer right here i'm going to put in a new user layer called this ground and then here here and here we'll just make this ground and then also on the player we're going to need to select our layer mask here for ground just ground the one we just made now we should be able to jump there we go so as you can see you know if spamming space i'm sure you can hear me do that you can't jump while you're in there so great that's working um all right well next our drag right all right so here's a drag function we're going to start to apply an air trac so we're going to say if not ground checks so if we're not on the ground then we're going to first add force um we're going to say a new vector 3 because so for now um so basically we're inputting our velocity right when we jump and we start to come back down this is obviously going to give us um velocity in the y direction so our gravity that we're manually applying and if we just said direction here it would apply drag also to our following speed so it would make us feel more floaty which obviously we don't want so this is why we're creating a new vector or vector three here and we're just gonna say uh direction dot x zero f because we don't want anything in the y direction we don't wanna be applying drag and then here we're also going to put dir uh direction dot z um and of course um we're gonna need to apply this by we're going to need it times this vector by our inner drag that we made earlier and then we're going to also multiply by time dot fixed delta time once again and then we're just going to return because we don't want to be applying drag here um because you know this is when we're walking so we're just going to return early so we don't do that so all right that should slow down our jump when we're moving forward and back of course so as you can tell right now as you can see when i'm jumping you know i speed up a lot which is something that some people don't like so just turn that up and i guess that's uh is that working let's just go overboard here and check if that's working yep yeah how's it working uh you might just need to go a bit overboard here at the values however uh but yeah as you as you can see it's working working as intended all right so there's a there's a drag well let's get to let's get to crouching and sliding and stuff so we're gonna come in here let's see uh all right we're just gonna make a new header we're gonna call this the slide and crouch settings right now we're not gonna be adding any sliding settings because i'm not we're not implementing that just yet i don't want to create a whole ton of variables and just confuse you guys um so for now we're just going to make a vector3 which will be our crouch scale so this is this is basically just the uh an easy way to do crouching so we're gonna we're just gonna set that to half of our y scale right there and then i'm gonna also apply a crouch jump modifier because what you'll notice is that it kind of seems like we are going higher when we're crouching um which really you're going about the same height um however it just looks like it's more because you know your players your player's vision's half right so we're just gonna we're gonna make a crouch multiplier here we're just gonna do one f um and now we're also we're also gonna need uh a bull here so i'm gonna make a new bull private bull gonna call this just crouching this is detect whether we're crouching or not and we are going to need a new control so let's head back into the unity editor let's open up our controls here and all right we'll need a uh plier and air movement modifier as well sorry uh so we're gonna add our a new uh action here we're just gonna name it crouch and we'll we'll do left control left control this is personally what i like you can do c or you know whatever button you prefer um all right that should have updated our script and we're gonna head back in here now and this is essentially the same thing that we did before with the jump so we'll just say controls.gameplay.crouch.performedplus equals context to toggle crouch which is something we haven't yet made but we will make it and then this is a new um this is a new event that i haven't showed you yet so we're gonna do controls to do controls.gameplay.crouch.canceled um this will be when we release the uh the crotch key and we'll say we just want to toggle crouch false so we want to stop crouching we are also going to need a vector up here private vector 3. this will be our original player scale that we want to return to um when we're when we're not crouching anymore so i'm just gonna initialize that to vector through that zero and then in here on awake i'm just gonna say original uh sorry this shouldn't be original player virtual scale sorry my bad we're going to go down into a wake here like i said before we're going to say original scale equals transform.localscale so that'll just be our you know our original scale um all right so let's write our toggle crouch function here so private void toggle crouch bool we'll just call this enabled and now what we're going to want to do is say if enabled then we're going to want to set crouching to true the bull we just made and then we're also going to want to set our local scale our current scale to be our crouching scale and along with this we are also going to want to offset our current position so we don't get stuck in the ground we're going to want to offset it by creating new vector3 here and we'll do our current transform.position.x because we don't want to offset on the x at all but we do want to do transform.position.y minus 0.5 f and then we'll just do transport.position.c um and this this will be so you don't uh when it whenever you crouch you're not like in the air and then you fall um the witch is pretty ugly i mean i don't think anyone really wants that and now we're going to implement our [Music] uh on crouch uh so we're gonna do if if not enabled so else crouching equals false then we're also just gonna want to set our transform.local scale to our original scale and we'll want to do transform.position equals new vector3 this is just actually you know what it for simplicity reasons we can literally just copy this line and change the minus here to a plus this will just offset the player you know half a unit into the air so we don't you know come back and elongate and get stuck in the in the ground all right so this should be good for now we'll also implement sliding here in a minute um go ahead and test that out also going to we're also going to implement our crouchdown bonafire here in a minute um crouching will come into play later however i can see we're crouching all right cool that works so now that we've got that set up i'm going to implement the in-air movement modifier the uh the crouch jump modifier uh all that so let's let's start with the crouch jump modifier so we're going to go into our on jump here and um i'm going to use what is called a ternary operator so this is basically just a simplified if statement and uh in in simpler terms um so we're gonna just say basically this will be if crouching um and this is basically our if statement so if crouching is true then we're going to want to use our crouch jump multiplier and otherwise if if crouching is false we'll use a colon here which will basically act as the else else statement um and that'll just be our jump mod or multiplier so we'll just say if crouching then use our crouch jump multiplier else jump multiplier and this should be enough to just apply the jump modifier here this will be pretty noticeable i think considering our other see here's our normal jump and then here's when we tried crouch jump um of course we made this as a um as a setting so i'll set that to three and then you notice we jump a lot uh shorter than you do when you're standing which makes sense because obviously when you're crouching it's not like you can like slap squat into the air um so yeah that works uh now let's use our [Music] inner movement multiplier we're going to want to do that in our update movement function up here so we're just going to make a multiplier here for our movement so i'm just going to say float multiplier equals uh so we're going to use another ternary operator here uh simplified if statement so we're going to say if grounded so if ground check if that returns true then we're just going to want to keep our normal movements over on the ground we're just going to multiply that by 1 which will return you know the original value because if or so else an air movement modifier so this is just saying if we're grounded then use um one uh which will you know just give us the same value or we'll use our inner movement modifier which will make us less maneuverable when we're in the air so now that we have that we are going to come down here we're going to say where we uh times fixed delta time we're just going to do uh times multiplier and then we're going to times why the multiplier again which is just something i personally like to do um we'll do we'll do ground check if you're going to use another ternary operator here if we are [Music] for grounded and crouching so this will this will be for sliding later so if sorry about all the parentheses here i know it's a little bit confusing so for grounded and crouching then we're going to want to do zero because i don't want to be maneuvering on the ground uh or actually we can make this smaller we can just do something like point zero three whatever you can make that a um thing up there um like a variable if you want personally i don't think i need it and then we'll use our colon here or else and just say multiply so i'll just explain that because i'm sure it's probably confusing so here if we're grounded and crouching um which means we're walking while crouching uh we're going to multiply our movement by 0.3 which will reduce the multiplier which will make us walk slower while we're crouching or we'll just use the normal multiplier here right so if if that's not the case if we're just normally standing then it'll be our normal movement but if we're jumping it'll be this up here sorry an air movement modifier that's where that ternary operator comes in so now if we go back in we should be able to see that this makes us less maneuverable and slower while we're crouching so yep so it's it's kind of hard for me i'm holding a right now to strafe in air and then when we're crouching we move a lot slower which you can change this of course i might change this as well to maybe uh maybe like 0.7 yeah because i don't want to move much slower than i do when i'm in there you can also make this zero if you don't want your player to move at all when you're crouching um so yeah let's let's do let's do sliding now this should be pretty easy to implement um yeah it shouldn't be too hard so here we're going to want to say um so just this is going to apply um a bunch of force to us while uh while we're grounded and crouching um so basically when we're sliding we can make this more comprehensive later once we actually have more sliding um is to detect whether we're sliding so for now we're just going to say if ground if crouching and grounded so if crouching and ground check then we're just going to want to add a bunch of force down so it keeps us on the ground we don't you know like go flying off a ramp or something um let's just do 5 000 and then we're gonna want to return early so we don't uh apply all this stuff this will also make it so when we're sliding uh we can't move in other directions which i don't like so yeah there's that but that's not gonna be just enough to uh apply our sliding here so we're gonna need to go to our toggle crouch here and then say if enabled so this will be right when we press control we're going to want to say um if our rb uh rigid body velocity magnitude is greater than 0 at least so i'm just going to do 0.5 so we're definitely moving and going fast enough and we're grounded ground crown check then we're going to add force or we to add force we're going to do orientation so the way in which we're facing dot forward times a variable we haven't made yet i'm going to name it slide force and i'm going to scroll up here we're going to go to our sliding crouch settings and then right up here i'm just going to make a new serialized field called this private float slide force and i'll just give this a value of something like 600 i'll go to 600. anyways back down here oh we're also going to need to make another bowl here so i'm just going to comma and then sliding so this will just make another bool name sliding and i'm going to come down here where we're initiating our slide it says sliding to true so we know that we're now sliding um and then in our else statement we're also going to want to say sliding equals false oh that's slide four sliding equals false there we go all right let's see if this works yep it kind of works so ah here's a here's the fourth thing i was talking about so if you try jumping and on the ground you you can't but it's noticeable you'll you'll have to play around with the values to stop that but you notice you know this isn't me moving this is the force that's being applied to us when i crouch because obviously i can't move now when i'm crouching but um yeah is this now we're gonna come back into here and we're gonna make this more slippy kind of like a like an actual slide here um rather than kind of like scraping against the floor which isn't smooth um so we're going to come up here and we're going to make a new variable i'm just going to call this private sorry serialize field private float slide drag set that to 0.1 all right so now we're going to come back down here to our apply drag function and right under here we're going to say if crouching then add force our move speed times time.fixed delta time times negative our beta velocity at normalized times slide drag and then we're just going to want to return early here and this will make our sliding movement feel much more slightly i guess more smooth so let's see here yeah sort of yep this will be more noticeable if i uh raise this here that might be a bit too too much uh okay but yeah as you can see you know we're much more slimy uh i'll demonstrate again really quick by changing the drag this time say this is a drag on point one but if i raise it's like 10 you see we stopped much faster but if i raised it if i lowered it down to something like 0.01 we slide a lot more but personally i like 0.1 it feels like you slow down a lot quicker or towards that it feels like you speed up and then slow down a lot faster towards the end of your slide which is something i want um which means also we should be able to uh go down ramps now so let me just get up this ramp and start sliding yep there you go you can slide down ramps and this is um this is because of the extra force we're adding towards you know when we're crouched so we stay on the ground because normally you just kind of like kind of fly off of it um yeah that works now it's pretty fun uh all right so we're also just gonna really quickly just come down here and say if we're not already sliding in our toggle crotch function should have done that earlier but um this is just you don't i don't think you really need this here but it's just to make sure just to be 100 sure you're we're not adding this force in any other case however there's one thing i want to show you so our sliding can be abused right now in this state so for example you can just spam slide and uh this will constantly apply force because you know you're spamming your crotch key right so a way i found to do this is just to add a slide cooldown so let's head back into here go back up to our slide settings and just right under slide drag we're going to say serialize field private float um slide cooldown and i'm just gonna set this to one second [Music] then we're gonna come down to our update function we're just gonna say if let's see here if we're also going to need another float sorry let's make another float here private float time since last slide and then i'm just going to initialize this to mathf.infinity because technically we haven't slid before yet right at the time of creating this so we're just going to do that come down here back to update and say if time since last slide is less than slide cooldown time since last slide uh plus equals time dot delta time time dot delta time so this is just act as it like a timer this will just you know increment by the time between frames and to make this actually work we're going to need to come down to our toggle crouch function here where we apply our slide and we're going to say and time since last slide is greater than or equal to our slide cooldown and then we're also gonna want to set time since last slide equals zero all right and this should stop uh the sliding from being abused by adding a cooldown so yeah as you can see i can spam crouch now no matter how how much i do i don't consistently add force anymore and i just stop which is what we want um another thing we are probably going to want a slide jump modifier so um we can actually get some height while we're sliding you know while we're jumping so we're going to come up here we're going to say right under slide cooldown i'm going to make another series field because private float slide jump modifier or multiplier set that to two and a half then i'm gonna come back down what was it on jump we're gonna go down to on jump and then here our ternary operator this is probably going to get a little confusing um first off we're gonna need to know whether we're actually sliding or not um and we have enough speed um this is not needed however this is something i like because you will be able to gain a lot of speed when you're slide jumping right and typically i don't really want to do that because i don't want my player to be able to go super fast when they don't have enough velocity like i want them to be able to jump very high when they're going down a ramp and then they jump but if they're just normally sliding and then they jump i don't want them to you know launch so here we're going to make we're going to go up and we're going to make another new float we're going to call this one private float current slope initially initialize that to zero um this will just be the angle of the current slope we're on we can put that in our ground check because this l um you know we're always going to want to know what slope we're on as soon as we check whether we're on the ground or not and this just makes the most sense to me to put this in here so we're going to do some math in here we're going to say current slope equals vector how about i say like that vector3 that angle vector through that up and then hit dot normal the normal of the surface the right hit so this will just be um this will be our raycast hit here so the object we're hitting below us so in this case a ground object and this will get the current angle of what we're standing on so that'll be useful when you're going down a ramp right you can you can tell that you're going down a ramp so now we're going to go back to our on jump here and it's possible to basically nest a ternary operator within a ternary operator and you don't have to do this however i like to just confuse myself so this is how i'm gonna do it so here we're gonna we're just gonna get rid of this here and just keep the crouching part and say if crouching and current slope current slope is less than 10 then we'll do we're going to select our crouch jump modifier or if we're crouching and our current slope is greater than or equal to 10 then we'll do slide jump modifier or multiplier or just a normal jump modifier or multiplier and i know that's a long line so i highly recommend here just writing a comment as to what this does so for example i'll just i'll just write one out here and say if crouching and not sliding crouch jump multiplier if sliding slide jump multiplier and if all else is false normal jump multiplier this will just be easier when you come back to this and you're like what the did i write here um but yeah i'm also you don't have to do this i'll i'm just doing this for demonstration purposes again i'm gonna write our current slope to the console so we can just see that that is working um all right so now we should have implemented our slide jump modifier correctly um all right so here here we go as you can see our uh our slope there was higher than 35 and i guess it was applying our slide jump modifier there it's just you can't really tell because i guess the extra gravity but let's see here yep yep yeah that worked uh all right let's let's bring that down a notch um probably something like 10 would work pretty well here um maybe higher we'll find out yep 10 is a 10 is okay i'm happy with 10. so we'll do 10 here um yep there's that um we can let's do sprinting uh we can do sprinting and let's let's do uh yeah this should be easy actually really quick i'm just going to make this uh so i'm going to make a couple couple of variables here in the bowl section these are basically our states so i'm going to make a new state here their new bull will call it jumping i'm also going to make one called sprinting for the future so here we're going to want to change this to be a body um but yeah this works as well um you're going to want to set to you're going to want to set jumping to true here so uh what we're going to do here is we're also going to call on jump again what we're going to do here is that basically if you're holding space bar and i want i want the player to be able to instantly jump again if they're holding the space bar as soon as they hit the ground so it's not kind of like a spam fest of hitting the the space bar to try and jump again as soon as you hit the ground right you don't have to do this but this is something i personally prefer we're also going to say here uh controlsgameplayjump.canceled we're going to set jumping to false then we're going to want to have to come into our update movement here and we'll say if jumping and grounded ground check then we'll just call on jump and this will just make it so when we're holding down uh spacebar and we hit the ground we'll just jump again i'll show you that here now um not exactly sure when that changed however suddenly we jump very high so i'm not gonna i guess i won't worry about that too much um yeah that's kind of weird well anyways i'm not complaining i guess um but yeah here let me show you uh when we're holding jump just jump you know instantly again anyways um uh i'm sorry if that changes randomly for you too and randomly gets rid of all of your uh or kind of basically up all your values um hopefully it doesn't i don't know why i did that for me but we're also going to want to come in to apply drag here and say if we're not grounded or we are jumping then we're going to want to apply the force here right sprinting so i'm going to come up here and i'm going to set up a couple a couple new um we'll set up a few variables here so we're going to say serialize field private float sprint multiplier and i like 12 here i think is what i had it set to before um we're also going to want a setting here for max slope i'll explain that in a minute set that to 15 f and uh we're also going to want to adjust the max speed um we're going to want to basically modify it and add to it um because you know when you sprint it's basically you just right now it'll be you just basically speed up faster right but you don't you're not actually heading any faster than you were before so we're gonna need to add some we're gonna need to make our max speed higher uh all right yeah that should be all we need for now and we should be pretty much almost done here so we're gonna need to add a new control really quick i'm gonna come in here we're going to add a new button we'll name the sprint and i'll set this to my left shift key it's personally what i like and then save asset again um back to visual studio code and we'll add another event listener we're going to do controls that gameplay dot should be basically prowling this by now dot sprint dot performed plus equals context to sprinting equals true and then just like jumping we're gonna say controls that gameplay dot sprint dot canceled plus equals context to sprinting equals false not sure why that's processes here that should be performed it's probably because i spelled it wrong first um okay uh now this will basically detect whether we're sprinting now and we're going to want to come down to our update movement in here and woohoo more ternary operators i hope you're excited all right so here we're gonna this this is where we modify our movement right so here we're gonna want to set up a ternary operator so we're gonna want to check whether we're sprinting and if we are sprinting then we're going to want to use our sprint multiplier and if we're not sprinting then just a normal modifier that should be enough to just increase our you know speed here however we're going to need guess what yet another ternary operator up here on our max feed and we're going to want to say if we're sprinting um then just max speed plus or sprint max speed modifier or just normal mark speed all right and that should be enough to get us sprinting hopefully this has taught you some more about ternary operators because they're actually very very useful it's good to know how to use them but yeah as you can see now we're sprinting which is very nice um yeah so there's that and now i'm going to explain why we needed that slope thing so in games for example like titanfall you can or apex legends well that's probably a better example considering that's newer um games like that you can jump and if you come down while you're you know you're holding crouch you'll slide however in our current state if we just came down after jumping we'll just kind of smack into our into the ground become like a very heavy hamburger and we won't slide at all which is personally i don't like that but if you want to smash it in the ground and become a flat heavy lead hamburger then by all means you can skip this part and remove that other variable we made earlier um however i like to slide when i after jump so we're going to make a a new new uh method here and this is an internal unity one this will be on collision enter um so if you're unfamiliar with this this runs whenever a a collision gets detected on your collider or your rigid body um and right here we're going to want to write an if statement um this is going to be quite long um so here we're going to want to first check if we are grounded and if we are grounded and we are crouching and we are not we're going fast enough to be able to slide and our current slope is less than our max slope so this is making sure that we um current slope is smaller than the maximum slope and and we're not sliding already and time since last slide greater than or equal to slide cooldown there you go and guess what this is like the smallest add force thing ever this is basically exactly exactly what we did to add sliding before orientation.forward the direction we are facing times slide force um and now it's probably worth writing another comment here um which i'm going to do because um but this is pretty confusing so i'm going to say get player to slide when he hits the ground if if is grounded and is crouched and magnitude is above the threshold um i might just lower this or remove that word there so it's smaller because this is might be a long sentence here and the current slope is smaller than the maximum slope and we're not already sliding and not on cooldown all right all right so there's that and let's go play around with it now all right so we're jumping we hold control and we slide we're jumping and we hold control and we slide so it's pretty nice and it doesn't work if you slide into a jump either which is pretty pretty good sorry for a second there i thought the uh jumping was a little glitch and we can slide down this and jump extremely high because my values are all up um yeah um this is that's pretty much i think that's it yep i think that's it all right uh so thank you for watching i hope you learned something and i really hope that this wasn't too confusing for you um feel free to use this as you wish feel free to use this as a base um you know for whatever you want um this is um this is pretty much based off of danny's movement system the developer that's making carlson right now um that he helped me a lot with making this script um just being able to reference his code and stuff really helped a lot as well this is basically my modified version of it um but yeah if you have any questions uh feel free to ask in the comments um yeah if i'm forgetting anything you know comments um if you if you want to see something else uh you know just write a comment um you know tell me what you want to see next um i was thinking about doing a weapon weapon system um my next video but um if you enjoyed um yeah like like and subscribe or something like that thank you all right bye
Info
Channel: Wolfe
Views: 3,933
Rating: undefined out of 5
Keywords: Unity, FPS, Movement, Tutorial, Physics, C#, GameDev, Development, Coding, 2020.2
Id: bwFLLnhm4D4
Channel Id: undefined
Length: 82min 10sec (4930 seconds)
Published: Sun Jan 31 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.