First Person Controller From Scratch In Unity 2019.x

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys Curtis here from swindle creative today we're going to be creating by request a character controller this is highly expandable and based on requests in the comments of this video we will expand the character controller even further adding functions such as ladder climbing swimming whatever you want honestly so let's get started here I'm gonna go ahead and create a floor for a character stand on I'm just gonna use a cube and expand it in a few directions like so I'm just gonna search for a grid texture or something along those lines let's use this one drag it on there I'm gonna give it a size of I don't know ten by ten should do the trick alright so let's create our character we're gonna use a capsule and we'll just stick it to the ground like that and we're gonna take our main camera and we're gonna parent it to our capsule we're gonna head up to this gear here in the transform we're gonna hit reset and that's gonna put our camera right in the middle of our character controller we're gonna just raise it up to around where its head would be if this capsule were or a human person or whatever controllable character the player is controlling just like so and now all we really need to do is while we're gonna add a few components and then we're gonna get to writing our script so the first thing we're gonna do is we're going to add a rigidbody component just like that and under constraints where you're just going to freeze our rotation in the XY and z axes and the reason we're doing that is just so that while we're moving around we're not rotating in any way that we don't want to be so we can close each of these we don't need to look at them and let's make a new component and I'm gonna call it the Swindell FPC for first-person controller and we will be making the third-person controller as well in a upcoming video so we're gonna click create and add and once that pops up we can get started here we go I'll just make this full screen so it's nice and easy for you guys to see I'll zoom in a bit here as well okay so the first thing we can do is tidy this up a bit I'm gonna get rid of the comments and we're gonna get rid of the start function just like that and we don't need any of this I'm gonna get rid of all of that and we have a nice clean thing here to work in so why don't we first define our variables so the first thing we're gonna need is a public whoopsies transform and that's going to be our camera and the next thing is our rigidbody which doesn't need to be public actually because we can just we could grab it let's not grab it at start let's just for simplicity sake public rigidbody spelled it wrong we'll just call it RB just like that hopefully my terrible typing gives you guys time to follow along easier I don't know I'm probably just being hopeful so next we're gonna get a public float and this is going to be our cam rotation speed just like that we're gonna set that equal to around 5 just like that 5 F for float next we're gonna make a public float called camera minimum Y and set that equal to negative 60 F and we're going to make a public float camera move up sees camera maximum Y and that's been equal to 75 F so what we're saying right now is that our camera is going to rotate at a speed of 5 and it's going to be locked in the minimum and maximum of the vertical access to negative 60 down and positive 75 up and that's gonna stop us from having a camera that can rotate in a full 360 degrees which would look like you were doing front flips or something so next thing we need to do is add a public float called rotation smooth amount actually you know what let's it would make more sense if we said rotation smooth speed we're gonna set that equal to 10 now let's handle our like movement stuff so public float walk speed I see I've made a mistake but I'll fix it in a second let's set this to nine and I'll just fix this real quick and then we're gonna get a public float run speed set that equal to 14 and okay so public float max speed that's gonna be 20 and the reason we need a max speed is because we need to control the the method I'm gonna be using for our to add force to our rigid body is going to need to be clamped so that our speed doesn't get out of control when we're pressing both the vertical and horizontal inputs so that's the purpose of the max speed the next one is going to be a public float and we're gonna call it jump power and let's make that 30 okay next public float extra gravity so this is basically the reason we're doing this is because by default Unity's systems gravity is just not enough it's not doesn't feel realistic doesn't feel snappy it actually feels very slow so I like to do this you know you can change it in the settings as well but if it's right under a character controller we can kind of individually adjust how much gravity is being applied to objects in our game and I really like having that amount of control so that's why I do it this way the next thing is a doesn't need to be public and it is going to be our body rotation X and then we have a float called our cam rotation Y I'm just making sure that I'm spelling everything right here okay and then we have a vector3 and this is going to be our direction in tent X and we have a vector3 Direction tent why just like that and two more things I believe we have a float for our speed because our speed variable is gonna change between our walk speed and our run speed that we've previously dialed in and then down here we're gonna have a public variable a public pool called grounded and sometimes other scripts that we're gonna make later we'll have to like check if the character is grounded so that's why we're making it public so now inside of update we're going to call some functions that we haven't created yet but we are going to create so actually why don't we just go through the functions one by one and then we'll remember to call them hopefully so the first function is going to be our look rotation function just like that so the first things we're gonna do is actually lock our cursor so that it's not you know flying all over the place while we're playtesting our character so we're gonna say cursor dot visible equals false and we're gonna say cursor dot lock state equals cursor lock mode dot blot just like that so what that's gonna do is stop the cursor from being visible in play mode so I'm just gonna write some comments here to help you guys in case some of your beginners I want you to make sure that you know what's going on so the first thing we're gonna do is we're gonna get the camera and body rotational values yeah that looks right so we're gonna get our body rotation X that we kind of declared up top and we're gonna make it plus equal to input oh my god I'm so terrible at typing sorry input.getaxis and it's going to be Mouse X here we go x cam rotation speed just like that and we're going to get cam rotation y plus equals two input dot get axis mouse Y that is also gonna be multiplied by our cam rotation speed right here and then what you can do if you are OCD is make sure that all of your equals signs line up you don't have to I like it I think it makes it look really nice just like that you know just it's nice locally to have ones that are close together be even it doesn't matter in the grand scheme of things um ok so the next thing we're gonna do we are going to stop our camera I don't know why I'm certain capital letters that's ok from rotating rotate in 360 degrees so this is where we're actually gonna lock our camera according to those values that we had earlier so we're gonna say cam rotation y equals mass F dot clamp and we are clamping our cam rotation Y and we are clamping it from our camera minimum Y to our camera maximum Y just like that so there's our clamps our camera will no longer be able to spend in 360 degrees so now we're going to create the rotation targets and smoothly rotate the camera and body towards them so create rotation targets and handle rotations of the body and okay so we're gonna create a new quaternion and we're gonna call this cam target rotation and that's going to be quaternion dot Euler and so that's basically takes a vector three like that and we're just gonna replace the first one there with negative cam rotation Y okay so and now we need to create one for the body so quaternion what's going on here come on for some reason my suggestions are all messed up okay it's not doesn't look like it's doing me any favors here body target tation equals return Ian Euler and then this one we replace the Y value so the middle one with our body rotation acts just like that okay so the next thing we need to do is actually handle the rotations so we're going to say transform rotation equals quaternion dollar so we're gonna smoothly move from our transform dot rotation to our body [Music] target rotation and we're gonna do that by time.deltatime x our we can be a variable for this right we're taking the smooth speed yep perfect and now we're going to say camera dot local rotation equals quaternion LARP camera local rotation to our cam rotation cam target rotation by time.deltatime x tation smooth speed just like that and now if we in our update if we call this function by typing in look rotation just like that with the two brackets and if we save and we get out of here go back into unity let it compile it's doing this little loading thing down there in the bottom right hand corner we are gonna have to drag our camera onto the script and drag the object itself into rigidbody to define the object with the rigidbody we're gonna hit play we should be able to look around there we go so we have a good-looking script we can only look that low and of course you can change that value based on the camera minimum Y so if we set that to negative 90 we'd be able to look perfectly straight down and if we set it to something like negative 45 we would only be able to look down about this much so let's head back into our script and let's write our next function so our next function is going to be our movement function so for movement the first thing we need to do is we need to make sure that our movement Direction matches as our whoa I don't know what happened to my speech just then our camera no our movement Direction has to match our cameras forward direction so Direction must match camera direction so we got a little trick a cool little trick to make sure that this works so we're gonna say our directional intent X is going to be equal to camera dot right and then we just have to fix a couple things so directional and ten x dot y equals zero and directional intent X dot normalize just like this with the capital n it's gonna call a little hope we need a said there there we go it's gonna normalize that for us and we're gonna do basically all of this same stuff so we'll just copy and paste it we're gonna change these X's to Y's we're gonna change camera dot right to camera dot forward on this one there we go so we now have our in the intentional direction that we want to travel in the next thing we're gonna do is we're going to change our characters velocity in this direction so we're gonna say RB dot velocity now this is kind of a long string of code so try not to get I'll try and make it super clear so RB dot velocity equals our directional intent why okay so our forward intent x input get access okay now this is our forward one so we're gonna use vertical so that's our up-and-down arrows or our W and s keys depending on how you're using how you're gonna play your character we're going to multiply that by speed okay now we need to add our next you know movement so we handled our forward backwards direction now we're going to add our side to side direction so plus directional intent X side to side x input dot get it oopsies get nope get access horizontal x speed and we are going to now add our final direction which is just going to be vector three dot up and we are going to multiply that by the rigid bodies velocity dot y so we're just gonna basically keep our velocity up and down the same and now all we need to do is clamp our velocity within our max speed so we're gonna say our B dot velocity equals vector three clamp magnitude and we are clamping our our B dot velocity and we are clamping it by our max speed just like that alright so now if we call movement up in the update function right here whoopsies in the second movement just like that don't forget the bracket save it and head into unity let it compile again once it has we should be able to hit play and start moving around except for we can't because our speed value is zero so we need to make sure that we're actually setting our speed properly so what we're gonna do is in our movement function here we are going to control our speed based on our movement state so we're gonna say if input dot get key where's Kiki keycode dot left shift so if we're holding the shift button then speed equals run speed and I'm just gonna copy and paste this down like this we're gonna say we're gonna add a little exclamation point right here to say if we're not holding down our left sheet our left shift key then we're going to say that we are going to be moving at our walk speed so now if we head into unity after saving if we head into unity and let it compile we should be able to hit play we should be able to move around and sprint and look around so here we go moving around there's our sprint it's nice and smooth everything works really well just so that it's you know clear how we're moving around I'm just going to give us a little bit more space like that and I'm going to just give us like a a wall back here something like that maybe I'll even give us another one oh I did not realize that was at an angle okay I guess we'll fix this one too there we go so now it should be easier to see what's going on when we're moving around there we go so next thing we need to add would be what probably ground check I would think let me think no because our gravity is still so unity has this thing where rigidbody gravity is like really weak and so mathematically it should be a replication of real life but it very much isn't so we're gonna create a little bit of extra gravity just to make it feel a little bit more snappy so new function void extra gravity just like that so what we're gonna say inside of extra gravity it's it's one line of code it's super super easy we're gonna say RB dot add force it's gonna be vector three dot down times and we've already made this variable extra gravity just like that and now if we call it inside of update right here extra gravity then it's already working perfectly for us so the next thing we're gonna do is we're gonna create a ground check to make sure that we're grounded so we're gonna say void oopsies around check just like that and in here we're gonna make a ray cast hit we're gonna call that round hit we're going to say ground it which is our bullet we've already made equals physics dot ray cast just like that and we're just going to say fit it so it's gonna come from our transform dot position and it's going to be moving in downwards so minus transform dot up so that's down because transform not down doesn't exist we're gonna say out grounded ground hit there we go and we're gonna send our array length to 1.25 F just like that and that should give us an accurate accurate representation of whether we're grounded or not so let's just make sure that that is being called here in our update whatever update is here it is so we're going to call round check just like that and now we need our actual jump so void jump here's our jump function so we're gonna say RB for rigid body dot add force it's gonna be vector three dot up times jump power and it's gonna be force mode dot impulse just like that so now all that's taken care of we have really just one thing left to do which is call our jump so it's the only function that's gonna be called a little bit differently so we have to say if if grounded and input dot get button move down jump so if you press jump and you're on the ground then and only then we can jump and with that we'll hit save head into unity and our character controller 'made 100% from scratch should be pretty much ready to go and completely usable so here we are here we have movement in every direction here we have our jump here we have our running jump I'll just go ahead and create some obstacles and stuff for us to jump on so let's just zoom in here we'll make some some sort of platforming little thing here so we'll copy this something like that I guess and then one other thing we can do is we can test how it works on slopes so I'll just do something like that and we'll just jump around a little bit so here's a slope you can stand on it we can move up and down it easily no jitter going on here's some simple platforming so yeah there's a custom character controller hope you enjoyed and and you know what like on my on this computer the Mouse looks script is a little bit hard to control so what you can do is you can just go into the settings and I'm going to set my smooth speed to something like I don't know like 15 and I'm gonna choose my mouth mouse mouse rotation speed camera rotation speed to for something like that feels a lot better a lot easier to control that feels really good so anyways I hope you guys enjoyed this video I hope that it's helpful and I hope you look forward to future videos where we expand on this concept make sure you leave comments telling me what you'd like to do what you'd like me to show you how to do with this specific character controller what functions would you like to see while running double jump like let me know and I'll take care of it I know how to do all that stuff so just tell me what you're interested in and don't forget to throw a like on this video and subscribe if you're new to the channel thank you so much for watching and I'll talk to you guys next time
Info
Channel: Swindle Creative
Views: 22,023
Rating: undefined out of 5
Keywords: unity, tutorial, first person, player, move around, camera look, move in unity, easy move
Id: DzO270DH0tk
Channel Id: undefined
Length: 26min 45sec (1605 seconds)
Published: Tue Nov 19 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.