3RD PERSON CONTROLLER in Unity - Movement Animation

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hello guys welcome to the next episode of creating a third person controller in unity and today we're going to work with the animator and actually animate our model so he runs and walks when we move them so first go to your model itself the raw file not the prefab and make sure the animation type is set to humanoid this is obviously if you have a human model if you have some other kind of model you can keep it on general then hit apply now we can actually use human animations for this model so the next step in the process would be to add an animator component to the game object itself so we're going to do that now and we're also going to create an animator controller so let's hit add component and then we're going to add animator and this will allow us to animate our model now on the avatar here if you're using my model it's probably blank just click this little button and you should see low poly man avatar and click that let's right click in the art here actually i'm going to make a folder first i'm going to call this animations and we're going to put all of our things to do with animations in this folder i'm going to right click in here and i'm going to create an animator controller and this will be the component we use to actually drop in animations on our player i'm going to call it humanoid because those are the kinds of animations we'll be using we can drag that then in the controller section of the animator and then i'm going to just get some animations here so i have an idol as you can see when i play it the player just idles i have a run if i click play the player is running and then i have a walk if i click play the player is walking so we're going to set this up in a thing called a blend tree so i'm clicking on the base layer which is a default layer you should have in your animator if you don't see this make sure you're clicking on your model and then clicking on the animator window up here and then we're going to parameters and i'm going to insert two floats i'm going to call them vertical and horizontal and you know just for the namesake i'm actually just going to put the horizontal above the vertical because that's usually what i do and i don't want to start doing something different you don't need to do it do whichever way you want to do so i'm going to then right click in here in the empty space in our base layer and i'm going to create state from new blendery i'm going to rename this to locomotion because it's going to be our walking idle and running animations in the future strafing and stuff when we handle that and i'm going to double click it here to open it up remember this on our base layer again now you're going to see this thing called entry if we click it you will see blend type let's make a or let's make our type rather a 2 d free form directional and what this means is we take in two parameters in this case will be horizontal and then vertical and based on that we can actually play animation and don't worry if that doesn't make sense right now it will make sense very soon i promise so you see here now we have these horizontal vertical bars let's add three motion fields one for every animation that we do have so we're going to ignore this horizontal completely meaning they're all going to be zero because we're not messing with that right now that will be when we do locking on the strafing let's insert our idle animation here in the first one and as you can see here the first value will be uh horizontal and the second value will be the vertical value that's position x position y so let's drop in our walk and then we're going to make the position y 0.5 and that's because we want our player to walk when we're tilting the joystick halfway to full and then we're going to drop in a run animation and we're going to make the y position one so when you're fully pressing forward on the joystick we want our player to run and idle being 0-0 because when we're not pressing the joystick up at all you don't want to move so as you can see if i move this vertical from 0 to 0.5 halfway with the joystick up we start walking and then if i change that from 0.5 to 1 we go into a full on run and we're basically going to change that number the vertical and horizontal number that is to the vertical and horizontal numbers on our input handler that we made just a couple of videos ago so if you're tilting the joystick halfway up that vertical bar will go halfway up if you're tilting the joystick fully up then the vertical bar will go fully up uh with w keys obviously since you can't tilt it uh when you press it it will just go fully up so that is how we're going to animate our character so first we need to make a script to talk to the animator and added those values so we're going to call that the animator manager and on the script for now we're only going to make the one function so let's open that up i'm just going to actually erase the start and update method on the script for now because we're not going to use them so let's erase that and the method i'm going to create the function rather we're going to call it i'm going to call it public void update animator values and then to use this function we're going to pass two types of information we're going to pass a float and we're going to call that horizontal movement and we're going to pass another float and as you probably guessed we're going to call that vertical movement so we're going to take the numbers from our input handler and we're going to pass them through this and then from those numbers we're going to make some alterations here so we're actually going to need to call the animator itself to grab those variables or those parameters rather so let's say a variable type of animator we're going to call it animator and we'll make a wake method and we'll call it on the awake and since the animator sits in the same game object as this script we're just going to say animator get component animator you could also make that variable animator public and it's dragging in manually if you'd like it's all the same so we're going to actually grab these parameters so i'm gonna make an in i'm gonna call it horizontal i'm gonna make it in i'm gonna call it vertical now the horizontal and vertical animator parameters we just made can be referenced in a script using integer variables and i'll show you how so we're going to take these two integer variables and we're going to say down here on the awake method horizontal is equal to animator dot string to hash and then we're going to say open brackets and we're going to say horizontal and then we'll do the same thing for vertical and all that means is when we now type in horizontal or vertical referencing these integer values we're actually now referencing our animator values for horizontal and vertical that's all it is put simply so referencing this variable will now go to the animator and we'll edit the horizontal parameter and the same is true for the vertical so that's cleared up we can say dot set animator.setfloat and we're gonna change the float of the horizontal parameter which we just made on the animator a while ago so then we would say our horizontal which is this one right here and as you can see see it's an integer id and then we're going to set it to a value which would be the horizontal movement that we're passing through this function which is the movement from our input manager or will be and then the damp time i'm gonna say 0.1 f and time dot delta time the damp time is basically the blend time so it doesn't look like it just it just snaps uh or it just instantly changes there's some blend time between it to make it look softer and nicer and smoother and now do the same thing with the vertical and vertical movement now you can be done right here if you'd like but i'm going to implement something i call animation snapping because i personally like the look of it it's used in a lot of games the first one i can think of because of my channel in the series running obviously is dark souls and what this does in simplicity is if you're if you have a value so if you're about to walk but you're not quite walking you're not quite running the code will snap you to a walk or run depending on how far you are so basically it rounds the values and i find that just looks a lot neater and cleaner some titles you don't need it it's mostly for if your animations don't go together perfectly or if it looks a little strange when they're halfway blended between each other i choose to use it on most of the things i do but it is just a preference and you don't have to use it but i'm going to show you how to make because it is valuable information so let's make a float snap horizontal and a float snapped vertical and then we're going to say down here if horizontal movement is greater than zero and it's also also horizontal movement is less than 0.55 f then we're just going to snap it we're going to say snapped horizontal is equal to 0.5 f so basically what this is saying is if you get a name if you get input from your controller and it's greater than 0 and less than 0.5 f it will always be 0.5 f and that means on the animator it will always play uh the walking animation perfectly and not a blend between the walking and the running or the idle and the walking et cetera et cetera and then we're going to say elsa if horizontal is greater than 0.55 f we're going to say snapped horizontal equals 1. then we're going to say elsif and we'll do the same thing for the negatives i'm going to say horizontal movement is less than zero and horizontal movement is greater than negative zero point five five f then we're going to say snapped horizontal is equal to negative 0.5 f and then else if horizontal movement is less than minus 0.55 f then snapped horizontal will be minus 1. and lastly we're going to say else if nothing else it is zero and then we can just basically copy this whole thing i'm just going to copy it but before i paste it i'm going to make a region here so basically we're going to switch out our horizontal but now for our snapped horizontal movement and then up here make a region i'm going to say snapped horizontal and then end the region down here i'm going to paste this whole chunk of code again i'm going to replace horizontal with vertical and i'm going to make your region and call this snapped vertical i'm going to fast forward here so i'm going to quickly do this you guys know what to do just replace all the horizontal vertical and down here say snapped vertical okay now let's save that now we actually call this function somewhere in our scripts and the best place to call that right now would be our input manager because that is where we're actually doing our inputs so we need to call this animator manager on our input manager so let's go over to our input manager now so right below player controls i'm just going to say animator manager animator manager and then on the awake method which we don't have so i'll create one just below this and i will say animator manager equals get component because this sits on the same game object as that script and we're going to say animator manager and we're going to save that now right below our movement input i'm going to make a private float and i'm going to call that move amount i'm going to explain to you why you're thinking we could just use our vertical input in our horizontal but well not exactly not for this version of our movement with our strafing that will be true but right now it's not we have to say animator manager dot update animator values and then we're going to say 0 because there's never any movement on our horizontal yet until we do strafing and then we're going to say move amounts now we actually have to set our move amount and our move amount is going to equal math f dot clamp which all it does is clamp the value between a value of zero and one so we're use clamp one sorry math.clampo1 and then we're going to say inside these brackets mathf.abs for absolute and we're going to pass through our horizontal value and then we're going to say plus math f dot abs vertical value and all the abs does is basically takes away the sign in front of the value so it's always going to be positive and i'll show you why that's important momentarily but if you save that now we minimize the scripts and we hit the play button and now we can run around on the screen and that is pretty cool all right so if you understood all that and you made it this far great job so back to this code here uh what we have is math f clamp 01 which again clamps it between a value of 0 and 1. inside that we have math abs which is the absolute value of the horizontal input plus the absolute value of the vertical input so all that means is if the horizontal or vertical input is negative it will make it into a positive value by change or just getting rid of the sign in front of it rather and the reason why we do this in our script is because right now in our animator we actually can't work with a negative value because our vertical input only goes between zero and one so we're going here to show you right now if i click on the blend tree as you can see go to our animation preview all we can do is go from zero to one so we have to make that an absolute value now in the future when we do walking backwards and strafing and such when we're locked onto targets we'll actually use the negative values because we'll make uh negative values for walking backwards and running backwards and strafing backwards but when we're not locked down or just moving forward we only want a positive value i hope this video was helpful as usual if it was don't forget to drop a like it does jingling up my series a lot and leave a comment to appease the youtube algorithm gods and if you're feeling super generous guys check out my patreon below all support is appreciated and i will see you guys in the next one then we are going to jump into setting up our camera and moving around screen with our player [Music] you
Info
Channel: Sebastian Graves
Views: 60,931
Rating: undefined out of 5
Keywords:
Id: YiNCqmAF3Lc
Channel Id: undefined
Length: 15min 16sec (916 seconds)
Published: Sun Jan 17 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.