Move and Animate a 3rd Person Character in Unity3D

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
animating video games is hard that's why you see so many failures all of the time and why it's a full-time job at most game studios and it's also why when i started making games i started with things like tanks and cars because i knew i only had to animate a wheel spinning around not a body moving and everything else but if you really want to get into game development animation or at least setting up animations is a skill that you really should have so today i'm going to teach you a really quick way to set up a humanoid animation or a humanoid character that can run in multiple directions and map automatically to your controls and without very much work at all the single little script one tiny little animator and a bunch of free assets so if you want to learn how to animate you want to get better at it or you just want to know how to set up a character that can run around and aim wherever you want and automatically map its animations or play the right animation based on the direction that you're running then make sure you hit the like button and even subscribe if you want but really just hit that like button i appreciate it everybody else appreciates it and we get more cool stuff like this and after you click that like button make sure you go down to the comments and grab this robot model that i'm using so that you can hook them up yourself and use them in your games or whatever else you want to do with them alright let's get started with how this character runs around how he moves and how he animates we'll start by looking at our character prefab and then we'll dive into the code that controls him and the animator so let's inspect this prefab we have here a player that's this robot model it's essentially created from this robot by dragging it out here and adding some components to it i'm going to delete the one that i've just added let's take a look at the components on our existing setup character we have an animator here to control the animation you'll see how that works shortly we have a rigid body we have a capsule collider these are here not so much for the animation but so that we don't walk through walls and so that we can collide with other objects we don't necessarily need them for our animation though i'm going to collapse these both down and let's take a look at the part that we do need for our animation and that's this player movement script it has a speed variable on it and an aim layer mask that's set to terrain let's look really quickly at what that layer is and then we'll dive into the code so under our layer section we have a new layer here if we go to add a layer you'll see that i've added it as layer number eight and named it terrain i've set that up to be the terrain object only so if i click down here on the terrain got my terrain object selected and you see that it's on that terrain layer the reason for this is so that when i'm spinning around and aiming that's the only thing that i'm looking at for my direction now you might be thinking all right jason thanks for showing me layers and prefabs but can you get to the confusing complicated stuff show me the hard code that i'm not going to understand that's going to make this all a waste of time all right let's dive into it now but i think you're going to be pleasantly surprised because the code is actually relatively simple we have a total of 49 lines of code and a lot of that is just aimed on well aiming our character not even the animation let's dive into how this code works and how it's all set up right at the top you'll see that we have two serialized fields one for our speed you saw that was the adjustable speed variable in the inspector and another for our aim layer mask i mentioned earlier that this is just so that we only aim at the ground we don't aim at walls or mummies or other things that are coming along then we have an awake method the awake method is caching our animator using the get component call and saving it off into this private field here on line eight i'm using an expression body method here because there's only one line so i don't need to add the braces before and after then we have our update method the update method aims towards the mouse just as the method here says and then does all the rest of our code let's take a look at the aiming really quickly for anybody who's curious about it and then we'll dive into how we're controlling the movement and animation i'll hit f12 to go to aim toward mouse and scroll down a little bit so you can read the code so here you'll see that we create an array using camera.main dot screen point to ray and passing in our input mouse position this just gives us array straight into the scene in an optimized situation we might want to cache this camera but for this case it doesn't make any difference then we do a call to our raycast system we use physics.raycast we pass in our ray and here we get back out the thing that we've hit ideally the ground that we've hit we pass in a maximum distance so parameter three here is the max distance to shoot this ray away into the world so imagine that the ground is 100 meters away from our camera if our value was only 10 then we wouldn't hit the ground but if it was 101 we would hit the ground here we use infinity so that it doesn't matter how far away or how far away it is our mouse is over the ground we hit it and we determined that it's only on the ground by using this aim layer mask as our fourth and final parameter or the layer mask if we get a hit so we're over the ground then we calculate the direction by subtracting the directions we flatten it out by setting the y to zero and the reason for this is that if we're up above or down below i don't want to be aiming and tilting my character up and down i want to just turn around and spin on the one axis or just spinning around the y so i need to keep the y value at zero then we normalize it so that the distance between the two points isn't calculated just the direction we turn that distance and total movement vector into it just a direction that's essentially what normalize does then we set our forward to that direction cool that's aiming not animating and moving so let's dive into animating and moving how does that work and once we've got our direction and we're set kind of in our forward direction which actually kind of does matter here for the code that you'll see in a moment then we start to read our inputs we'll read our horizontal input and our vertical input which on my keyboard is just wasd or it could be your thumb sticks on your controller by the way if you want to see this and the character running around shooting zombies and a whole bunch of other stuff with a controller in a full game make sure that you're subscribed i'm going to cover that soon in a video and a live stream and it'll be a lot of fun so make sure you hit the like button and subscribe all right once we read those two axes we convert them into a vector 3 for our movement that moves along the x-axis not at all along the y-axis so our second parameter here is zero no y movement and then we move on the z-axis with our vertical so this makes it so that w and s go on the vertical axis and a and d go on the horizontal axis gives us our movement vector if that vector's magnitude is greater than zero so if it's anything other than zero and zero we normalize it just to make sure that we're well turning it into a direction and not a magnitude and when when you think about this if you've never normalized a movement vector like this if you have a one on the x or one on the horizontal and a one on the vertical what you'll end up with is essentially like a .707 for both of them and the reason for that is that you you want a point so imagine you've got this circle here if you've got a value here and a value here on the end you don't want the combined value to be the one over here in the corner you want to be this kind of one along the curve and that's how you avoid things like running diagonally or strafing giving you extra speed you need to make sure that you're normalizing your vectors so here we normalize it we multiply it by our speed so that it's adjustable and time dot delta time so that it's tied to our frame rate and then we translate it in world space which just moves the object in world space now if we rotated our world it might move a little bit different we'd have to adjust for that right now we don't need to worry about it though we need to dive into the animating part and this is the exciting part or the fun part so buckle up here we go the animating is controlled with two floats we have a velocity z and a velocity x that we're passing in we calculate them by using the dot product so we use vector3 dot dot we pass in our movement normalized so we use our movement vector or our direction and then we get our transform forward as the second parameter we use this for our z1 this is going to tell us whether we're moving forward or backwards so if we're moving forward it's going to give us a positive value up to positive one if we're moving backward it'll give us a negative value up to nega or down to negative one we do the same with velocity x to get our right and left but for our second parameter we pass in transform.right if you want to understand how dot products work there are a ton of great videos on it that have beautiful illustrations and will explain exactly how this math works i don't want to dive into it and create those illustrations in a not as great way as them so i would look up how to use the dot product to determine going left and right or forward and back if you're curious how that works but this is the code that you need to accomplish that task once we have our velocity on the z and the x and this is telling us how fast we're moving forward and back on the z and how fast we're moving side to side on the x and remember this is all regardless of our rotation because we're using our transform forward or transform right so we can turn and get the correct values here once we have those though we pass them into our animator using animator dot set float and you might notice that this looks a little bit different than most calls to set float what we're passing in here is our parameter name first that velocity.z or velocity z we pass in the value and then there are two additional optional parameters here that i didn't even know about a long time ago and they are the damp time and the delta time what this does is allow us to smooth out the transition of this value over an amount of time so here it would set our velocity to that new value over 0.1 seconds or a tenth of a second then we have to pass in time dot time.deltatime to tell it how much time has actually passed in this frame now the reason for that is so that we don't get instant snapping if you try it without this you set this value to zero what you'll notice is that the character will run and then snap instantly back to an animation or the idle animation and they'll snap instantly to their run animation this will give it a nice smooth blending so that it looks nice and clean so let's save it off go in there look one more time at how it runs and then take a look at the animator and see how we set that up now let's take a look at the final piece of the puzzle the animator that's actually taking these values and converting them into a nice smooth animation so we'll open up our animator component and i'm going to open my player animator controller notice that it just has a single state this one movement state sometimes you'll see it called locomotion or movement or whatever other thing it doesn't really matter because it's a blend tree if i click on it you'll see that it's a blended tree right there and i can double click on it to open it up and see all of my different animations now if you're thinking hey look you have a bunch of different animations so obviously you're able to do this i don't have these animations so i can't do this i don't have eight animations for my character well you might actually be able to use these same animations and that's because these animations come from a free service they come from miximo that allows you to upload your own character humanoid character and then target any of their animation packs at it adjust them a little bit so you can maybe move out the character arm space move it in make them look really weird and put their hand against their chest or just adjust them so that they look right and then you can download an entire pack like the rifle eight-way locomotion pack that i used for this video you'll be able to do motion in all of the different directions with the side steps crouching standing and everything else with or without a rifle if you grab the non-rifle version so let's take a look at how we've set this up once you've downloaded the animations what do you do with them how do you actually make it work in this blender tree well first if your animator doesn't have a blend tree you need to go back up to that layer right click and create a new blend tree so you can either turn an existing state into a blend tree or hit from new blend street and just create one then you'd give it a name something like movement or a locomotion and then double click on it to open it up once you're in here you'll see that you don't really have a lot of options there's no motions set up there's only one parameter available in this weird slider might be a little bit confusing the first thing that you want to do is change the blend type over from 1d to 2d simple directional that'll add the second parameter so we now have access to our velocity z so i'll select velocity z for the second one and to grab it slightly off screen and we'll start adding some motions so hit plus and hit add a motion field and for this character i really need nine motions if i want to go in eight different directions including the idle or in addition to idling sitting in the middle i'm gonna need a total of eight motions let's start with just five though and then we'll add the last four afterward so i'm going to add five motions here and it hit hit oops i hit add blend tree i want to delete that second one right there that third one by hitting the minus delete i'm going to hit add motion again and add motion again and one more time so i've got five motions here and you notice that the points and positions are all kind of weird they don't really make sense values that went into here automatically don't make sense either and if i select some values in here like or select these compute position options they never seem to make sense to me either so what i want to do is set these up so that they actually do make sense i want to have one value in here that's my idle animation when my velocity on the x and z is set to zero so if i'm not moving at all i want my character to just play his idol animation so i'm going to make that the first motion i'll leave zero for the value of x and for y and then i'll go find my idle rifle animation so go down to my robot folder or the from that pack that i've downloaded the entire thing of find my idol here and grab my rifle idol and just drag it out here now if yours is not named rifle idol because it's named mixamo1 let me show you how to fix that real quick you go over to the animation here and then on this section of tabs make sure that you click on the animation tab you might be on the rig one or one of these other tabs and then once you do that you should see your animations there should only be one animation if you use the latest version of xml and you made sure to choose the fbx for unity option when you downloaded it but then you once you have that and you've got it selected you can click right here and choose a new name and hit apply you may also need to adjust the root transform rotation turning the offset of this i've noticed that with this robot character specifically he's a little bit weird on his rotation so i need to rotate him about 35 degrees to the left here or negative 35 degrees to make him look right look perfect in his animation so i'll show you that for the other hair or the other animations as we go through too so now that i've got my rifle idle selected or assigned i need to set up my other animations i need to walk forward a walk backward a walk left and a walk right and then we need to figure out what those positions are let's start with walk forward so i'll take my walk forward animation right here and i've renamed this to rifle walk forward i'll drag that into my next animation section and for this one i want the forward position or the z value to be at i think one so if we have a positive one on the z value it means we're walking forward so i just want to put a one there i want to leave the x at zero and then look at the point here it moved right up to the top so it's up above or ahead of my character now i want to add one for moving backwards let's do the same thing we'll go to our move forward let's see where's move backward or walk backward there we go we got rifle walk backwards i'll drag that into this third field and i'll set the position value for y to negative one so it's back behind me but notice that it's off to the left it's off to the left here because position x is set to negative one that should be zero so that it drops it right behind me so now i've got one right behind me and one right in front of me so i have these other two positions that are not really correct so let's fix these before we assign the animations and then we'll look at the rotation offsets on all these animations as well so i want to have a walk left what would that look like i'm thinking a negative 1 on the x is going to push it to the left and then a 0 on the y will make it centered so now it's directly to the left of me and then for the walk right we'll do a positive one and a zero as well now i need to assign these two animations so we'll find our walk right and our walk left that looks like they're right here at the bottom like my rifle walk which is this right and drag that to the right one that looks right oh no i dragged it on to the left i dragged the left one on so i'll take the left one drag it to left and then take the right and drag it to right easy to redo if i mix them up so now i've got this nice little diamond or tri or like a essentially a diamond i guess with a little spot in the middle that should control my animation i'm going to save this off we're going to hit play and see how this works before we add in some more animations actually i lied we're going to first look at these offsets so the forward offset that i ended up going with and make sure that you have bake into post check and loop time on all of these if you don't have loop time they'll only play once and then they'll stop animating so for my walk forward i went with a negative 41 on the offset and let me just briefly show you how we come to those values too right down below there's an animator preview window and actually let me unselect and reselect this animation so you can see it so click off the character click back on and i'll hit play and watch as the character moves around i can hold the right mouse button to adjust this and the goal here is to make the character move along straight or move straight along that blue axis you can see here that negative 41 is not actually a good value he's actually kind of moving a little bit to the side so i can just grab this and slide it left or right until i see a value that looks about right where he's moving straight so i think what is this negative 33 maybe would be a good value so i'll change it to that hit apply and now i've adjusted it for my animation if you ever need to get this working on your animations your characters look like they're walking one way and animating another way make sure that you go in there check that bake into pose on the animation and adjust the offset now if i also if i drag my player over here notice he's just going to walk in place if you're seeing that it's because your player probably is set up to not apply root motion but you want to make sure is the case now let's look at the offset on these other ones the walk left is set to a negative 32 the walk right is set to a negative 49 and i believe my walk backward is set to a negative 41. let's try that out and see if that looks good take my walk backward i'm going to drag the model right in here and look at that yeah that seems pretty good it's a little bit off but i'm going to leave it on the negative 41. all right so let's see this in action let's see how it actually works i'm going to take my animator drag it down to the project window and then drag around so i can see it and then i need to go up to the base layer and make this new blend tree my default so i'll right click on it and set as the layer default and i'm going to double click on it to go back into it hit play make sure maximize is off and watch the character animate and run around so i'll run to the left and look that is doing the backwards and if you look at the values there you can see his z value and his x value pretty much match up with what you would expect if i sidestep you see that the x is going farther to the left or the right or negative or positive and if i go left and right or forward and backwards you see that these z values changing if i go right into the middle i get this kind of a weird blend if i stop then he snaps back or slowly kind of snaps back into that smoothed animation with a smooth idle animation so let's see what it looks like when he doesn't have that snapping on real quick and then let's set up the other animations i'm gonna stop playing i'm gonna go back into the code for one second and let's see what it looks like without the smoothing so if i take this little chunk of code here the part that does the smoothing of our setting our float save that off with it removed and then go back into unity what you should see is this weird little snap back to place let's see it in action i'll hit play and then run over here and stop and look at that he just kind of instantly snaps so that's the benefit of that cool little smoothing feature let's go back in re-implement it and then add in the other four directions so i'm going to hit control z undo that change go back to unity and add in our other four directions so now for our blend tree we just need to set up our walk to the back left walk forward left forward right and back right animations i'm going to add in four new fields add a field add a field add a field and add a field i'll put them into the correct positions so for this will be let's see front left we're going to go a negative one on the x which is the left and a one on the y which means front and then we'll do front right which would be a 1 on the x and a 1 on the y then we'll do a back left so a negative 1 and a negative 1 and then a back right so a positive 1 and a negative 1. now i've got boxes in all of my spots i just need to assign the animation so go back over to our project view we'll find the forward let's see what do we have forward left let's drag that out to our forward and left field then we'll take our forward right and do the same take rifle not that one rifle walk forward right by the way if you have a hard time clicking through these and you find yourself clicking off you can always hit the lock button so that when you accidentally click on one of these things it doesn't change your focus on your inspector all right so we've got forward right assigned then we just need to add in the two backwards left and backwards right one so backwards right is right here backwards right that's positive on the x value click and drag it and then backwards left is the final one assign that right there now i should be able to hit play run around and watch this guy animate nice and smoothly between these different animations look at that everything looks pretty good and even as i spin around my character you'll see that the animations just pretty smoothly adjust so i think it looks pretty good it's pretty simple to set up and if you're able to use humanoid animations and set your character up it's a great way to have a nice looking character that just kind of runs around smoothly now if you really want them to be perfect you want to have the footsteps matching exactly you have a couple other options you can always go with the root motion animation where the animation's actually controlling the character movement or you could set up a foot ik system or inverse kinematics i don't want to dive into either one of those right now because i find that most of the time probably 95 of the time just getting the right animations playing and making things look just about good enough covers the situation and makes it work right sometimes our main characters need to have super close animations and really great stuff but for the majority of characters something like this will just work and it'll make your game run great and look cool anyway if you're interested in seeing more of this again i'm gonna do a full video where we'll dive into how to make this guy shoot mummies run over platforms open up things and essentially build out a little mini game you'll be able to watch that free on the stream and then watch the video later just make sure that you hit the like button hit subscribe and all that stuff and um if you have questions or requests or recommendations for this um or the extensions make sure you just drop a comment below and let me know and then i'll try to make make them happen all right thanks again for watching really appreciate it again don't forget to hit that like button and goodbye
Info
Channel: Jason Weimann
Views: 53,370
Rating: undefined out of 5
Keywords: unity3d, unity tutorial, unity, unity animation tutorial, animate, unity animation, unity animation transition, unity animation basics, unity animations, game development, animate in unity, game dev, how to animate in unity, unity 3d animation tutorial, unity beginner tutorial, unity 3d, blackthornprod, animation, 2020, brackeys, c#, animator, movement, unity animator, animation unity, blendtree, blend tree unity, development, jason weimann, dani, unity3d college, animate in unity 3d
Id: uZlSOAP76-A
Channel Id: undefined
Length: 23min 10sec (1390 seconds)
Published: Thu Dec 10 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.