3RD PERSON CONTROLLER in Unity - Player Movement

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] all right guys welcome to episode 2 and today we're going to give our character the ability to move around but first i'm going to create a new folder and call it scripts let's just get some organization happening right here i'm going to drag my input manager into it but not my player control script because it's linked to this player controls asset so let's just leave that out here with that i'm also going to create another folder and i'm going to call this one art and i'm just going to drag in my model and my game object just want to keep the assets nice and neat and tidy try to stay on top that early in the game it's very important will make your life a lot better in the long run so i'm going to create a new script in the scripts folder and we call this player locomotion and we're going to handle uh the functionality to move our player on this script so let's open it up all right i'm going to get rid of the start and update method for now so we're gonna go through this nice and slow and the first thing i'm going to do on this player locomotion script is i'm going to create a public void handle movement and this will be the function that handles moving our player around so first we're going to need a couple of things right off of the get-go here i'm going to start by making a vector 3. we're going to call that move direction and this is going to dictate obviously the direction in which our player is going to move at any given time i'm going to say move direction is equal to camera i'm going to call this camera object we haven't made that yet don't worry we're going to the second dot forward so as you know in the last video we made our input manager and we gave our input manager the ability to give us a value based on what direction we're moving the joystick or if we're pressing the w a s or d keys and that value is separated into two separate values we have a movement on the x-axis and y-axis so depending on if you're moving the stick up down left or right or if you're moving stick if a combination of both ways like up into the right or hitting a w and d key at the same time it will give you some values and what we're going to want to do is we're going to want to use our move direction and we're going to utilize both of those values but first we're going to utilize the vertical value so we're going to want to say hey let's move our character in the direction the camera is facing multiplied by how much we're moving the joystick up or down let's say our movement input our direction will be the direction of the cameras facing forward and the amount will be our movement input so let's make a transform and call that camera object and basically this means our movement direction will be whatever way the camera is facing multiplied by how much movement we're pressing from the moving input so let's call our input manager up here and then let's make an awake method and let's say input manager equals get component input manager and the awake method all it does is it's called before start i like calling it to initialize variables and we're calling the input manager on get component because we're going to place it on the same game object as replacing the entire locomotion so over here in the input manager as you can see we have a movement input vector 2 which takes in the x and y variables of our movement well let's make a public float just for our vertical input and a public float for our horizontal input and what we can do is we can actually split this vector 2 into two separate independent variables which is what we're going to do which is why we wrote out here our vertical and our horizontal so this vector movement input just takes in our wasd keys or our joystick and gives us a value on the x and y axes based on what we're pressing or what direction we're pulling the joystick so let's make a private void handle movement input and say vertical input is equal to movement input dot y and what it's doing is it's taking the vertical input variable here and it's giving it the value of the y axis on the movement input so the stick direction going up and down and likewise we're going to say a horizontal input is equal to movement input.x so that will take the one value from the movement input variable uh the left and right and that will give it to this horizontal input so for example if it's left it'll be minus one and if it's right it'll be positive one and if it's nothing it will be zero the same thing we said for the vertical input except for up and down so back on the player look motion we can say is moving direction equals camera object.forward times input manager dot vertical input so we're going to move in the direction the camera is facing multiplied by our vertical input we can also say that move direction equals move direction plus our camera object dot right times input manager dot horizontal input and what that's going to do is allow us to move uh left and right as well based on our horizontal input and based on the camera's direction and then we're going to say move direction dot normalize and all normalize does is it takes a vector particularly in this instance our direction and it keeps its direction the same but it changes its length to one keeping it nice and consistent so next we want to say move direction dot y is equal to zero because we don't want our player to be walking straight up in the air uh and going straight up out into the sky that would be not a desirable effect now all we have to do is say vector 3 i'm going to say movement velocity is equal to movement direction now you don't need to declare this as a variable but i like doing it so it's nice and clear what we're saying here and then we're going to use something called a rigid body which is how we're going to move our character so we need to declare our rigid body first and we actually don't have one set up on our player but we're going to do that after we're finished with the scripts all we're going to say up here is rigidbody you can give it a name of any sort i am going to call mine player rigidbody nice and very descriptive and down here you can say player rigidbody dot velocity equals our human velocity so it's going to move our player based on the calculations we just made that's all it does very simple very straightforward very nice very clean and we're going to call that rigid body on awake right below our input manager but first let's make a public float movement speed because we can actually edit the speed at which we move based on this float i'm just going to initialize that 7 seven seems like a pretty fair speed for my past experimentation what we can do then is we can say move direction is equal to move direction times movement speed and make sure you're placing this right below the direction that y equals zero and right above vector3 movement velocity equals new direction and then what you will have is you're able to edit the speed on the fly uh from the script by editing the movement speed variable next down here under input manager i'm going to say player rigid body equals get component rigidbody because we are placing the rigidbody on the same game object as the player locomotion script and the input manager script okay cool let's save that now we have a way for our player to move but we also need to rotate because right now we can only move uh in directions but we also want our game object itself to rotate otherwise it will look a little silly so let's make a public void handle rotation now in a somewhat similar fashion we're going to say vector 3 target direction almost like our move direction we're going to say equals vector 3.0 so it's just that's just basically saying it's zero on all values starting out now this target direction is the direction we want our player to rotate when we move then we're going to say target direction is equal to camera object dot forward times input manager dot vertical input so it's just like above so far target direction equals target direction plus camera object.right times input manager dot horizontal input so so far it is the exact same thing as above and then we're going to again normalize it we're searching for the direction now that we want to rotate based on what buttons we're pushing so if we're moving forward we're going to rotate player forward moving backwards we want to turn the player around so you're always facing the direction which you're about to run so if i'm going to run to the left i'm going to turn first to the left and then run to the left if that makes a clear visual in your head so then we're going to say botunion this word differs targets rotation and quaternions are used to calculate rotations in unity so we're going to say target rotation is equal to quaternion dot look rotation so it means we're going to look towards our target direction so all this means put simply is that we're looking towards our target direction and where we're looking is where we want to rotate that's it then we're going to say quaternion player rotation equals quaternion dot slurp which is basically a rotation between point a and point b and we're going to use our current rotation so we can say transfer.rotation and our new rotation which we got from pressing the movement keys which is our target rotation okay and then we can do that by a speed so let's actually go up top and as we do with our movement speed let's make a public float rotation speed and i'm going to initialize mine at say 15 again through experimentation seems to be a pretty fair speed you can adjust yours however you like coming back down here again i'm going to say rotation speed i'm going to times that by a thing called time dot delta time and time dot delta time should be used when you want movement or any other constant variable change to the same speed no matter the frame rate because the frame rate will always differ if you want a further explanation on that i can make a video on that in the future don't worry too much about it right now now i'm going to say transform.rotation equals playerrotation and we are finished right there and now we have a function to move our player and to rotate our player so let's move swiftly along and hook all of this stuff up because we have the functions but we're not calling them anywhere or activating them so we need to do a couple more things first let's go over to our input manager and as you can see we have this handle movement input function but we're not calling anywhere it's not being used so let's right click here in the scripts and let's create a new script i'm going to call this one player manager and this is going to handle calling everything and running it think of this script as a script used to run all functionalities we create for our player so i'm going to make the awake method which again is called before start and is used to initialize things typically i'm going to declare a variable of type input manager so we're calling upon our input manager script i'm going to call it input manager and i'm going to declare a variable of type player locomotion so our player locomotion script and i'm going to call it player locomotion and i'm going to say in the awake method input manager equals getcomponent input manager because again when i do put the scripts on the player these will all be resting on the same game object and then i'm going to say player locomotion equals get component player locomotion because it's going to scan the game object that this script is on for a script of type player locomotion next i'm going to use an update method and a fixed update method and this method just runs every frame and then runs the logic inside and the fixed update is a bit different but works very similarly so back on the input manager script we have this handle movement input script but in the future we're gonna have things like handle jump input handle action input so let's make a public void public so we can call it another script and we're gonna say handle all inputs and then we're just going to put our handle movement input inside here like that and in the future i could say uh you know handle jump input and then i could say handle action input you can see how just encapsulating this code into one function makes it look a lot neater it will we have a lot more added to the script in the future so that's nice and since that is public and not private we're able to use it from the player manager so we can say input manager dot handle all inputs excellent now let's do something very similar to our player locomotion we have our handle rotation and handle movement but we need to call them somewhere and we're using fixed update for the reason i will discuss in just a second so first let's change this from public to private and public to private and then we can make a public function and we can call this function handle all movement and then inside this function we can say handle movement and handle rotation and then back over to our player manager we could just say player manager dot handle all movement in the fixed update and the reason we use fixed update is because when working with a rigid body it behaves much nicer and all things that handle moving it should be handled under fixed update this is a unity specific rule not quite sure why but just know that when moving a rigid body you should do it in fixed update so on our character here or low poly man for me i'm just going to go and add a component i'm going to add the player manager and then i'm going to add the player locomotion now over in our player locomotion we actually don't have our camera object declared anywhere so it doesn't know where to get it our transform so we're going to say camera object is equal to camera dot main and the transform of the camera so dot transform this will scan the scene for the thing that's tagged main camera and if we go back into the game you'll see defaultly in your scene there is a camera called main camera and the tag is main camera so we still need a couple more things we need a rigid body as discussed earlier and we're going to want to go to the constraints and since we're rotating it ourself we're going to freeze our rotation on x y and z because we want to handle the rotation manually through our scripts and then we're going to add a component a capsule collider and i'm going to go to my scene view here so i can get a better view of what this looks like and we want to make it roughly this size where our player does not need to be perfect i'll put mine up to say a height of one or y on the one sorry and a radius of 0.2 and a height of 2 and that looks uh we'll say 1.8 that looks pretty great all right now hit play as you can see we're actually able to move around but you'll notice our player snaps back to the four position when we stop and you'll see in the console here we're getting a message saying that our look rotation is actually zero and all we're going to do is go right below our track direction y equals zero and we're going to say if target direction is equal to vector 3.0 and we're going to say target direction is equal to transform dot forward so it is basically we'll keep a rotation at the position that we're looking when we stop moving and then we hit play again you will see that we can move around and look at that we will just face the way we're facing when we stop our movement and there we go we actually got our player to move around that's pretty cool now in the next episode we're gonna make it so he's animated while moving around so we'll actually have some walking or running animations depending on the speed we are moving at so if this video is interesting or helpful to you please drop a like below it does generally help my series get around if you're feeling super generous leave a comment to appease the youtube algorithm gods and if you're feeling like a total champion check out my patreon below i will see you guys in the next one where we will handle animating our player and we're going to explore some really cool themes based on our movement speeds and our inputs [Music] you
Info
Channel: Sebastian Graves
Views: 21,340
Rating: undefined out of 5
Keywords:
Id: suU4aBdBjKA
Channel Id: undefined
Length: 16min 52sec (1012 seconds)
Published: Sun Jan 10 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.