How to Create Player Movement in UNITY (Rigidbody & Character Controller)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in unity there are two main methods of making player movement the first method is using the character controller component and the second method is using the rigid body component now why would people use one over the other it is because the rigid body is physics based and the character controller isn't if your game requires for the player to interact a lot with the environment then a rigid body controller would be appropriate since rigid bodies are within unity's physics loop and can properly interact with other physics-based objects if your game doesn't require for the player to interact with the environment as much for example an fps game then a character controller would be suitable as it is more simple and optimized for a static environment now that we have a basic understanding between the two we can now get into coding them so let's do it okay so here i have a unity scene with a rigidbody player object it contains a rigidbody that has all the rotation axes frozen so what that means is the rigid body will not influence the rotation of our game object and there's also a script attached to the the player object which is the rigid body movement script which is the main premise of the section and there is also a bunch of these cubes this is mainly for fun because cube they also have a rigid body so we can watch physics happen and show how these two elements or all these elements interact with each other especially the rigidbody player object so now we can go into our rigidbody movement script and we can start working on this so the first thing we want to have done is a reference to our player's rigid body this is so we can you know influence the velocity and all that make it actually move and now we're going to have a reference to our player camera transform this is so we can rotate the player camera with our mouse now beneath we're going to put a space to make it look clean and we're going to create some more properties for general speeds like um the the movement speed the the player sensitivity and the player's jump force sure so we're going to create a float for the speed for the sensitivity the sensitivity is for the mouse like our when we move our camera around and a private float for the jump force there now at the very top we're going to create two private variables the first private variable is going to be a vector3 and it is going to be a movement player movement input and we're also going to have a vector 2 and this is going to be our player mouse input so we don't need the start function before we do anything in our update function we're going to create two separate functions the first one is going to be for moving the player and the second function is going to be for moving the player camera so in our update method we're going to set up our movement input so player movement input equals new vector three and here we're going to do input dot get axis horizontal zero because we're not going to influence the y but we will influence the z axis get axis vertical so what this line of code does is that it records the input for the main movement keys so like um if we're tapping into the id the string id of horizontal we are looking for the a and d keys so basically left and right if we press a it will return as negative one and if we press d it's going to return is positive one and for the vertical it's going to be looking for w and s if we press w it's positive one if we press s it is negative one and there is another function alias of this it's like get axis raw so the main difference between get access and get access raw is that get axis has smoothing on it you can see it on the little tooltip here and well you can't really never mind but when you type in get access raw you're going to see no smoothing filter applied and that is the difference between get access and get access raw so now we're gonna set up the player's mouse input player mouse input equals a new vector two and we're gonna use the same function input nugget axis but this time it is going to be mouse x so that would be moving our mouse left to right and input dot get axis and then mouse y is forgetting make sure it's proper a mouse up and down left and right all that now we're going to call our move player function and our move player camera function so now we generally have everything set up for all the movement so we're going to first work with moving the player itself so what we're going to do is we're going to create another vector3 and we're going to call this our move vector and we're going to be in transform that transform direction and our direction is going to be our player movement input and we're going to multiply this by the speed and once we have our move vector we're going to do player body dot velocity equals a new vector 3 of the move vector x player yeah playerbody.velocity.y and movevector.z so the reason why we are not multiplying this by the speed is because of the player body velocity y the reason uh why we're not doing it because of this is because um when the player is falling down right the the y velocity of the rigid body is going to be different and if we multiply that by your speed we're going to be going down fast like we're going to teleport to the floor and we don't want that to happen so we're going to multiply our player input by the speed since we only want to have the speed influence our movement forward back left and right not up and down because that will cause a lot of problems down the line so this line is generally done but we're also going to implement jumping so what we're going to do is we're going to create an if block and it's going to be if input get key down key down and it's going to be key code dot space the regular jumping key we're going to do player body dot add force and the force is just going to be vector 3 dot up multiply that by your jump force and it's going to be in force mode dot impulse there so this is just for jumping impulse is like an explosion right it's it's like instantaneous force that's why we're using impulse so now if we go back into unity and we assign all of our variables we're not we uh the camp player camera isn't being used yet because we haven't really done the the player rotate thing yet so in the speed we're gonna have five uh sensitivity it could be three if that's for later and the jump force will be 10. there so now if we hit play if we press w a s and d or the arrow keys the player will now be moving and you can see how the rigid body controller easily interacts with all the cubes around it but the last thing that we need to put on before this is a complete character or player controller is the player camera movement so what we're going to do now is at the top here we're going to create a private float and call this x raw or which is like just x rotation and in our move camera move player camera function we're going to subtract that by our player mouse input dot y multiply that by the sensitivity the reason why oh got to fix that okay so the reason why we're saying x rotation even though we're referencing to the y axis is because when we go back into the inspector or not the inspector but uh the scene right when we rotate the x-axis is what makes things look up and down so that is why we are saying x-ray and not y-rotation the y-rotation is what makes the player turn left and right so now that we have this set up we're gonna do player camera dot transform dot local rotation equals quaternion euler and in here we're gonna do the x rotation 0 0 [Applause] and now before that we're going to do transform.rotate and we're going to do 0 and then the player mouse input dot y or dot x sorry multiply that by the sensitivity there and that is the rigid body movement so now if we go back in let me set the sensitivity to three see that we can look around freely and we can interact with all the cubes and if we press space our jump function works and you can also jump in the air and if you don't want this behavior i'll go over that real quick but this is what the rigid body controller can do we can run around we can mess around with all these cubes these physics based objects and yeah to prevent jumping in midair what we can do is have a volume check every time we want to press space right so put it at the feet right and then call this game object defeat and we're going to create a separate layer for the floor and we're going to call this the floor layer we're going to mark this as the foil for the floor layer sorry so like any floors or any like any platform you want the player to be able to jump off mark it with the floor layer and in our script uh when we press space we're gonna do if physics dot check sphere and the position we're going to create a reference to that serialize field private transform feet the feet transform dot position and the radius can be whatever you want um i'm going to set it to 0.1 and the layer mask we're going to create a reference to that as well private layer mask the floor mask and we're going to set that to the floor mask and we're going to put our jumping line within this if statement and now if we go back into unity and assign the feet to here and put the floor layer in the mask and we hit play you can no longer jump in mid-air oh let me change the sensitivity back there i'm i am spamming my space bar and i'm not jumping through there and this is the rigid body movement now we can get on to the character controller movement all right so in the scene right now i have the physics cubes and the player object and behind me is a staircase to show one feature of the character controller that is fairly useful so now let's go into our player object uh should have a character controller component and a character controller script or the script that you're going to use so now if we edit this we can get to work on it so first we are going to need a reference to our character controller character controller and call this the controller and we'll also need reference to our player camera and the usual down here [Applause] so you know the speed variable uh it's jump force sensitivity and we're going to have an extra parameter and it's going to be our gravity because the character controller is not physics based we're going to have to program our own physics into like our character controller but it's going to be very basic we're not going to get too in-depth when it comes to um you know like we're not gonna worry about adding forces and doing all that fancy physics stuff we're just gonna stick with regular gravity just push us down so set the default for this normally gravity is negative 9.81 meters per second squared that's the force and now we're going to create two private voids player and [Applause] so the move camera function it is exactly the same as the rigid body movement um like camera movement so we can just copy this over we can we can really just do that copy that over uh you know the player mouse input all right so the player camera movement is set up because this is fairly universal amongst uh moving uh the player camera like rotating the camera based on the mouse it is the same all the way throughout unless you want to have something fancy like smooth camera rotation like you would want to lurp it or something like that but we're not gonna really mess around with that in this video this is just to show you the basics and give you an understanding of how to program uh character controller movement and rigid body movement so continuing on all right now i'm going to create our player movement variable player movement input and we're going to update this input.getaxis zero actually no not not zero actually yes input dot get access not raw vertical and now that all of our vectors are set up we can call our move functions building beneath the camera now this is where the main difference is this is where it gets different right in this move player function so we're going to need another variable and that will be our velocity so private vector 3 of velocity and in our move player function we are going to take our controller and we're going to do dot move and we're going to put our velocity in here and above it we're going to controller.move and then in here what we're going to put is our move vector so to get our move vector once again you just do vector 3 and your variable name and it is transform.transform direction and our direction will be our player movement input and we can put this in here and this time we can multiply the our speed inside of our move function so move vector multiply that by our speed and multiply it by time to delta time so you don't go at the speed of sound or the speed of light actually so that is all set up now for our velocity how do we set up our velocity so we want to apply gravity right and there's an equation for that which is basically just y equals gravity or minus equals gravity multiplied by negative 2 multiplied by time by delta time and then we multiply it by delta time in here again because it's time that delta time it's just time squared so to apply this we can simply subtract the y on our velocity with this equation but we do not want to subtract it every time we move all right that's not good we can't do that because we like you don't build up velocity for falling down when you're standing in place on the ground like it doesn't work that way so we're gonna have to check if the player is on the ground and this time instead of using a separate object and doing a physics.checksphere function the player the character controller has a built-in boolean for that and it's called is grounded so if we do if controller dot is grounded right there then we're gonna set our velocity to zero like our velocity dot y to negative one not zero sorry so velocity dot y equals negative one there and if we are not grounded then we're going to apply this equation velocity dot y minus equals our gravity multiply that by negative 2 multiplied by time dot delta time if we're going to multiply by negative 2 is just get rid of that that sign there the negative sign there and we move with our velocity multiply the time of delta time here because it is time squared so now this is mainly what we have to do for our movement so if we go back into unity and we assign all of our variables set our speed to 4 uh three and you know nine point eight one oh yeah jump four sorry i forgot to implement jumping so for jumping it's also very very simple all we have to do before or actually after no yeah before it's before before we move like before we apply all of our velocity and stuff we do if input get key down key code dot space and we want to be grounded so we're going to put this in our nested if if statement and we're going to set our velocity dot y to our jump force there now it is completely set up all right set our jump force to 10 now if we play okay i think it is supposed to be negative 9.81 and i forgot i'm subtracting my bad okay now you notice that we can jump around and these cubes here they have rigid bodies and look what happens if i run into them with the character controller nothing happens that is because the character controller is not within the physics loop and it cannot interact with physics-based objects but there's one thing that i like about the character controller there's automatic stepping it automatically moves the player up and if i come back down and i scale this up a bit if i just hang on if i just move this up and you can see that it snaps hang on there's a parameter on the character controller that you can increase the step offset if i just increase that so i can actually get up here there it snaps me going up the stairs and this is very useful and that is it that is the character movement for rigid bodies and character controllers if you learned something like subscribe you know the usual and i'll see you in the next video or tutorial [Music] you
Info
Channel: Rytech
Views: 18,829
Rating: undefined out of 5
Keywords: Unity Player Movement, Unity Movement, Unity Tutorial, Unity Rigidbody, Rigidbody, Character Controller Unity, Tutorial, How To Unity, unity tutorial, unity fps, game development, Unity Character Controller, Unity FPS Tutorial, unity player movement 3d, unity character movement, karlson movement tutorial, how to make a game, how to, unity fps movement, character movement in unity3d, how to move characters in unity3d
Id: b1uoLBp2I1w
Channel Id: undefined
Length: 22min 2sec (1322 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.