Move in Unity3D - Ultimate Unity Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey what's up I'm Jason and I've been developing games for about 20 years but when I started with unity I still got confused about how to move things there were so many different ways that I could move objects around and different systems that I could use I really didn't know which one was right for me and which one fit my scenario so in this video I'm gonna teach you everything that you need to know about moving objects in unity we'll talk about things like basic movement how to get around the scene view scene editor and move objects in there we'll talk about moving them through their transform position I'll teach you how to use rigid bodies to bounce things around and character controllers to move characters will go into navigation and animation and a whole lot more if you're new to unity this video should be perfect for you we're gonna start off assuming that you really don't know a whole lot about it and kind of at the beginning but if you're a more advanced user hold on for just a moment because there's a really cool trick that I just learned a couple weeks ago that most experts don't know about and I think it might save you a lot of time and just be cool to learn in probably a lot of fun before we start though if you have any questions about this video or just about movement and unity in general drop them down below and I'll get back to you as quick as I can and try to help you along the way also if you think of anything that I missed some movement system or movement thing that I probably should have noted or that you're just kind of more curious about drop a comment - it's helpful it kind of gets the discussion going and YouTube loves it as well and speaking of that please don't forget to hit that like button for me in YouTube it makes us both happy before we talk about how to control an object's movement with code let's talk about how to control it in the editor and in the scene view we're gonna create a new cube here so I'll go to game object 3d objects and create a cube and notice that when I created it it's gonna pop right up on top of me the first thing then the thing that I use most often and unity for setting positions is right-clicking on the transform and hitting reset that sets all of the position rotation and scale scale values back down to their default so 0 0 and ones of course that's down below my ground cube that I've already pre placed here so I can use it with the move tool the move tool lets me move up down forward and back and I can switch to the move tool right here this is the rotate tool and I click over here to the move tool so if you're seeing a different tool different little indicators here you can go back to the move tool like that or hit W W is the shortcut for switching to that move tool so once I'm in the move tool I can again move it around I can grab one of these arrows here or these side pieces and just drag it and put it into position I can also move this with the transform component here so I can take this x value and just type in a different value it's at 1.9 I could set this to 5 and move it to the 5 position on X in my world I can also grab this x value and just slide it back and forth to move it I can do the same with Y which is gonna move me up and down in Z which will move me backwards and forwards now another really useful feature for moving things is the grid snapping if I hold down the ctrl key or command and drag notice that the value here is going by 0.25 so it gives me nice easy snapping if I want to snap objects together this can be useful if I say have 2 cubes so if i duplicate this with ctrl d and drag it out here and I want them to line up perfectly just help control the whole time and it got them lined up perfectly once at five point five ones at six point five and they're one meter wide so they line up great the last thing and this is the really cool trick that I wanted to share that I just learned about myself is this neat little control shift option so if I hold down control shift I can grab this box and it'll actually snap to the side of the objects that I'm touching so it'll snap the colliders together now moving objects in the scene is great and all but really we want to move our objects in code we want a player to be able to control things and the easiest way to do that is to control this transforms position from our code and that's what we'll start off with so here we've opened up a scene called box moving with input and I have a moving box and it has quite a few components on it so let's take a quick peek at it it's got a cube mesh and a mesh renderer that's just the default from the cube along with the Box Collider if we make a 3d object that's cube we get those but then it has these four scripts on it only one of these scripts is enabled though with this little checkbox all of the rest of them are off and we're gonna cycle through these in different parts as we talk about different types of transform based movement for our cube the first one is called move transform position with input let's open up the script and see what that looks like so here we have our first class our move transform position with input class that has the single update method that gets called every frame by unity automatically now here we're doing a couple of different things the first thing I want to look at though is actually not up at the beginning it's right here on line 19 and I have a check that says if input get key down for the key space so if I've pressed space this frame then we sit in the transform position to a new vector3 we set it to 0 on the X 5 on the wire 0.5 on the Y and 0 on the Z and if you're not sure what those are if I hit the backspace and just hit the comma again and see the little pop-up up here so that's the value that I'm going to set this to if I hit space I'm gonna just save this real quick with ctrl s go back into unity and I'm gonna move this cube before I hit play just get it off into some random position in fact I'm gonna hold the ctrl shift and do that little trick and make it maybe snap to the side here let's make a snap on top then I'll hit play and then I'm gonna hit a space and watch what happens it resets back to the default position that 0.5 and 0 or not really the default but the position that I have hard-coded let's try moving around with those arrows I'm sure you saw the arrow code so I hit up arrow and it goes forward down arrow and it goes back left and it goes left and right and it goes right I'm gonna stop playing and open up that code one more time and take a look at how that's accomplished so on line seven we check to see if the right arrow was pushed down or pressed and if it was we just add vector three dot right to the position and if I look here I wish I could just point right at my screen but if you see the vector three or the shorthand for righting vector three one comma zero comma zero vector three dot right it just means a one for the X and zero for the other so we're adding one to the x value here it's a negative one for the x value a 1 for the Z value and a negative 1 for the Z value that's all we need to do to move very basic snap based movement now this is something that we would use in more of a grid-based or tile-based game or something something of that nature where objects are moving along into these set slots or set positions that's what you would use this for but you might want to limit or lock that movement too so let's take a look at an example of how we can do that but maybe constrain this movement just a little bit now we're back in the scene I'm gonna expand out my inspector a little bit and then we're gonna uncheck the move transform position with input and I'm gonna check the move transform position with input limit movement this is the same script but with some movement limitation set up so now I'll hit space I'm gonna hit play and then hit space get reset and then I'm gonna try moving around so I'll go forward and when I get to this point I keep hitting forward nothing happens I go left and it stops me there go right and it stops me here and I come back and it stops me so I'm limited in my movement but you may have also noticed that we're going through these boxes and when we get to rigid body physics and rigid body movement you'll see how we'll stop that but for now let's look at how we've limited the movement based on its actual position and if you look at the position you might notice that it's going to negative 5 here 5 and all the way over to positive 5 let's expand out the script and open it up here our move transform position with input limit movement script very long name does a lot of the same things that our previous move transform position with input script did except that we have this extra check in fact it's the exact same code but we have these double ampersands and this transform position X less than 5 so what's going on here really we're checking to see if they've press the right arrow and that the position is less than a positive five so that we can't move right if our position has reached five if we get to five or greater there's no more moving right we're stuck we can't do that we'll have to move left first and if we look at our left check we check to see that the position X is greater than negative five because we can't go left any more which is going negative or beyond zero the next two parts are kind of the same we just checked the Z position four up arrow and the Z position four down arrow again with a five and here I went with a negative one so we couldn't pull the box in too close to the camera nothing too exciting but I wanted to show what this looks like and how we add in some extra very simple limiting we don't have to necessarily have box colliders or things preventing the movement we can just write it in some simple code if this supports our game and this is what our systems really need now before I jump ahead I want to really quickly show how I would rewrite this in a slightly easier to read version and that is in our movement cleaned script so here you'll see that all I've done is taken these if statements and converted them into properties that are named or what they do so or what they're checking so here I say if we should move right then add the right movement if we should move left add the left movement if we should move forward and should move back it's just again to make it a little bit easier to read and easier to understand as we're going through you can imagine that as these conditions get more and more complex it can get a little bit confusing to understand what it was and what it was that you're checking and if that check is the correct check for the movement and having to mentally map it so I find that doing something like this creating these little private properties using the expression body property method the next movement type we'll talk about is one that I think most people really care about the one that we are probably trying to use in your game and that's moving around an object smoothly or like this where we have a cube that we can control and just have it move smoothly along and maybe control the speed crank up that speed and watch it fly along fast or slow it way down and make crawl let's try that Oh point five and you can see it slowly moves along so how is this accomplished how do we do this let's take a look at the code here's our move transform position with input smoothly script and you'll see that we have a serialized field that's a private floating-point value called move speed and it's defaulted to two that's controlling our speed and you'll see how we use that on line 11 in just a moment next I want to look at line 10 where we call input dot get key here I want to note that get key is very different from get key down get key down fires once when we press the button and we use that for things that we want to bounce around or have steps based movement or single time input reads get key returns true every frame that we hold this arrow down that's how we're getting the smooth movement so every frame this is gonna be true as long as we're holding it and we'll add the position well with this vector 3 dot right but we don't just add vector 3 dot right we multiply it by our move speed and by this magical time dot Delta time and this is really kind of where all the magic comes from time to a delta time is the amount of time in seconds since the last update was called or since the last frame was called so what this is gonna do is smooth out this movement so that we move to meters once per second this update will be called so many times per second maybe at 60 times a second whatever that number of times the second is over the period of one second the delta time will have added up to one so imagine it was just four frames say we ran at four frames a second and each frame was at point two five we would do a quarter of it the first frame a quarter of the movement in the second frame a quarter of a third and a quarter of it the fourth frame eventually we would make it the full two meters so this is actually our meters per second in movement speed so all we do is multiply those and we get the value and it moves nice and smoothly now let's talk about rotating because our current setup really only allows us to move an object in this one world position and if our camera adjusts well we don't have lined up movement anymore so how do we do that well let's disable this script and I'm going to enable my move transform position with input smoothly rotation we'll start moving around and here I'm using WASD to move and Q allows me to rotate along with E so I'll hit Q and then hit W and check it out now I'm able to rotate and turn now let's see what it looks like on the previous one if I go check this other box and I've already rotated and I hit my arrow keys as I said I still move in world space in fact I can actually enable both of these because I've bound them to different keys I can use my arrows to move in world space and my WASD to move in local space and rotate and control that very interesting movement so let's check out how this script works and how we're able to do the rotation based movement because that's usually what we want we usually don't want to just move in world space unless we have a fixed camera that's never changing alright let's go open this up and our move transform position with input smoothly and rotation script has two serialized fields we have a move speed for our movement in meters per second and a turn speed for just how fast we're going to rotate here you'll see that we have a lot of the same code that we had in the previous section for our movement but I've bound them to D a w and s or WASD and our D key is going to move to the right so we just take Delta time times move speed and here we don't use vector three dot right this is the big difference if we want to move local to our object or our transform and not world space we can just use transform dot right to do that instead of vector three dot right which will give us the world space movement the other thing that we need to note is there isn't today transform dot left but- transform dot right is the same as left and then we get the same performed up forward and negative transform dot forward to go backwards the other part that's new here is the rotation so Q is set to turn me to the left so I call transform rotate and we pass in time Delta time and heart turn speed or we multiply this value times that and then we use vector three dot down because I want to rotate in the negative direction on the y axis and I go into unity and I just look at this but just drag this down I can see the value going down means left and value going up means right good way to test if you're just kind of getting mixed up and not sure you know just go in and drag these values around and see what happens now we'll talk about rigid body movement this is a very common type of movement used to control objects that need to respond to physics here I've set up a cube that we can move around using its rigidbody component I've added a rigidbody to it have not changed any of the default settings and we've added this move with force script it has a movement force value of 10 and you can kind of see how the object bounces around now let's look at our move with force class it has a rigidbody component that we are cashing in the awake method in our awake it's just an expression body method because there's only one line so it's only doing one thing we say rigid body equals getcomponent rigid body so that's just going to get a reference to this rigid body component so that we can tell it to add force later so in our awake we do that we also have this serialized field for our movement force which is going to control how much force we add to the object as we hold the key down now this will need to be relative to the mass value set on the rigid body as the mass goes up we need more force to move the object around the next thing we have is a fixed update method and this is different from our update method generally when we add force to a rigid body or a lot of physics objects or doing a lot of physics related things in general we want to do them in the fixed update definitely adding force to a rigid body though we want to use in our fixed update step the fixed update is very much like the update with a tiny little bit of difference it's where all the physics work is actually done and it has a special priority so that it can run at its own special frame rate and that we can have our physics be a lot more reliable and not fall apart when our frame rate falls apart so we need to do this in fixed update if we do it from update it will give us some issues and you'll notice them where we get to force calls in one frame and suddenly one frame we get a little boost or a little extra stutter in fact if you change this code and you go from fixed update to update you should be able to replicate and see that negative behavior of that problem where it starts bouncing around a little bit and it isn't as smooth now let's take a look at how it actually works on line 13 we just check to see if the W key is being held down with input get so if we're holding W it's true and then if we are we add force to the rigidbody giving it our movement force multiplier and vector 304 so this is going to move along that z axis it's gonna add to the z axis and it's just gonna make our objects kind of push in that direction which our R cube is kind of making that weird rolling action which i think is kind of cool now let's look at how we can make a cube and move around in a more natural fashion more like a character having maybe slide and bounce into walls and stop so to do that we have a move with force to use transform rotation script I'm gonna hit play and show you what that does if we only turn this script on so I hit play and I roll forward it's not doing what I wanted and I can roll to the left can't go left anymore now if I hit forward I go a different direction this is all because the forward direction of my object is getting out of whack so let's check take a look at that if I look at my blue value right there forward is actually pushing down and I believe right is gonna push that way so I hold right and I go that way now forward is pushing left so if I hold forward it's actually going to roll to my left not the behavior that our player would want really really confusing so how do we fix this well if I reset this transform position and I check the freeze rotation boxes here suddenly I can move a little bit if I hold the two together I can move but I'm no longer getting that rolling behavior so why can't I move anymore it's because there's not enough force to actually give me a slide so I'm gonna crank this force up to 20 and now I can kind of slide around so you can see I'm able to slide I still can't quite turn so how do I add in some turning I'm gonna add in another script and then we're gonna go take a look at both of these scripts together so we're gonna add in this rotate with Mouse script now if I put my mouse over the game view I can turn it and I can hit W and like it moves in the forward direction I just hit a there to go sideways D to go left go back with s and W and I can just kind of rotate it all around and drive like a crazy man and bump into boxes and not go through them now I have some limited physics based move let's take a look at those scripts and see how they actually work first I want to look at the force with our force use transform rotation one here we've got a public class move with force use transform rotation and we cache the rigid body in our awake method on line nine so that we can use that later to add force and we have our movement force variable that we can adjust the speed with and remember here we had to crank it up to 20 because when we're not rolling over we actually need a little bit more force to get over the friction to be able to slide along the ground in our fixedupdate we check to see if the W key is held if it is then we add the force really nothing different here from our previous one except for using transform dot forward so just like in our transform position movement if you saw that we use the transform related vectors our transform forward and our transform right here we use transform forward transform right and again we use the negative values to give us back and left in our rotate with Mouse script we have a turn speed so we can adjust how fast our player can turn and then we have an update method which gets the axis value for Mouse X which is actually a float let's just spell that out it's just a number and it's gonna be positive or negative some small number then we call transform rotate we pass in the horizontal value which is just that Mouse X value this is again how fast we've slid the mouse left or right or how far we've slid it left or right the turn speed we multiply it by and then vector three that up because that's a 1 on the y axis so we're just rotating on the y axis and we're using world space to do that rotation now let's take a look at jumping how would we implement a jump with this well we can turn on our jump script hit play see what it looks like and then go take a look at the code so here I've got it I can move around and I can hit space and jump and kind of bounce around whoa and now I just jump kind of wildly now I'm going to turn off the rotate with Mouse one I'm gonna go to move with force and see what that looks like with jump ok oh and turn off to freeze rotation options so now I should be able to kind of like roll around and do a weird bouncy jumping and let's get the scene viewing game view together so we can get a nice big view of what that looks like kind of jump around and some interesting weird behavior not sure what what use it is yet but I think it could be fun let's take a look at the jump script and see how that ties in and how that works with these other two scripts so if I expand it out you'll see that we have a jump force set to 300 and I'll open the script up and we can take a look at the jump class it has a rigid body that we cache and awake very similar to a lot of the other scripts that we've had and then it has a jump force with a value set to 300 and this boolean value called should jump which is either gonna be true or false it will be false by default and then I have a big note about why we have this should jump and why we have an update and a fix that because you might think well in our update we'll just read and see if they press the key then we do a jump and we add some force in the up direction or maybe we'll read it in the fixed update the problem is very well spelled out here I hope in the description or the comments that you can download this code and go look at yourself is that it's possible for us to miss a space call so it's possible that we hit space and the player doesn't actually jump if we put everything into our fixed update so if we change this to say something like this I'm gonna copy it and paste it if we didn't have this I'm gonna comment this out with ctrl K ctrl C if we had it just like this it's possible that the user presses space we run an update get key down it's already processed and it doesn't it's it's already returned to true for that update then we run another frame fix that be it hasn't run yet cuz fixed update doesn't necessarily run with every update frame so we run another update and the user is still holding space so get key would return true but get key down no longer returns true and then our fixed update calls and get key down as returning false and we've missed that jump so this is why we read our input and update and then we act on it and fixed update when it comes to these key down type events if it was just to get key I'm gonna hit ctrl z twice to undo that by the way but if it was just a key check so it wasn't a get key down and it was for every frame it would be totally fine that's why we can do that for our movement but for our jumping we don't want to do that and we want to separate this out and really spell out the issue there now let's have a little fun and see what this all looks like when we change our objects from a cube to a sphere so I've got a rigid body based spear slash ball and I'm gonna turn it on and hit play you're gonna see a little bit of magic and then we're gonna talk about how it's working so hit play and I'm not gonna touch the keyboard and look that my ball is just kind of bouncing around and it just keeps bouncing now I'm hitting the keyboard to move it around a little bit and you can see this cube actually moves with it too so if I move I get the ball bouncing and moving and the cube moving I'm gonna ignore the cube though and just control that ball so why is this fall so bouncy and Wiggly and how am I making this happen it's actually a side effect of this sphere colliders material so a lot of time people don't notice this but the colliders actually have a material option if I go to my box Collider I could set it here as well and we can just add in a physics material and it'll adjust the way that the object responds to physics and collisions if I select my bouncy one you can see that I have my bounciness set to 1 which is the maximum 0 to 1 is 0% to 100% essentially and I've turned down the frictions all the way to zero I also set friction combined to average so that if it bounces and combines with something else it has little friction it'll average the two out and bounce combined to maximum so it'll always be the maximum bounciness it's a way to allow you to have multiple physics surfaces combined and do what they should do here I just really want it to be super bouncy now how do we create one of those were just right click in the project view go to create and choose physics material where's that at right there choose physics material you can name it create your own give it your own settings and play with them as much as you want add them to the two objects that are bouncing and colliding try it out and play around it's a lot of fun and it gives you some really interesting gameplay options now let's take a look at controlling a character here we've set up a capsule for our character so we just create a capsule with game object 3d object and capsule have added a rigidbody and frozen the rotations and then we have the jump script that we've used previously it just adds force in the upward direction and the rotate with mouse that reads our mouse x and then spins our transform rotation so let's take a look at this we can move we can jump we can kind of bounce into things or bump into things at least I can't really walk up those steps but I can kind of jump up here let's see what the script looks like what this code looks like and how it works here we have another move with force use transform rotation script but it also has a clamp of velocity feature on it and if you look at line fourteen we're just checking to see if our velocity for our rigidbody has approached or gone above our maximum velocity and if it has we just stop adding a force so this is kind of a soft cap where we're gonna get to around ten we'll go maybe a little bit over stay kind of right around that level we won't hard cap at exactly ten though and stay there but it stops us from going too fast and our previous one if we don't have this eventually our cube or our character just keep building up velocity and speed and get out of control so here we have a way to control it and make it a little bit more like a player movements that's why when I hold down W it doesn't ever actually just take off and get faster and faster and faster to the point where I'm going out of control one question I get all the time is should I use a character controller or a rigidbody what's the difference why do they both exist well a character controller is really just kind of an extension of a rigidbody it does a lot of the things a rigidbody does and then a couple more things that characters generally want to do like being able to walk up slopes and control gravity a little bit more individually or give some extra I guess tight control over an object without scripting too much of it more character level controls so let's see what a character controller setup looks like here I've created a capsule and added a character controller to it I've in my rotate with MAO script that just reads that X movement and spins us around and then I have a character simple move script I'm gonna expand it out ensure that it has a move speed and is grounded that isn't an option we check this is just to kind of show what it's doing so I'm gonna hit play we'll take a look at how this moves and then talk about it so I turn I run around you can see I kind of slide along that a little bit smoothly it's a bit smoother than my default physics rigid body setup that I had so when I had a character that was moving with a rigid body it doesn't get that same smooth sliding I also can just kind of walk up these slopes look that I can get up there and kind of run up there it's a little bit small but I can get up there at least and if I had my jump I'd be able to jump up there as well and I move over here and just fall down let's take a look at the character controller settings now so we have a slope limit that adjusts how tight of a ramp we can walk up and we have a step offset which adjusts how big of a step I can dig so I can go up to here I can't make that last step but if I turn up my step offset a little bit more there we go now I can step up there and if I turn it up even higher like 201 not 11 like a 1 I can even step up on to this block here so slope is again for the inclined if I wanted to just crank it up and be able to turn walk up something that's more than 45 degrees I could turn that up and the step is for going up those actual stairs the radius and height you can see in here so if I go select my character and just start adjusting my radius of the character controller you can see it moves and that's what it's using for the physics to control the character doesn't use the actual capsule Collider and then the height so if I crank this up you see my character is much taller sometimes our radius and capsules or radius and height don't necessarily match our character exactly we want to get them as close as possible but sometimes we need to do some weird stuff well you may even have a character controller that's down low and the thing that's kind of flying above like a hover and bird with character controller part that's moving around is controlled down lower or the whole body or something so let's take a look at this code we have our character simple move script character simple move caches our character controller and awake and then it has this public pool is ground that again is just so that we can see it in the editor and we'll talk about that well after we look at this code then we have a fixed update method and the fixed update we read the horizontal and vertical axes into these two floating values these are actually set up in edit and then project settings and input but here's the input manager and I've expanded out horizontal and you can see that the left and right or a and D keys control the horizontal and if I expand out vertical it's down and up or SW there's also another horizontal and vertical down here by the way and if you look at this the difference here is that this is set to joystick axis so this is actually gonna read from game pads or controllers where this one is going to read from key or Mouse and it can be a little bit confusing because you can kind of set up keys and button names that don't really map to things right so you might think that if you didn't have this one it would still work it won't you need to have both of these in here if you want to be able to read from the keyboard and the controller they're actually separate that's why they're duplicates it's intentional but a little bit confusing I understand that all right so let's go back into our code we read those two floating point values that are either gonna be 0 or a value between negative 1 and positive 1 so it'll be positive if I go up or right and negative four-fight go down or left on my controller and then we give a new direction or we create a new direction of extra three named direction by passing in the horizontal for the X and the vertical for the Z we create a movement vector and we do that calling the transform transform direction and this is kind of where that magic comes in that's making it map to world space or local space where it actually controls our character and we can have the camera snapped behind him instead of moving around in world space so instead of using like a world transform we're creating this local transform here and that's all this is this movement transform Direction just takes a world position like forward and converts it to my local forward multiply that by move speed and then we call character controller dot simple move I'm gonna add a space here character controller dot simple move returns back whether or not the character is grounded which means whether or not the character is on the ground so we call simple move we give it our movement amount that we want to move and we just get back whether or not it's on the ground we're not doing anything with that or just kind of putting it into the editor because I wanted to show that we can read that and we can use it if we need to so let's take a look at it I'm gonna go in here I'm gonna go back to my game view and I've got my character selected and I just want you to look at the is grounded part so let's change the height back here to one or what was it one point one point eight there we go now watch the is grounded value if I go up here I'm still grounded you can see there's a little bit of a period where it drops and if I drop off the edge a little spot where I'm not on the ground and we can use this to control things like jumping on a player so if we want to have a player that jumps then we don't want them necessarily jumping when they're not grounded if we have a character controller set up we can just read that and tell whether or not they're on the ground in fact let's go do that now here I've set up a character with the rotate with Mouse script and my character move script so that I can turn rotate and do this nice little jump action so I can jump up on these cubes bounce around like a weirdo and have some fun I also wanted to note though that the main camera is just a child of this object and that's how it's following it around so if I go to the main camera I hit F you see that it's just a child with an offset here and I'm aimed down there so as I move my thing around it just kind of follows me nice and tightly so I can jump now let's go take a look at the character move script well first let's note that we have a move speed a jump speed and a gravity something that we haven't had in previous scripts I'm gonna open this up and take a peek at it we have an awake that cache is our character controller and then we have a move Direction that we're keeping up here this vector three let me get rid of the private key word because a little bit of extra noise we don't need there so we have this move direction and then we have a fixed update our fixed update reads our horizontal and vertical axes we get an input Direction vector that we create from our horizontal and vertical giving them to the X and the Z we transform that into our local objects transform so our forward will be forward four object and then we take that transform direction and we're putting it into this vector three called flat movement you'll see why in just a moment that is the move speed times our time Delta time for our fixed update because in a move call which is not going to be the same as the simple move call we actually need to take into account time and Delta time the move call or the simple move call that we have here we don't need to pass in that Delta time it's going to be kind of automatically figured out for us we also can't pass in a y-value we can't go up and down with simple move move is gonna allow us to do all of that but it also makes us put in the extra work of handling that stuff so we're taking our transform direction and multiplying it by Delta time in our move speed and that gives us our move direction but our move direction isn't exactly the flat movement it's the flat movements x and z values and the current move directions y-value so we're keeping this vector around every frame and the y-value is being controlled completely independent of our inputs our input for horizontal and vertical are only controlling the Z and the X and the y is controlled some other way to see how its controlled two lines down so how do we control it while we check here on line 25 if the player jumped then move direction dot Y is set to our jump speed which I think I've set to 0.5 else so if they didn't jump this frame and they're grounded then we just set their move direction to 0 so if the character controller is on the ground move Direction 0 we don't need to push them down at all now is grounded is also a check on the character controller so I had mentioned that we can use the simple moves return value to see if it's grounded we can also just read the is grounded property on the character controller at any time to see if it's on the ground and then the final thing we do if we're not jumping and we're not grounded is apply some gravity by subtracting gravity times Delta time from our move direction so we just keep applying gravity every frame this value gets lower and lower and we fall faster and faster and then we finalize it with character controller dot move and pass in move direction to make our character actually do the movement the final thing I want to take a quick peek at is our player jump which just checks to see if the character is grounded and they pressed space so this little check right here make sure that they are already grounded and we can't jump it for not grounded and that's how it works gives us a nice simple character controller that we can move and bounce around and play with the values of a bit navigation is another type of movement that we use a lot in video games where we want a character to kind of figure out its own way to a position we don't want to necessarily script that it moves from point A to point B to Point C and then figure out all of the code to move it to a specific character or any of that stuff instead we can use the built-in navigation system to do a lot of that work for us and you'll be amazed at just how easy it is to do so here I've got a navigation set up just to show kind of what it looks like and I'm going to hop into game view click around and watch my character move so I click and the character moves to wherever I click if you can get there it's at least as close as you can do it and the camera is just stuck behind him so I'm just gonna move so I'm gonna click right up there and watch as he just kind of finds his way up the ramp so how does this all work how do you set it up well there are a couple of things that are important to note here first let's take a look at the character our character is set up with a capsule just like every other character we've made where we go to kick game object and choose 3d objects and create a capsule but then I've added a nav mesh agent component that's already built in and a nav mesh character script I have not changed any of the defaults on the nav mesh agent script you know we just have this character but I've done a little bit more to the scene and this is the really important part if we go select the environment here which is actually all of our objects I'm gonna go to the scene view go to environment just turn it off and on it's everything except for the camera and the character notice that the static checkbox is checked if I drag this over and expand it out the really important one here is navigation static I can actually uncheck this and hit yes change children and just check the navigation static and hit yes change children again which is going to make it apply to all of these children down here under the environment so if I just have that it's enough to allow me to do my navigation but not alone I still have to do more I still have to get to this navigation window and tell it to do a bake now if you don't have the navigation tab open you have to find it through window and it's currently under AI and navigation it may move it seems to move a lot so just be ready for that now when I've gotten this window opening by default it's on the object option it's just showing me the there's really nothing here but if I select the ground you see that it has the navigation static checkbox here this is another spot where I can set that I can go through and click on each one of these and set the navigation static option I can also set whether they're walkable not walkable or if they're like a jump area now I've got all of mine set to walkable because I want to be able to walk on all of them and I need to go to the Bakke tab I've already done this and that's why this is blue but by default it's gonna look like this you gotta go in and it looks like this and you go why I don't have my blue part maybe you don't even have this on and you go hit bake and you know I still don't have a blue part on so what you need to do is make sure that you've hit bake in that you have show knavish check then it'll show up if I hit clear it should wipe it out hit bake and it should reappear it's really fast because my map is tiny and the computers fast the speed may vary if you try to do this on a giant world map but on something reasonable it should be almost instant or really really fast there are also some options here you can adjust for the radius of how big a character has to be for when it's doing this generation and or how big it thinks the character should be and how far they can jump how far they can drop down a little bit more advanced stuff that we're not gonna dive into today but those are the two important things there's also an area's option where we can set costs of areas so that they'll prefer lighter areas or easier to lock areas like roads over swamps for example and then there's some option for controlling agents and making different types of agents that have different settings again we won't dive too deep into that I just want to get at the character and see how it all works so we have the nav mesh agent and we have this one script let's see just how much code I had to write to make it happen that's it so we have our nav mesh agent and awake being cached and then in our update we check to see if the mouse button 0 was pushed down so if I left click mouse button 0 is the left-click button and down as the event when I fire or when I first click it and if so we create a ray array is just a invisible imaginary line into the world and we're doing it from the camera using our screen point array and giving it the input mouse position so what this is going to do is create an invisible ray into the world from wherever we've clicked and it's going to in intersect with whatever object we've clicked on essentially then we'll get a boolean back when we call our physics dot raycast and we pass in a ray now there are a lot of ways to call a ray cast this is just one of the many or one of the easier of the many ways you know also passed in a bunch of parameters I'd like to do it this way they'll create the ray pass that in and then we take an output parameter of hit info now I'm assigning this to a bowl we could also have this inside the F statement but I want to make it very easy to read so we check to see if we hit something so if the raycast actually intersected with something so we actually clicked on an object that had a Collider then we figure out where we clicked which is that hidden faux dot point it's actually part of this we're not really figuring it out so much as putting it into a named object or a named vector and then we tell our nav mesh agent to set the destination and this is it this is all of the work that we're really doing the majority of everything else is just figuring out where we've clicked and then we just say hey go to this point so if we have a thing or a place that we want it to go to all we have to do is call set destination and it will go there we want to move to another objects position just give it the object position and it will try to move there that's all there is to it navigation is relatively simple for the majority of stuff there's also a project on github before the newer navigation stuff I would look into that it may or may not be into the package manager when this comes out or when you watch this video it's kind of constantly changing but the navigation system is just getting better and better and more powerful you can do navigation systems where they walk on ceilings walk on walls do all kinds of fun cool and interesting stuff so it's a neat way to move things around and it's really really accessible and easy to use now let's look at animation as a way to move objects a lot of people overlook this as a possibility but it's a really common way to move objects when we have a set pattern or set movement that we want to do and we want to be able to have some tight control without having to write a lot of code having can manage that we're going to be able to maybe easily change it so here I've set up my character controller character scene but with an animating box in it and I'm gonna go to the inspector tab and take a peek at it we have on here an animator that has this animator controller called animating box if I open it up just double click on it it has a box moving script with a motion called box moving seems like a lot I really didn't create all of this it's all kind of automatic but I want to show you what it looks like and how it all works so then we go into the box moving and if I double click on it actually get the animation window which is different from the animator window animations are the individual independent animations of objects or sets of objects animations are a state machine or animators are the state machine controller that allow us to switch between different animations the animator controls the animations okay so if we jump to the scene view and I select my animating box and I look at this animation window suddenly I can see my animation here and if I hit the play button I can see that I actually have an animation where my cube moves back and forth I'm gonna hit play run into this cube see what happens real quick and then talk about how you can create these animations modify them and just show you how easy it is to do so I'll hit play and we're in game view mode and look at that oh the box actually knocks me around now one thing I can't do is jump on it and have it slide me back and forth I've actually done a video on exactly how you can accomplish this in a couple different ways that you can accomplish this so if you're interested in that just go look through my videos for sticking to objects and you'll see it there otherwise let's dive into how this actually is created and how we create our own animation so I note there's no code here I didn't write any code this is all just an animation that I've created so I'm going to delete my animating box and I'm gonna go through the steps to create it yeah deleting that and I go into my project I'm going to the box moving here and I'll delete the animating box dot controller so I just have my scene here now create a new cube go to 3d object cube I'll reset the position and I'm gonna move it over to where I want so here I'm holding right mouse and using a a to go over and I'll hold control and just gonna snap this up to somewhere I want let's go somewhere a little bit different let's make it maybe go up and down instead of left and right that might be a little bit more interesting probably not but we'll see okay so I've got my cube I'm gonna rename it to animating cube and then we'll go to the animation window and here I just hit the create button I need to have the cube selected but if I hit the create button it's gonna give me a pop-up and I'm gonna be in the right folder already in this animation one but I'll call it um cube up/down give it a name and it gives me a dot Anam file if I look in here cube up/down so I'm gonna go back to the animation tab though and hit record here I can just take this and move the position just a little bit and it'll add a keyframe for the position now I can drag this little timeline over here and go to let's go to the halfway mark at point or the zero 30 drag this down to the ground in fact you know what let's use ctrl shift and use that neat little snap to ground and then I'll go over to the 1.0 area again and drag it back up in fact you know what I'm gonna do I'm gonna delete that select it and hit delete I'm gonna select this little keyframe and hit ctrl C to copy it click over here to one and hit control V to paste it so that way I've got this nice smooth down and right back up to its original position if I hit play I can watch it kind of in action and if I hit play in game I should be able to see it kind of pushing up and down and stomping now it's gonna act a little bit weird on me and just kind of knock me out of the way because that's just how it works and it's not a very smooth surface and my head just kinda gets knocked over but you get the idea of how we can move these things around I want to show you that we don't just have to do position though I could also have it come to here and at this point maybe hit the E or R and go to the scale tool and make it wider so maybe it goes down and gets huge and then it goes back up and gets small again so I've added this keyframe and it actually and it for all of them because I didn't hit the record button so I hit undo undo undo and redo with ctrl Y then I hit the record button go to this point and go back to that scale tool and drag this out and then drag this out now if I drag up and down you see that it shrinks and then stretches and it doesn't shrink back down though so I can just copy this frame again here go click on it ctrl C click over here control V and paste it now I've got this like going down and splattering in fact I'm gonna make the splatter even bigger let's make this like a four and a four tickets it goes down shrinks or goes down flattens out and then gets back look at that neat little movement and it would still kind of with that lift our player up there's something strange not a great great look but I think you can get the idea of the cool stuff that we can do with animation and now we can even animate these physics objects and have them interact with the world all right if you made it to the end here please just drop a comment and let me know if it was useful or not useful or helpful or if you got confused or if you had any questions drop those down below - or suggestions it really does help a lot even more than just hitting the like button if you can't leave a comment though the like button is helpful as well and I also wanted to say a special thanks to everybody on patreon you guys are awesome I really appreciate the support of you and everybody on this YouTube channel it's great doing this stuff I have a blast with it and I hope that you're learning a lot too alright thanks again and goodbye
Info
Channel: Jason Weimann
Views: 137,876
Rating: undefined out of 5
Keywords: unity3d, unity, movement, brackeys, transform, rigidbody, charactercontroller, character controller, charactercontroller vs rigidbody, unity3d tutorial, gamedev, unity 3d, animation, navigation, navmesh, navmeshagent, animator, animator controller, animation controller, physics, physics material, game development, unity 3d tutorial, fps, easy, unity3d college, programming, unity tutorial, unity tutorial for beginners, unity tutorial 2020, tutorial, beginner, coding, gaming, game, ai, how to make
Id: fyV77lN1Yl0
Channel Id: undefined
Length: 49min 24sec (2964 seconds)
Published: Sun Jun 28 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.