First Person Controller - Basic Movement and Mouse Input (EP01) [Unity Tutorial]

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi guys mike here from comfort interactive welcome back to the channel today marks the start of a new mini series where i'm going to show you how to make a fully customizable fps controller now the reason for this is i actually have in my mind a few ideas for first person games that i want to do tutorials on so instead of repeating myself for each one of those series i thought i'll do this one first and then i'll just use it as a callback for any future first-person game that i do a tutorial on so i'm going to split this out into multiple different videos the first one this one is going to just be the base foundations for keyboard inputs and most luck and then we'll go into the more detailed things like crouching jumping all the way down to interaction and adding the head bob and what i'm going to try and do i'm going to try and make it so this is really modular so we're going to be able to toggle on and off all of these different features dependent on what you need for your games so i hope you stick around i hope this is actually going to be useful for you i know it'll be useful for future tutorials so it's not going to be completely wasted effort but before we jump into it i do want to thank gigatank 3000 for sponsoring this video i've got his links down in the description below go check him out on his website go follow him on twitter keep up to date with what he's up to and i also just want to thank everybody supporting me over on patreon you guys are fantastic okay so i've just gone and set up a completely empty project and i've deleted all these standard unity assets out of here all we have left is the directional light and the main camera that comes by default in any scene now the first thing i'm going to do i'm just going to reset that camera to be in the direct center of our project so all the zeros now we'll go and we'll create c sharp script and we'll call it first person controller now we'll open this up in visual studio so what we're going to do we're going to add in all the variables that we think we're going to need for this step then we're going to add in the empty methods and then we're going to tie all this together we're going to do one method at a time so first of all we're going to want a public property to actually determine whether or not a character is in control or a player is in control of a character rather so that's going to be a public bool can move we're going to make this a property so that's going to be a get and then a private set because we don't want to set this outside of this script i'm going to default that to true next we'll add in a header attribute just so we can keep this all nice and tidy inside of our inspector and these are going to be uh movement parameters so for this tutorial for this episode of it we're going to add in a walk speed and a gravity multiplier into here and we're going to do this properly so i'm not going to make these public i'm going to make these private but we still want to be able to amend them inside of the inspector so the way that we can do that we have the attribute serialize field private float walk speed and i'm going to give it the default value of three next we want our gravity multiplier so exactly the same serialize field private float gravity and i'm going to default that to 30. now we can amend these in the inspector at any time that we want next we want to define a lock parameter so this is going to be the parameters that are controlled via mouse locks we'll call these lock parameters i'm going to want four different ones in here we're going to want the look speed for the mouse on the x-axis and the y-axis and we're also going to want an upper and a lower lock limit because on any first person game if you drag your mouse up or down you're blocked at a certain point because obviously you can't crane your neck all the way back and look behind you upside down that would just ruin the player's experience so we want to limit how far a player can look so again serialize field but this time we're going to put a comma and we're going to add a second attribute in here and this is going to be the range so inside the inspector we can limit an upper and a lower number for these so for our lock speeds more often than not games have a look speed of between one and ten so we'll just add that in now if we finish this off private float look speed x and i'm just gonna default this to two for now and then we need exactly the same again for lock speed y now you could keep those both the same if you wanted them linked i always like having x and y separate even if i end up having them the same that's personal preference for me but some people just like that extra bit of customizability next we'll just paste that in again but this time we're going to limit this variables range to 1 and 180 and what this is going to be is going to be our upper lock limit and i'm going to set that to 80. so that's going to be how many degrees we can actually look directly up before the camera will stop moving and then we'll copy that call that lower look limit as well so that's going to be it for the variables that we can mess about with in the inspector but we do still need a few private reference variables inside of our script so these are just going to be private we're not going to mark these as serializable i'm going to need a reference to the camera call that player camera and we're also going to be using the character controller call that character controller so we need a reference to our camera so we can rotate it and we also need a reference to our character controller that will be attached to this game object so we can actually move a character about just a few more variables now we need a private vector 3 which is going to be our current move direction and a private vector 2 which is going to be our current input so our move direction will be the final move amount that we apply to our character controller and our current input is going to be the value that we're given to the controller via a keyboard so either through wasd or your arrow keys and finally we need one more private float and that's going to be our x rotation and the reason we're going to keep a reference to that is because that is the angle that we're trying to clamp with our upper and lower lock limit so we can keep a reference to that and then use math.clamp on that but we'll get to that very shortly so let's move on to the methods before we actually start coding this i'm going to change start to awake and the first thing we want to do is cache these two components so our camera and our character controller and you've seen this done before we're going to do player camera equals get component in children because our camera is going to be a child object of uh actual first person controller and inside the angle brackets we're going to pull camera now this could be done by serializing the components and initializing them yourselves by dragging them into the inspector but for stuff like this i just like using get component in either start or awake next a character controller is going to equal get component this time because this is going to be attached to the parent object and grab that character controller nice and simple next we want to lock our cursor so the cursor is no longer visible and it's locked within a game window so we can do that by setting cursor dot lock state equal to cursor lock mode dot locked and then we'll go ahead and hide it by setting cursor dot visible is equal to bolts so we've cached all our components we can now get working on handling our inputs so i'm going to wrap my entire update in an if statement and that's going to be if we can move so if we take away the ability for the player to control this first person controller we don't want to actually do any update functions on it that way we can actually force things like locked perspective cinematics and the such so inside here we're going to need three methods we're going to need to handle our movement input our keyboard inputs we're going to need to handle uh mouse inputs for look and then we're going to need to apply all of those final movements every frame so let's create these methods that'll be a private void handle movement input if i did all that correctly anyway there we go private void handle mouse lock and private void apply final movements and we can just call all three of these from inside of our update method remembering to put apply final movements at the end for obvious reasons so let's start with a handle movement input so this is going to be our keyboard controller so the first thing we need to do is actually calculate the direction on a vector 2 of a player's input multiplied by the speed in which we want to move so the way that we can do that we've already defined our current input vector 2 variable we can set current input equal to a new vector 2 and this is going to be our walk speed multiplied by input dot get axis and then in parentheses we write a string of vertical vertical being the w and s key or the up and down arrow so that's going to grab a forward and backward movement but we also want our side movements as well so a and d are left and right so again we'll just multiply walk speed by input dot get axis and this time we're going to pass in horizontal so that's going to get our left and right movements the next thing we've got to do is we've got to cash temporarily our direction on the y-axis so the actual vertical going upwards away from the floor or going towards the floor and the reason we've got to do that is because when we update the vector 3 for the position we want to make sure that we reset that y position back to its original value so to do that nice and simple we'll create a float move direction y and we'll just set that equal to move direction dot y and then we'll use that in just a second now we want to actually calculate the move direction so the vector 3 based on a character's orientation so we're going to be using transform dot transform direction and passing in forwards or left or right as well depending on which axis we're working on at that point so let's just go ahead and do that so that's going to be move direction will be set equal to and then in brackets we'll do a player's transform dot transform direction and pass in vector 3 dot forward to get our actual forward axis and then we want to multiply that by our current input dot x and then we we want to add to that again in brackets uh transform dot transform direction and this time we'll just pass in vector3 dot right and multiply that by our current input dot y so now we should actually have a vector three inside move direction which adheres to a current player's orientation so whatever direction they're facing in and then we're going to add together those values and our current input so if we're facing northwest for example and we press w we're going to move forward along that northwest angle and then like i said before we just need to remember that move direction dot y needs to be set to a cached move direction y so let's go ahead and save that go back into unity and we can actually set up some of this controller so what i'm going to do i'm going to create an empty game object call it first person controller center that back to zero and i'm going to put my main camera as a child of the first person controller so now that we have our main camera as a child when we cache get component in children that's going to grab our camera now we need the character controller now the character controller goes on our first person controller parent object so let's add that in and we already see we have we're given a capsule collider and quite a few different options down here now right now we don't really need to change anything inside of our character controller what we're going to do though we're going to grab our main camera and we're going to bring this up slightly just so it's towards the top of that capsule collider so we're not viewing the game from the stomach of our player now if i just quickly add in a plane for a floor and then we go over to our first person controller and let's drag our script on so there we go we can see we have a walk speed of 3 gravity of 30 and the rest of our look parameters if i just bring that down slightly so just like that we actually have our first person controller base object ready to go all we need to do is remember to apply all of these movements inside apply final movements and then once we can see that uh our all keys are working we can get working on the mouse lock so in this one i wasn't gonna do it but i will apply gravity now that won't actually come into use until we do the jump tutorial but it is good to have it in there so let's first apply gravity so if our character is not grounded so that'll be the exclamation mark we'll get our character character controller and we can call is grounded on that so if we're not grounded we're in the sky we can set move direction dot y minus equal to gravity times time dot delta time so that's going to pull us down towards the nearest surface next we'll apply all of those movements that we've calculated inside handle movement input but to do that we get a character controller that we've already cached we can call move and then inside move we'll pass our move direction multiplied by time dot delta time so if we save that hop back over into unity and hit play we won't have most controls at this point but we can see our character moves about and if we uh walk backwards off this plane gravity takes over nice and simple all right let's work on this mouse lock for now then so this actually isn't that difficult what we're going to do we're going to first determine what our y-axis is on our mouse which is our up and down value and this is where it may get a little bit confusing but the most y value actually controls the x rotation of the camera and the reason for that is when you move your mouse up and down you want to rotate your camera around from a central pivot point so moving up would rotate along the x-axis to face the camera down or up so let's start by doing that so we've already got a local reference to rotation x and what we're going to do we're going to minus equal import dot get axis and this time we're going to pass in mouse space y now remember capitalization on that you need a capital m a space and a capital y in most y and then from that we're going to multiply that by our lock speed y and again if you want to keep that lock speed as the same value ignore the y just call it lock speed and use the same variable for both the x and the y so now we have a rotation based on our mouse input we want to clamp that using our upper and lower lock limits so we can't actually spin the camera around 360 degrees so again we're going to set rotation x equal to math f dot clamp now here we need three parameters we need the value that we're trying to clamp rotation x we need the minimum value that we're allowing that value to be which is going to be minus upper lock limit and we need minus because obviously when you rotate out in that direction you're going to hit 360 and then it's going to go into a minus value so we're going to set it to minus whatever our value is so minus 80. i hope this is making sense and then for a third parameter we're just going to use a positive lower low limit so that should clamp our camera to looking at 80 degrees up and 80 degrees down next we'll apply that rotation to our player's camera so that's going to be player camera dot transform dot local rotation and we'll set that equal to a new euler so or euler i got shouted out for calling it euler in one of my other videos so quaternion dot euler passing our rotation x and then zeros on the y and the z so that takes care of our looking up and down now we want to be able to look left and right and for that we're not going to rotate the camera we're going to rotate the parent game object itself so this time we've got transform dot rotation multiplied equals and we're going to multiply it by quaternion dot euler 0 on the x we're going to use input dot get axis mouse x multiply that by look speed x and then zero on the z axis and now just like that we should be able to go into our game walk around and actually use our mouse to look and we can we have smooth movements on our mouse we can walk to the corners look around and you see if i just go to the edge here that's my lower lock limit so i can't look past that angle and then if we look up we can just see the corner of the sun right there i can't look up further than that simple now if we increase that look limit to 180 and 180 we should see i can go well beyond into a 180 degree angle which obviously is way too far but all depends on what kind of game you want and again we have the lock sensitivity in there the walk speed and the gravity multiplier so i think i'm going to leave it there for this episode we have a functioning first person controller at the moment but in the next video we're going to start with adding our sprinting functionality because what is a first person game if you can't sprint so i hope that's been useful for you make sure you tune in for the next episodes of this where we're gonna keep bolting extra functionality onto it that you can toggle on and off and just as a heads up this is going to be the last tutorial series that i'm going to be able to record in this area because we're moving house soon so i'm going to try and get all of this series done and recorded so there shouldn't be any gaps hopefully in tutorials while we're doing the move but the next time you actually see me from a new recording i should have a decent looking office for a change so keep an eye out for that all right then i'll see you again next week thanks for watching guys if you like the content remember to subscribe to the channel for weekly unity tutorials
Info
Channel: Comp-3 Interactive
Views: 6,992
Rating: undefined out of 5
Keywords: comp3, comp3interactive, unity, unity5, unity3d, unity2d, tutorial, game, development, introduction, code, coding, csharp, c sharp, c#, games, develop, 3d, 2d, programmer, easy, beginner, advanced, professional, multiplayer, how to make a game, how to make games, game development, how to use unity, brackeys, comp-3 interactive, unity 2d, unity 3d, how to make, how to do, game developer, game dev, make money with games, first person, first person shooter, fps, first person controller, fps controller
Id: 2FTDa14nryI
Channel Id: undefined
Length: 20min 1sec (1201 seconds)
Published: Fri Jun 04 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.