THIRD PERSON MOVEMENT in Unity

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] you can't get very far with most games without some player movement as in literally the player can move anywhere without it so in this video we'll create a solid third-person controller with both player and camera movement of course just like with first-person controllers how you want your movement is going to completely depend on your game this of course raises a lot of questions like do you want the rotation of your player to follow the camera do you want smooth or snappy movement should your player be able to strafe run jump and so on and depending on exactly what you need and how you answer these questions your controller is going to look very different so instead of doing something really complicated and specific to one thing let's create a really solid base that works and that you can easily add more on top of but first this video is sponsored by Jason Wyman Jason is an industry professional and he just created a new amazing course on game architecture the course teaches you exactly how to structure your game and code in a professional manner so you avoid all the headaches that you might run into when creating a bigger game you'll learn everything from writing testable code project structure management advanced state machine development and the list goes on the most awesome part is that you will learn everything through building your own unique RPG where you get to build quest systems learn about inventory management and interfaces and even branching dialogue systems new content is constantly being added and you get access to weekly live QA sessions with Jason himself alongside with email support and a discoid channel where you can chat with your fellow students so no more getting stuck on problems you just can solve so sign up using the link in the description and actually we're now launching our first ever hoodie on our merch toy line of code isn't it pretty and it's a brand new breakfast Duggar design created by our awesome editor Thea for the first week they'll be a 20% discount on the hoodie and for those of you who sign up for Jason's course you'll receive a free hoodie as well alright remember to stretch and warm up because we're gonna get moving so you can see him here in unity and I've gone ahead and set up this really quick example scene and of course the first thing that we want to add here is some kind player-character you can of course use your own model if you want but I'm just gonna use some similar shapes in unity for now so let's go to the Hokie here let's right-click go through the object and I'm gonna add in a cylinder I'm gonna hit reset on the transform here and place him somewhere in my level I'm also just going to scale him up a bit on the X and Z and let's make him a bit taller on the Y here so something like one point two by one point eight byte one point two and we're not gonna be needing the collider here since this is just going to act as our graphics so let's go ahead and right-click on a capsule Collider and hit remove component and this will definitely do for now Harvard this gives no indication of what direction our players currently facings let's go ahead and right click on this and create a quick cube object as well and it's just scaled this down to make it a bit smaller and place it somewhere in the front of our players that we can see that this is the forward direction of our player it's not pretty but it will do for now I'm also just going to add a quick read material remove the box Collider on our cube and that's it for our graphics now let's take these objects here and place them under a parent object for our players that's right-click let's create an empty object and it's first of all Center this on a cylinder so let's drag it under our cylinder reset the position here drag it out again and then drag our cylinder under our new game object we then rename our game object to something like third-person player there we go this way we've separated all of our logic which is going to sit on the player himself and all of our graphics which are currently under the cylinder and so we could also rename a cylinder here to graphics all right so we're now ready to create some movement and I think it's a good idea that we start with the camera for this will be using cinema sheen which is unity solution for creating interactive camera movement it makes it really easy to set a pretty advanced camera behavior without writing any code so let's go ahead and open up our window and let's go to the package manager here we need to select all packages and we can then search for cinema sheen simply click on it and go to the bottom to install and update and once this is in your project we can start using it to do this we go to the top on the right says cinema Sheen and here we can create all kinds of camera behavior what we're going to be making is called a freezer camera let's hit create Freder camera and as you can see this creates a new game object with a free look camera component as you can see this is a separate object from our main camera let's go ahead and rename this to third-person camera and what this essentially is going to do is tell our main camera what to do as you can see our main camera now has a brain component and it shows here that the current live camera is the new frida camera this means that it will currently follow the directions of our freezer camera so let's go ahead and set this up the first thing that we want to do is have it follow around our player so let's simply take our third person player and drag it into the follow start we also want to have it constantly look at our player let's take our player object and drag it into the lookup slot and right away if we switch to our game view we can see that our camera is currently looking directly at our player let's just go ahead and dark this to the side here so we can see what's happening now of course currently our camera is way too close to the player so to adjust this we need to go down under where it says orbits now we can think of the camera as having three orbits that we blend between in fact we can actually see these in the scene view there's one at the bottom one in the middle and one at the top and this allows us to adjust camera settings depending on the height of the camera for example it normally works well to have the camera move closer to the player when it gets near the ground so to do this we can position the bottom break here near the ground and give it a pretty small radius so let's start with that let's go over here under our bottom rig let's decrease the height here so that we get close to the ground and let's set the radius to something like 12 there we go let's then set the height of the middle rig to something like maybe 5 and the radius to something like 17 and we can see immediately that because our camera is currently on the middle orbit it gets farther away from the player because we increased the radius and for the height here let's set that to something like 14 and give it a radius of 12 we also want to set the binding mode here from symbol follow to world space this is because that for this controller we don't want the camera to be influenced by the rotation of the character instead we want the character to move in the direction of the camera and so a camera shouldn't be bound to a player but to the world space instead now we can go ahead and preview our camera settings by simply going up on the access control and changing the y-axis value as you can see this moves our camera up and down and it seems that there is an obstacle in the way here so let's just take our third person player and move it over so that we can see what's happening or we can just hit play and try it out using our mouse and as you can see our camera is moving in and out of the three different orbits awesome now by default this is going to be using this standard Mouse input you can change the input axis names here in case you want to use something else to control the camera or even change the Y and X values manually through script by leaving the input names blank but in my case I just want to use the mouse you can also change the sensitivity here by adjusting the speed I'm gonna set this to four and decrease the acceleration time and I'm also going to increase the speed on the X I'm gonna set this to 450 finally you can actually compose your shot for each orbit by going into the corresponding rig in the inspector as you can see we can adjust all these properties here for top middle and bottom rig or even simpler if you turn on game window guides at the top you control this using the guides in the game view so right now I'm on the mill rig and I can go ahead and adjust that sounds and position the player wherever I want really cool but for now I just want this to stay in the center alright so now that our camera is working it's time to get the player moving to do this that select our player that's it a component and we're going to add in a character controller I'm going to set the radius of this to something like point six the height to three point six and that should give us a nice character controller Collider I'm also going to add another component here and that is of course a third person and movement script this is a custom script that we are going to be writing let's double click it to open it up in visual studio now I'm going to go ahead and remove the start function here and the first thing that we need is a reference to our character controller because we're going to be using that to move so let's create a public character controller and let's call it controller you can think of the character controller as the motor that drives our player what we need to do is tell the motor where to go let's also create some kind of speed variable let's create a public float speed let's default it to something like six now with this we can go into our update method and the first thing that we want to do is gather some input now unity is currently working on a new input system the old one is still going to be working for quite a while and that's what we're gonna be using in this video but if you're having issues definitely check out our video on the new input system in case the old one is deprecated now I want to be able to move around using vast and arrow keys so let's go ahead and get some horizontal and vertical import all right float horizontal and let's set this equal to input that get access wrong we're going to be using the raw axis because we don't want any kind of input smoothing we just want the raw input and let's input in here the horizontal axis let's do the same thing with vertical again input get access raw and we'll input vertical so as always the horizontal axis is going to go between negative one and one it's going to be a negative one if you press the a key or the left arrow key and plus one if you press D or right arrow and the same thing for vertical but with the W and s keys or up and down arrows instead we then go ahead and create a vector three here which is going to store our direction and we can set this equal to a new vector3 where we first input our horizontal we then input 0 for the y axis and then the vertical and of course the reason why we are doing it this way is that if we go into unity we want to move our character if we have a look from the top here on the X and z axis but we don't want to move it on the Y and just to make sure that if we hold down two keys and go diagonally that we don't move faster let's go ahead and normalize this then we can check if we are moving in any direction by going direction dot magnitude so the length of our direction vector is greater than or equal to zero point one well in that case we're getting some input to move and we can actually go ahead and move our character by simply going controller dot move here will input the direction will multiply with speed and multiply with time that that's a time in order to make it frame rate independent and if we save this and go into unity we now need to drag in our character controller here we and set the speed to whatever we want and if we play we can start moving in the different directions awesome as you can see this is currently completely independent of where our cameras pointings that we definitely need to have it orient towards the camera and also our player currently isn't rotating to face the direction that he's currently traveling to fix this that's first point or player towards the direction that he is moving to do this we need to take the direction and figure out how much we should rotate our player on the y axis to point in that direction and we do that using the 810 two function so we'll create a float called target angle and set it equal to math f8 onto and here will input Direction X come at Direction dot Y now what is a 10/2 well I tend to is a mathematical function that returns the angle between the x-axis and a vector starting at zero and terminating at X comma Y now that might sound pretty complicated but in reality it's actually fairly simple if we just visualize it so here's a coordinate system this represents our scene seen from above with a player in the center the dotted line here is the direction we would like our player to point in we can use the 18:2 function giving it first dy and then the x-coordinate of our direction to get the angle from the x axis to the vector however this assumes that our rotation starts at zero at the x axis and then increases counterclockwise but in unity our character will have a rotation of zero when he is pointing forward and from here it increases clockwise to take this into account we simply pass in the X first then the y coordinate and voila we have the we need and if we have a look at our script that's exactly what we've done we take the attempt to function passing the x coordinate and then the y coordinate of our direction now this will give us an angle in radians so let's just go ahead and multiply this with massive dot radians2degrees in order to convert it into degrees and now we have the angle that we want to use so let's set our rotation by going transform rotation equal to quaternion Euler and this allows us to input three numbers the rotation around our x y&z Stetz put 0 around our Y we'll use the target angle and on the Z we'll use 0 as well and whoops it looks like we use the direction that Y here that's of course 0 instead we need to use the z axis here there we go and right away we see that our player rotates to face the direction that he's traveling of course he currently snaps really quickly into place let's just go ahead and add some smoothing to this let's create a variable to adjust this called public float turn smooth time instead of the equal to something like 0.1 by default and for this we can use a function called the smooth damp angle so down here before we apply our rotation let's create another float called angle and so it's equal to math if that smooth damp angle here we input our current angle which is transformed that Euler angles dot Y our target angle which is our target angle variable and then we need to give it a reference to a variable that can hold our current smooth velocity and this is just a private variable that we create at the top here so that's create a float turn smooth velocity and all we type down here is Rev turn smooth velocity and the function will handle the rest and finally we input our smooth time so let's put in our turn smooth time now this function is definitely a bit weird but it's a great way of smoothing numbers and particularly angles inside of unity so if we now put in our angle here instead of our target angle we should see that when we move around a player will smoothly turn from one side to the other awesome of course we still need to have the player travel in the direction that our camera is facing so to do that we go into a script and the top here we need a reference to our camera let's create a public transform let's call it cab and now when we go ahead and calculate our target angle here let's simply add on to that cab that Euler angles dot Y and that's really all we need to do we simply need to add the rotation of the camera on the y axis on top of our angle and this is of course going to have our player point the right way but we still need to make sure that we also move in that direction so instead of just using the direction here let's go ahead and create a new vector3 called move direction and that set that equal to quaternion dot Euler zero target angle come as zero and this is why a bit of knowledge of quaternions and vector math come in handy because in order to turn this from a rotation into a direction we will simply multiply our quaternion with vector three dot forward and that should give us the direction we want to move in taking into account the rotation of our camera and so let's just change direction here to move direction and as always when moving we want to normalize this and that is actually it for our script if we save this now and go into unity when they need to reference our camera and for this use the main camera and not a third-person camera use the original main camera here and with that if we play and look in a certain direction and press forward we can see that immediately our player starts to go in that direction we of course also go sideways relative to where we are facing and backwards and so we have a nicely functioning third person character with camera awesome now finally one of the problems that you might see with the current way that we've setup the camera is that because we're able to kind of look everywhere sometimes we might be able to look a place where we can't even see our player anymore and if it gets really bad we can maybe even move a camera inside of obstacles or parts of the level luckily cinema Sheen actually has a feature built in to help avoid this so if we select our third-person camera and go to the bottom here you can see that we can add extensions to our camera and one of these extensions is these machine collider what this basically does is have a camera check if it's currently colliding with something or if there's something in the way of our cameras seeing our character and then moves our camera out of the way it's really easy for us to set up we just want to select what layers we should check whether or not we are colliding with so right here you can see it's currently set to default well I've gone ahead and set all of my environment objects to use the ground layer so in my case I'm just gonna go ahead and change that to ground and then remove the fault here we're also going to have it ignore all players under ignore attack here in select player and then simply give our player that player tag and finally we want to set the strategy from perserve camera height to pull a camera forward this is the behavior that you want in most games and what this basically does if I go ahead and demonstrate here is that if something gets in the way of us seeing our character our camera simply moves forward and if we collide with the obstacle itself the same thing happens you can see we cannot move our camera into this obstacle really really cool I find another thing that really works with this if we go to the top we can change the lens a new clip plane to something smaller which will just help increase our margin of error because we can get closer to an obstacle without our camera clipping through it and finally if you want and this is completely up to you you can add some damping to this so we could go ahead and set this to something like point six and point six and what this will do is just smooth out the movement of our camera this will mean that it sometimes travels through the object but I find that this is actually an okay trade-off in most cases that's completely up to you if you want to add that but I find either way this is just a great way for your camera not to constantly get stuck inside of other objects awesome that's pretty much it for this video if you enjoyed it make sure to subscribe and ring that notification bell so you don't miss the next one now from here it's up to you to start adding more features to your controller as you need them if for example you want to add gravity and jumping to your controller will of course have a link to our first-person controller tutorial where we show exactly how to do that it should be pretty easy to just add to the script of course creating a really extensive controller can take a lot of time and quickly becomes pretty complicated so if you want you can always pick up a third person character controller on the unity asset store I personally recommend this one since it's fairly easy to get started with and it has so many features like driving sliding ragdolls a moving platform system inverse kinematics and the list goes on well of course have a link to that in the description also don't forget to check out jason's course on game architecture learn how to properly make big games by simply clicking the link in the description and get a free hoodie included on that thanks for watching and I'll see you in the next video thanks to the awesome patreon supporters who donated in April and a special thanks to - blank lost to violent stuff forever free Samara Phi John replica studios SRT Mike Luna set Jason you Tesco Navi ninja will avert an endangered - sonic Dona team Gascoigne tanta Sam Jacob Sanford Noah Kiowa sake mark and twenty art Gregory Pierce Michael Cobb of the mighty Zeus oven Cooper else in the fierce Erasmus and serious you guys Rock
Info
Channel: Brackeys
Views: 799,398
Rating: undefined out of 5
Keywords: brackeys, unity, unity3d, beginner, easy, how, to, howto, learn, tutorial, tutorials, game, development, develop, games, programming, coding, basic, basics, C#, third, person, controller, movement, player, camera, moving, 3d, over, shoulder, cinemachine, character
Id: 4HpC--2iowE
Channel Id: undefined
Length: 21min 5sec (1265 seconds)
Published: Sun May 24 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.