Adding Animations | Blend Trees, Layers, & Animation Rigging - 3rd Person Shooter - Unity Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
today i'll be going over adding animations to your player first i'll be going over adding a model from mixamo into your unity scene and it'll be a humanoid model i'll also show you how to add animations in the animator i'll go over blend trees i'll go over layers and finally i'll go over the animation rigging package which is what we're gonna do to actually aim the gun dynamically and we'll be using this so we don't have such a constrained system where we're bound by the animations that mixamo or someone else provides and so as you see here as we move around the character is blending their strafing movement which is this kind of like walking sideways it also transitions into a jump the gun is held with the animation rigging package which is inverse kinematics and then when the gun is shot it recoils back into an animation that we'll be doing and finally when we look up and down you'll see that the body aims upward and the body aims downward and that's also with the animation rigging package and before i begin i just want to thank these people that their resources on animation rigging helped me a lot with this video kiwi coder final stand studios and dapper dino so if you're interested go check them out alright so let's get started so this is based off of my previous video but even if you haven't done it this will still apply to your project but if you're interested it's the third person shooter controller where i go over using cinemachine and the new input system to make a third person shooter and it spawns bullets and also decals when it hits a wall all right so the first thing that we want to do is actually download a model from mixamo to use so if you go to mixamo.com you can sign up for free i already have an account so i'll just log in alright so here we have a bunch of animations that pop up and really they have a lot here that you can use but for now we're going to go to the characters tab and here you can actually choose a model that you want to use so you can choose any model that you like for example if i choose this mannequin which kind of looks weird then we can download this with an animation but i'm going to scroll down and i'm going to get amy since i think the design is pretty cool you could also upload your own character here and there's some instructions learn how to prepare your model and they also have this upload and rigged 3d characters with miximo that you can use they also have a fuse cc program that's free and there you can actually customize a character it's a bit limited because it's more like a human character and it's semi-realistic but if you'd like you can make a character there and it can import into mix and mo automatically but for now i'm just going to choose amy and so if you just select a character and click use this character and then if we go to the animations so we're actually not going to use any of the animations here except for jumping because unity has some animations that we can use that fit our character better alright and then once we go to the animations tab we'll actually only be using a jumping animation since unity has some animations that we're going to be using on their end so we'll only be needing a jump animation so you can search up jump here and select the one of your choosing or there's also pistol jump which i'm gonna be using a pistol for this video there's also like an assault rifle jump i'm gonna go with the second one here and so you'll see that we can preview our animation here and there's some options that we can choose overdrive is basically how fast this animation goes it's like the characters in overdrive mode see super fast and now it's slow the middle is fine character arm space is the space in the arms between the character so if you make it big then the arm space is huge and vice versa and trim is just like trimming the animation if you want it to start at the beginning if you want it to start at the middle as so but it kind of looks weird when you do that and here you can reset these values and then mirror just means that it's facing the other way it's kind of mirroring the animation so we can just click download and some options will pop up so for our format we want to click it and go to fbx4 unity this is the fps of the animation 30 is fine with skin basically means if you want the actual mesh of the character to come with the animation which obviously we do want that to happen and keyframe reduction is basically a way to reduce the number of keyframes in the animation saving some performance but it might result in a less accurate animation and as an example here's an idle player animation and you'll see that it has a bunch of keyframes everywhere to make sure that it animates correctly with keyframe reduction it'll try to remove some of these if the difference between them is not large enough so it makes it simpler but it also makes it less accurate so for now i'm going to go with none and click download so when it's done downloading just go to your assets right click create a new folder and let's call it animations and you can just drag the fbx that was downloaded into there and so if we click it we see that some settings on the inspector pop up so there's a couple things here there's the model which has some settings here that you can look through the important ones that we want are these three rig animation and materials so for the rig we have legacy generic and humanoid generic is just as it sounds it's a generic rig it can really be anything that you want it to be so if you have an animal with four legs then that would constitute a generic rig because it's well generic and you choose this for anything that is not a humanoid because we don't want to use the legacy one unless we really need to so humanoid basically means it has two legs and two arms and so since our character character's gonna have two legs and two arms then we want the humanoid animation type and so is therefore a difference between the generic and humanoid well you can definitely have a humanoid character be generic but the benefit of setting it as humanoid in unity is that if you have another humanoid character then you can easily retarget animations from one character to the other even now when we're gonna import unity's other animations we can easily apply it to our humanoid model because those animations were made for humanoid models however if it's generic you'd have to map those animations to the characters another big benefit of the humanoid is that we'll easily be able to use ik or inverse kinematics which in this case we're going to use the animation rigging package for that and inverse kinematics is basically just attaching one object to the other and when you move that object the other object responds accordingly which you'll see that we'll be doing with our gun and holding our gun with our hands or the character's hands alright so now that you selected humanoid for avatar we're gonna do create from this model because this is the first thing that we've imported we don't have any other avatar here and avatars are basically a mapping between the model and the animations so it basically communicates between both of them and when you animate it it tells the model how to react accordingly so for example let's take this character here where you can see the bones of the characters so this is the rig it basically defines the bones inside and so basically an animation all it's doing is moving each of those bones according to what the artist said it like in their animation program or maybe in unity and so the mesh is the skin of the player and it's you know covering the bones and so when you move this bone for example the mesh will be deformed in order to follow that bone correctly and so for example when you animate a character the avatar basically communicates all this animation and keyframe to this character so that it can move the bones accordingly and you can see that when you click on your character there's a skinned mesh renderer and this basically manages that mesh deformity when animating the bones so i hope that made some sense so for the avatar let's just put create from this model click apply and you'll see that if we kind of zoom in here it's still grayed out and that's because the textures are not imported correctly so we have to go to materials and for mixamo we actually have to under location we can do use external materials legacy and click apply and then here this is saying that there's a little bit of an error with the normal map and so you can just click fix now and it'll fix it for you and it seems that maximo uses some legacy materials that's why and so if we go to the animation tab you'll see that we have our pistol jump animation here along with some settings so if you want the animation to be looping you just select loop time however with jump you really usually don't loop it we also have some extra settings these can help you kind of adjust the rotation of the character and bake it so that it stays like that just in case it's not facing the way that you want it to face and if we kind of drag this bar from the bottom you can see the animation preview and press play you'll see that it's jumping and if we right click we can actually kind of move around the player just like we do in the unity editor which is pretty cool you can also speed it up a lot and there's some extra settings here in this animation panel you can attach curves to the animation which will add additional data to your animation you can also attach animation events which is pretty cool a mask is kind of an avatar mask which it tells unity what part to feed the animation into so for example if you only wanted to feed the animation into the legs and not the upper body then for the avatar mask you'd select only the legs if you kind of click this humanoid option here you can select what parts you want the animation to affect and so if it's red that means it doesn't affect it if it's green it does affect it so now we click play you can see that it's not affecting the upper body which it looks really weird and so i'm just going to select all of them for motion the root motion node we're not going to be using root motion which basically means that the animation is controlling the movement of the character we're gonna be controlling the movement through our player controller script and yeah just click apply here to save all of your settings so now let's import our character to our player so for our player you see that i have the movement on the parent and i also have the capsule here the mesh render so i'm just gonna remove that mesh render along with the capsule i'm gonna also remove the face and the gun i've just deselected it so kind of starting from scratch now let's drag the mesh under the player so that it follows the player controller and you see it's kind of floating in the air before we change that this kind of seems a little small the character so i'm going to increase the scale to 1.5 on all axes and then in the player let's actually remove this capsule collider because the character controller already has a collider for us and you'll see that our collider is kind of down there but we want it to be in the center of our player so we can adjust the y center so that it better matches our player and maybe increase the height a little bit and then we can decrease the position y of the player so that it's on the floor 0.5 seems to work well all right now that we've imported our character you might notice that the camera doesn't really align much with the character anymore so be sure to update your camera in my case it's a cinemachine virtual camera and you can kind of adjust the screen x and the screen y along with the camera distance to better match what you're looking for in your game and i can copy these values into the aim cinemachine here you can just copy the component but there are only three values and so i'm just gonna unselect the third persons in the machine one and then for the aiming one i'm just gonna make sure it's a little more inside and put the screen x to this side here so however you like it all right i'm gonna put the third person cinema machine back to active here and so now let's add a animator component to our player so under player add component animator and this will control the transition of our animations along with animations in general so we have to make an animator controller which we can just do it in our animations folder if you like so right click create and then go find animator controller and we can just name it player animator controller all right if we click that you'll see that we get something like this here oh and if you're interested i actually have the layout selected to tall which kind of looks like this and then i just moved the game to the bottom and i lost the animation tab so window animation animator alright so here's our animator tab which we have this entry here so this basically just links what's our default animation which we don't have any right now but if you actually drag in our animation that we downloaded from mixamo into here it'll set the entry to it so this is the default one because it's the only one we have there and so if you have multiple animations for example you can make a transition between them and you can click the transition and specify different conditions that you want to be met to transition from one to the other which i'll be going over shortly but before that under the player we're gonna want to attach the controller that we made so drag in that controller to the animator and for our avatar we can just click this circle here and find the avatar that we imported which is the chapter 46 which if you select this arrow here you'll actually see that it's right here this little person sign and if we click play you'll see that it's just jumping which is pretty cool all right so let's just download the strafing animation the strafe animation so for that let's search up unity standard asset github click the first link here and let's just download this github repository with this button here download zip and so once you've extracted it double click it go to assets standard assets characters animation and then you can select either female or male i'm just going to go with female animations strafe so strafing will be kind of like our walking and idle animation and so i'm just gonna go back up to strafe and drag and drop the whole folder into the animations of our assets and so we have this forwards one and then we also have this one to the right and then we also have one going right and backwards so kind of diagonal and so how do we link all of these together if you use this normal way attaching transitions from one to another it's going to be really messy however luckily unity has something for that called blend trees where it kind of blends between the animations in a tree-like fashion so if we right-click and create state from new blend tree we have a blend right here and we can just call it strafe and i can right click it and set it as the default so now that will be the default if we double click that then we have this blend tree that we can select here you'll see that this one blends from zero to 1 and depending on the value it'll choose an animation that we have not set up yet but in our case we want to be moving in two directions so we want to have different animation forward backward left and right and also diagonally which is not 1d which in this case this won't cut it we need two values the value that we're moving forwards and the value that we're moving left to right as an example for 1d you can add a motion field here and you can add an animation so for example if you select one of these arrows here you can drag in that triangle animation here and if we select and add another motion field and we just select another animation you see that this one is zero and this isn't one and so when you go from zero to one you'll see that this one is selected right now and when it's one this one will be selected so you can actually play it here now it's one but when we go to zero it starts running but it's kind of limited since it's 1d and so now there's a couple of 2d ones that we can pick and unity has some great documentation on their page on 2d blending 2d simple directional is basically when you have a simple animation if you go forward you have one animation such as running freeform directional you can be a little more free and not only can you have a running animation but you can have a walking animation before that running animation so you can have multiple motions in the same direction and for free form cartesian this is not really used for directions but more like when your x and y parameter represent different things such as angular and linear speed for now we're going to go with 2d freeform directional because it offers us a lot of flexibility and you can expand on it if you'd like so select blend type 2d free form directional and you'll see that i'm just going to delete these motions here we have this kind of graph here and it's now a 2d graph that we can see our points on and i'm gonna delete one of the motions and you'll see the graph disappears so basically we want to have something like this where we're moving forward and we have one animation we're moving diagonal we have another and just to play it here so you can see it if i kind of play the animation i can move this red dot and you'll see that it'll slowly change animation depending on where it is on the graph pretty cool so you want to do something like that so pay close attention because this can get a little confusing so we want to have positions on all those corners of the screen so zero zero is going to be our idle but to make life simpler for us let's start off kind of in this easy counting format so we know we want a position in one one add another one we want one in one zero add another one we want another one at one negative one because this is negative one the left side the right side is one and the middle is zero add another one now we're gonna do zero so zero one zero zero zero negative one now it's negative one one negative one zero and negative one negative one now you see we have all the points on the grid there so now we just need to assign our motions to it so for zero zero we want the idle motion so strafe idle so find that here and drag it there for zero one that's going to be our forwards this one right here which is going to be the first one rapid straight forward so just drag that right there and now for backwards we're actually going to use the same one as forwards which you see if we kind of move this red dot downwards it'll look like it's running forwards but if we change the animation speed to negative one it'll actually go backwards and now it looks like it's running backwards which is pretty cool you can also use one of the animations here now let's do these so this is running to the right one zero so that's the second one and just drag that there now we have to find the one where it does a right and a forward so diagonal right here the fourth one and that's one one and then the next one is the backwards one so assign that to the third one motion and now for these over here the left side negative 1 0 is to the left so we can just assign the rapid strafe right to the left one and change the animation speed to negative one so kind of to flip it and do the same for the other ones so negative one one so let's find the one that's one one drag it there change the speed to negative one negative one negative one let's find the one that's one negative one right there drag it there and just change it to negative one all right so we have our basic blend tree but you see that we can actually move it to preview it because we don't have any parameters so in the animator component go to parameters and let's add some components here so if you click here you can add several components in our case we want to float and we'll have a float for our movement on the x and the z so we add the float just set move x and move said and then in the blend tree under parameters up here you can select x and then z which is going to be our forward and backwards and you can just delete the blend parameter in the animator so now we click play on this animation you'll see that it's in this strafe idle and if we move this red dot forwards it'll start running forwards if we move it backwards it'll start running back we move it to the left it'll go to the left if we move it to the right it'll go to the right diagonally so doing this i realized i made a mistake you see that this one kind of looks odd along with this one and it's because i actually switched them the negative 1 1 and negative 1 negative 1. so the right run forward we're actually going to put it to negative 1 negative 1. and the right walk backwards we're going to put it to negative 1 1. so now we preview with our little red dot here you see that it's now moving in the correct direction along with this one and that's because i mixed it up in my head this one should actually correspond with this one if you put the animation speed to negative one and this one with this one diagonal all right so now that you have your blend tree set up let's actually put these parameters in code so go to your player controller so under player right click player controller edit script alright so now we want to get a reference to our animator we can do private animator animator here we can do an animations comment then in your awake function you can do animator equals get component animator so this will get the animator attached to the player and we actually want to reference our move x and move z parameters in our code and so usually you can do something like animator.setfloat as an example and you can pass in it with a string and set the float as so but you see that we can possibly make mistakes if we do this in more places than one with this string we might misspell it so we can actually get this id and pass that in instead and we can do that with an integer so int move x animation parameter id and if you just copy that and name it move y and right here just going to uncomment this you can do animator dot string to hash and you can pass in the string so move x copy that move z and now that will save the animation id oh and i seem to have miss spelled this it should be move z not move y all right and as an example now you can actually pass in the integer instead in the set float alright so now this is going to go in our update function when we move our character so here we can do animator.setfloat and we can pass in move x animator parameter id and then we can pass in our input because our input will determine in what direction our player is moving which this is actually wrong but i'll show you in a second what we should do all right and so you just set that float and now you see that this is our default animation if we click it you can see now we'll see the blend tree if we move forwards you'll see that our move zed is now one if we move to the right you'll see that our move x is negative one and so forth you see it moves a little slow in the diagonal direction and that's because under scripts under your input action that defines your actions under the move if you click the wasd that we made it's normalized right now so it will instead of 1-1 if you click both at the same time it'll normalize it to be a length of 1 and so that results in x and z being 0.7 if you don't want it to be normalized you can set it to just digital the issue is that when it's both one and one at the same time it'll look like it's moving too fast diagonally so for now i'm just going to keep it as so all right and so the reason why it's going so fast is because we're setting the values from our script directly and the input system doesn't really blend between those values it just says if it's pressing it it's one and if it's not it's zero so we're going to have to manually blend the values which isn't really that hard we can use this move damned function which i need some parameters up here for that so this is a vector 2 vector 2. so for the smooth damp we'll need something to store our current blending so current animation blend vector and then we'll also need something to store the speed of the animation velocity and you can use that for whatever you want so vector 2 animation velocity i'm just going to copy that and so here instead of doing movement with the input x and y we can actually blend it up here so that the movement will actually match the animation a little better so if we do the current animation blend vector we can do vector 2 dot smooth damp and smooth damp basically smoothly kind of interpolates towards that value so it takes in the current goes to the target and it takes in a smooth time which is how much time you want the animation to smooth between the current and the next one and there's also a max speed so you can set it if you don't want it to pass a certain speed but we know it will never be over 1. so if we pass in the current which is the current one and then we pass in input which is our target then we have to pass in our velocity so you have to do ref animation velocity so this passes a reference to this vector which will just populate that value with the velocity and our smooth time which i'm just going to call animation smooth time so we'll go up here and then i'll do a serialize field private float animation smooth time and so here the smaller the value is the faster because that's basically how fast you're going from the current to the next one so we can just do like 0.1 as an example for now and then if we scroll back down instead of here for the move input x and y let's replace it with the animation and also down here in the animator set float all right so that's blending our input which is another cool thing so now you see that if we move to the left it'll kind of go slowly and it'll be much smoother than before and if you go in your player you can actually change it so if you want it to be faster you can set it to a smaller value .05 as an example and it'll reach that value faster and so forth but our player is pretty slow let's like moving in slow motion so we can change our player's speed to be maybe 10 and you see that now it's way too fast seven is a good speed and you'll see that we're actually floating on the ground still because our collider is a little big so if you set it to 1.26 it'll work so remember those values in the character controller we do 1.26 and then for our player speed we can do seven and for our animations to move time we can do 0.05 all right so now you learned how to use blend trees which is pretty cool now let's do the transition to jumping so if you go back in your animator if you remember we dragged this pistol jump here and so we want this to be called at any time we don't want to wait for any animation to be over because we want to jump whenever we press the spacebar so this we're going to call from our script and then once this is done we actually want to go back to our movement animation so you can just right click and make a transition to your strafe and if you click the transition here you'll see that this is important has exit time so this animation will only transition to the next one once it has finished the animation time if you don't have this selected then you have to add in a condition here so it will transition only if move x is greater than zero and you can also do less than but for now we only want to make a transition when it is finished so when it has the exit time so back in our script let's actually make a jump so just as we did with the animation parameter we can similarly do it with the jump so here we can do int jump animation jump animation in the awake equals animator dot string to hash and we called it pistol jump or you can call it whatever you want if we scroll the way down to our update function or wherever you have your jump code so here i'm doing if the jump has been triggered and the player is grounded then jump right there i'm going to put that animation there's a couple ways you can do this you can do animator dot set trigger and you can pass in that animation and this is for if you already have a transition to it for example like so and you can add a trigger here which you can just call it jump and then your condition can be that jump trigger so whenever that trigger is activated go to the jump which you can definitely do but you see that once you have a lot of animations having these arrows everywhere will start to get really confusing so with something like this we can just do it in code another thing that is common is to do animator play and pass in that animation however the issue with this one is that it doesn't actually fade between the animations so it'll cut roughly to the jump and it just won't look smooth and so luckily unity has a nice function for that and it's called a cross fade so it kind of crosses between the current and the next animation into kind of fade and so for that you can just pass in your jump animation and then you just pass in how long you want that transition to be so i'm just going to say animation play transition which we need to put up here so serialize field private float animation play transition and let's put a value of point 15 f which is pretty short time so we don't want it to take a long time in transitioning all right so now we have that cross fade there and when it's plays it will go back to the strafe animation so you see we're running if we jump it'll jump however you see that it kind of does this weird thing when it lands and we can change in this arrow here this is like the transition so this is the current and this is the next one you can either move the strafe back so it starts at the strafe animation a little later or you can move these blue arrows so that it kind of cuts off the jump animation and you can also make the transition longer by dragging here and we can just jump and you'll see that it's less prominent if we just move it a little more looks much better and what's good is that if you actually stop playing the animator saves any changes that you make which is such a blessing because i can't imagine not being able to change the animator values and having it reset when you stop clicking play alright so now that we have that done let's get to the really interesting part which is using the animation rigging package to make dynamic animations so the first thing that we want to do is go to window package manager wait 30 years go to unity registry and then find animation rigging click install all right and if for some reason you can't download it it says some resources locked make sure to close vs code or your ide because sometimes it locks the folder and then unity can't access it quick mention here you can also download a samples package which they have all the constraints examples there if you're interested all right so now that you have that downloaded let's begin first we need an actual gun which you can either get from the unity asset store open game art you can make one do you for now i'm just going to search up pistol gonna select the 3d low poly with bullet shell once you extract that folder we'll have a bunch of stuff here if you go to your project we can make a new folder create folder and we can call it models and then here you can right click and create maybe a pistol folder click it and then drag those three files into there which we actually don't need this blend for because that's for blender so we have this nice pistol which we can put it under our player so you see that we already have a gun that we made previously but let's just delete it since we don't need that and to make life simpler let's right click and create a new empty object called weapon so that we can easily child things to that weapon and it'll move with that parent game object and this is huge so the scale point zero one point zero one point zero one oh now it's a little too small tiny gun there we go all right so point zero three on all x's seems to work well for the pistol very nice all right and now you can just kind of position it kind of where the hands are so what we want to do is that whenever we move this pistol we want our hands to actually attach the pistol and move with the pistol and with that we can do animations such as recoil where our gun will move back and our hands will move accordingly and so what the animation rigging package will do is that when we move our pistol so if we move our pistol the hands will follow accordingly which is pretty cool then we can do kind of a recoil animation whenever we shoot the gun and so the way this works is that as an overview on our player we have a rig builder and so this is just a collection of rigs that are on our player and rigs are basically kind of like the bone rigs but there's the animation rig and then there's the rigs that we're gonna make with the animation rigging package which can override the animation rigs and so here's just a list of different rigs that we would have in our character for example we would have one for maybe the arms rotating with the pistol and we can have another one for our chest looking up and down with our aim and so what we're going to do is make an empty game object under the player one for each of our rigs and then we attach this rig component to it which we can adjust the weight of the rig so this is the one where we grab the pistol so if it's zero it's the default animation that's currently playing if it's one we completely override the animation and you can do some sort of blending to make some cool effects then under the rim we'll have a game object for our constraint and our constraint basically we're telling it what it should do so how this bone should move according to our target which in our case our target is our gun because we want our hands to target this position of the gun so our hands will move with the gun our hint this will be our elbow hint so if we don't add any hint a unity is going to have a hard time knowing how the elbow should be positioned to reach that location if you want to you can add a hint and position the elbow maybe more outward more inward and such you can also change the weight here you see that we change the weight for the right one it stops holding it and this is the actual bones that we're going to be changing so as you can see we have the right arm which is kind of like the shoulder we have the right forearm which is near the elbow and the tip is the hand so these are the bones that will actually be moving in order to follow the target and also adhere with the hint that you set additionally there's going to be some other settings that you can do you can change the weight of the position and rotation of the target if you want to do that individually see the rotation i set doesn't matter anymore because it's at zero now and the hint weight which you can see it's not a big difference but if we put in zero kind of changes a little and there's also this maintain target offset which basically maintains the offset between the tip and the target and if you're interested in learning more about the constraints you can go to their documentation page constraint component and they have detailed explanation of all the constraints so if you go to the two bone one you'll see that they have some documentation on and each of the values and there's supposed to be a gif here for some reason it just it just doesn't want to load for me all right so now we're going to use that to bone constraint so under our player let's make a new empty game object and i'm going to call it the pistol arm rig and for that we need to add in a rig component now we go to our player let's add a rig builder this is the collection of our rigs and you can just drag in the pistol arm rig there another cool thing that we can do under our player is add a bone renderer component and so now when we select our player we won't see anything but if you go to animation rigging this is actually recommended but you go to animation rigging bone render setup and then you can see the bones in the character and it'll be easier to select them in the bone render component you can select how you want it to appear pyramid line pyramid seems nice you can also change the size and you should also under the animation rigging do restore bind pose to put it in the t pose because if you don't it might result in some weirdness when you do the animation rigging all right now that we have the pistol arm rig we want to add a empty game object one for each arm so right hand ik control d f2 left hand ik under right hand ik let's add in that two bone ik constraint okay and so our target is gonna be the gun but it's gonna be a little hard since we want it to be near the grip so under the weapon we can create a new empty game object we could call it ref underscore grip and the reason i'm calling it ref is so that if we need to search it up here it's going to be easy to search it up you just put ref and then you can see what game objects are reference game objects and so if you double click it and kind of zoom in you can set it more towards the grip which we'll be adjusting later then back to right hand ik drag that ref grip into the target and then for the other ones roof mid tip and the hint we'll have to go in here to the miximo bone setup if you hold down alt and you click this arrow it'll open up all the folders but i'm going to open it up manually spine spine spine we want to go to the spine and i'm going to open up the right shoulder one right arm right forearm and right hand we're going to use these so in the right hand ik dragging the right arm to the root the right forearm to the mid and the right hand to the tip and so we want to add in this hint for the elbow and you can do this under the right hand is fine so create an empty ref right hand hint and then under the right hand ik just drag that hand in all right and if we click play um you'll see that we have this error here basically saying these game objects is not a child transform in the animator hierarchy meaning that there's some issue with how we parented these objects and after a little bit of digging i found out that first of all this has an animator component attached the miximo that we imported which we don't want and the meshes that we're changing have to be siblings to the rigs so with that we can just unparent it from there and we cannot because it's a prefab so if we right click here prefab unpack completely it will just basically let us do whatever we want with the prefab let's bring that out of this game object and we can delete that now and if we click play you'll see that now something is happening which is great now we want to kind of adjust this so it more accurately matches that gun placement and before that i'm just going to change the order because i like it like this better and i want to mention that in the source objects for the target if we click that grip reference under the animation rigging right here this pops up we can click it and this will basically give us a gizmo that we can look in the scene to better position our references double-click that shape or just click it and we can actually drag in for the target i like to drag in a box effector so now you can see a box gizmo which is pretty nice and then for the hint if we go there let's add in a ball effector and we can make this another color like blue we can go here and now we can see this blue sphere here we can also change the size and the position and rotation as well so this will help us with adjusting our values all right so i'm going to click play i'm going to select the reference grip and so basically this is just a process of adjusting the values until you reach what you want i see that if i move the grip forwards it's the gun is a little too far from the player so we're going to want to bring the gun a little bit forwards so i'm just going to unclick play bring the gun a little bit more forwards and maybe a little bit to the side so it'll look like more aiming at the correct direction if we click play now we go to the reference grip we can change it until basically the player kind of aligns with that gun here it's a little hard since we're also rotating the player but you can also change the rotation of the grip so that the player rotates the hand and this is kind of the way that you just set up the animation rigging you just change the position and rotation you copy the component transform you unclick play and then you paste the component transform which isn't great but it's something and you'll see that our locator has moved down here one thing i want to mention you see that now when we move our weapon our hand isn't actually moving with the weapon and it's because it only moves with the grip but we want it to move with the weapon and so to do that we can add a rig transform component here and now when we move the actual parent the weapon our hand will move with it and so this basically tells us that there's a reference inside of this game object with the rig transform and so unity will now know that when we move the parent instead then then we want the hand to follow that parent as well so make sure to add that rig transform to your weapon all right and so now it's just kind of a process of changing the values you want to change the reference grip remember we don't want to change the actual parents transform while setting this up so you can kind of adjust the rotation values here and maybe move it a little bit backwards it might be a little bit easier to do this in the scene by selecting the square but you see that now the the elbow is turning which is good it looks kind of weird so let's copy that transform copy component paste component values now our hand is kind of holding the gun but our gun is still too high so if we bring that weapon pivot down a bit maybe a little more forwards a little to the left again this process copy paste now the process is changing the grip so that it kind of matches it copy paste and now one thing i want to mention is the kind of the elbow you see it's kind of in the player which looks weird and that's what the hint is for so now we're hinting at unity that we want our elbow to be a little more up so we kind of aim it more outwards and maybe aim it towards the back we want the elbow to go backwards and that's a pretty good position so now copy that component paste and yeah you just kind of adjust this until you get something that you like it doesn't look absolutely perfect right now maybe the weapon should be a little bit more forwards and maybe the weapon should be a little rotated as well so it kind of more aims towards the center but yeah that's basically the process that you do and we do this for the left hand as well quick thing is that our player is still in the ground having a little bit of difficulty with this let's change the height of the player to 3.39 all right and that will fix it all right now let's just do the same thing for the left hand so we made it we already made a game object here so now let's just add in that two bone ik constraint and let's do the same thing for the left hand so instead of the right shoulder we get the left shoulder left arm left forearm and left hand the left arm goes to the root left forearm to the mid and left hand to the tip and if you're wondering why the left arm instead of the left shoulder it's because i just found it that it works better because the left arm is more here towards the shoulder while the left shoulder is more towards the neck which results in some weird movements and so in the left hand let's just create a new empty game object ref left hand hint this is going to be the hint that we're going to just pass into our left hand right here and for our just to make our lives easier in the left hand hint let's also add in one of those nice gizmos you can find in the project a ball maybe we can make this one green and we can also increase the size a little bit you see it's over here now for the ref grip here let's rename it to ref right hand grip control d ref left hand grip so the left hand drag the left hand grip to the target also be sure to add in one of those gizmos there maybe make it yellow so that's easier to see right now it's just the tedious process of kind of changing it so that it looks good enough and it doesn't help that our player keeps rotating so in the player controller you can kind of just comment out the rotation code so it won't rotate with the look alright in the left-hand grip you can just kind of change the rotation you see the left hand is totally in the wrong direction probably the x is what we want to move just a tedious process of getting it just right and you can change the gizmo position here just make it fit right click the transform copy component paste component values now let's check how the hint is doing it doesn't look bad but if you like you can kind of change it so that it's kind of more outward and backwards whatever you prefer you can copy component paste values and ta-dah now when we move around which i took off the rotation but you see that our hands are moving with the gun and if we change the gun position then both of our hands will move with the gun although you know if it's too far away the hands won't be able to reach the gun of course so make sure the gun does not extend past the constraints all right make sure to uncomment that rotation code and let's fix up this weapon a little bit because we can't shoot out from this anymore under the pistol create a new empty game object and it's going to be our barrel and just put this to the center right there and under the player game object make sure in the player controller to you know fix your references so for example for the player we need the barrel transform so just drag that there and now our bullets will shoot from there very cool but you see that one problem we're facing now is we aim up or down our player body doesn't really aim up or down which is kind of not realistic and then that's the next part which is the next constraint so add a new rig this is going to be called the aim rig because we're aiming our body add a rig component here under the player be sure to drag your aim rig there press plus then drag in your aim rig create a new empty game object and we'll actually need two first we're gonna aim the chest chest aim and then we're gonna aim the head head aim and here let's also create an empty game object and call aim target so this is the target we're going to be aiming at and it's basically going to follow this cursor so that we aim at the center of this location and so what's interesting is that now that you have multiple rigs the order of the rigs actually matter so if you go in the documentation this is kind of old it says here the purpose of the rig is to collect the constraint components of course and then generate an ordered list of eye animation jobs which the animation rigging is actually performed by the burst compiler which is why it's very fast and it will be applied after the animator evaluation so after the normal animator the animation rigging is applied and the order of the jobs or the order of the hierarchy matters as you can see it follows this step first traversal and you should only have one rig per rig hierarchy so if i make a rig here i should not have a rig as a child little side note but thought that'd be useful all right and so for these two we're going to use something a little bit different and we're going to do the multi-aim constraints so we like the word aim because we're aiming and if you go to the documentation there's a gift that shows you how it works but of course doesn't work for me why would it so for the chest we can adjust the neck our constrained object will be the object that we're going to be aiming the bone so we're going to want to aim the spine right so we can actually select the spine here let's aim the top one spine one go to chest and just drag in that spine one to the constrained object there's a couple options here that you can change this one is important aim axis so the aim axis specifies the local aim axis of the constrained object to use in order to orient its forward direction so basically what's the forward direction of this aim which in our case is going to be the z-axis which it's already put there the up is the up axis of our aim so it's going to be y so here the local up axis the world up we don't really have to set but you can set that it can be used to stabilize the upward direction and our up was just going to be the scene up if you put object up do you need to put a world up object here and then our source object is going to be our target so we can just pass in our aim target here and speaking of aim target this is a reference so we want to name it ref aim target and since it's a reference to our aim let's also add in a nice gizmo let's put a ball locator let's make it pink so it's visible and let's make it really big since this is going to be far away from the player and so we're going to be changing this target depending on where the player is aiming and our chest is going to move accordingly another thing that we can do is add some settings here so if you want to constrain the constraints constraints squared you can maintain the rotation offset there's more offsets here you can constrain certain axes and you can do the maximum limit that this bone should rotate so in this case it has full rotation negative 180 and 180 360 degrees so as an example let's aim oh geez so now when we click play this thing looks down at the object it looks horrible but if we look forwards and then we look up you can see that now looks up it's going to break its back like this but cool so let's actually copy this aim constraint component and in the head aim game object let's paste component as new instead of the spine as the constraint object let's do now the neck the neck will move up and down and the head will move accordingly you can also add a little offset here so that if it's not how you want it at the start oh jeez let's move this forwards the target aim and you can see that now our head aims moves nice um you see that the chest is a little exaggerated when we aim so you can actually remove the weights a little bit so it doesn't seem so drastic so let's move up let's move down you see that now it's moving you can make it a little more less prominent 0.6 to the target which looks good and you can limit the heading if you'd like so in the chest we can do maybe 0.6 in the weight so it's not that much and so let's actually change this target through our script all right and so now we want to change the target depending on where we're aiming so let's do a serialized field private transform aim target so this is the target that we're going to be moving then in the update function we can make a little line here or make it another function so for our aim here we're just kind of using the camera position and shooting a little ray forwards from it with a certain distance and so we can kind of mimic that so then we can do aim target.position equals camera transform.position plus camera transform dot forwards times some distance so aim distance that we can set so up here we can do serialize field private float aim distance which you can just set to maybe one or something in our player you can drag your ref aim target to the aim target here and then when we click play our character is totally not facing the correct direction because we put the offset a little too short from the camera so we're going to have to increase that offset so maybe if we put 20 it will now aim more into the distance we can put like 10 which is fine so now when we look up and we look down you'll see that it follows that aim which is pretty cool so make sure to update that aim distance when you press play okay you made a dynamically animated character that can aim up and down and it can also dynamically grip the gun awesome the last thing i wanted to cover is kind of like a extra which is kind of making an animation a dynamic animation for the animation rigging package so for example and when you shoot your gun you can kind of like move the gun back and it will move the hands back so you can do kind of like a recoil animation another cool thing that you can do which i won't cover but when you shoot for example you can kind of animate these targets so that you can kind of move the body backwards when you shoot and for cinemachine you can also add in a cinemachine shake which i do have a video on that if you're interested and so before i show you how to do that when you noticed our gun doesn't really move well with our character and that's because our gun is not parented to our actual body it's parented to the player and so what we can do in this case is we can parent the weapon under the miximo spine or you can pick whichever one you want and kind of mess around with it and see what works best for you so for example if we parent it under the spine one then it'll move more in tune with the character so you can see that now it'll move more with the character but of course it's not looking very pretty because it kind of got messed up when we change the position so just make sure to update those values and you know do that whole shebang which is changing the reference over and over again quick thing i want to mention is that you can decrease the size of the gizmos since they're really in the way while you try to do this all right and so when we click play you see that when we move around and we click shoot our character moves back and this is because i placed the barrel right on the gun so when the bullet spawns the collider kind of clashes with the player and sends it back so be sure to move the barrel a little forwards here so that it doesn't clash with the player and you can just right click the transform copy right click paste component values and now when we click play you'll see that the character won't glitch alright and so now let's add in that recoil animation that i mentioned so for that we need to go to the animation tab which is under window animation animation and you can kind of drag this wherever you want i'm just going to put it here for now and this is crazy right here but what we want to do is add a new animation so click this drop down create a new clip be sure you're in your animations folder here and we can just call this pistol shoot recoil and you see that it shows up here now and so we double click our player to kind of go to it we might have to change the collider it's pretty big let me go to the game tab and aim forwards wow i did not notice but the gun is really close to the player so make sure to adjust it as necessary copy the component and paste it back all right so now what we're going to do is that we're going to keyframe this so add changes in position or rotation or changes in something and that'll basically tell unity how this should move with time so what we're gonna do here is add some keyframes into our animation so that over time it moves to a certain position so i'll show you what i mean so click your weapon click the record button it doesn't matter if your player is down here this won't impact the actual animation now we want to actually move the gun back to kind of mimic a shooting recoil and we want to do that kind of in the middle of the animation so if you drag the line over to somewhere maybe 20 seconds here you can kind of push it back and you'll see that it'll make a keyframe animation here and it will also make one here which is the original position and so now we can just copy the original position and paste that somewhere at the end so that it completes the full recoil as so you can click the play button here and now you see that it has some sort of nice recoil animation you can go to the game view and see how it looks and if you want to edit it you can just go to that keyframe and you know just kind of edit it as necessary you can also rotate the gun on the x so that it's kind of aiming upwards when it shoots and yeah you can just play around with that until you get it and so when you're done make sure to press the un record button here and this will actually save the animation even if we're in play mode which is pretty cool so basically what this is doing is that this is over time and you're basically changing the position of something within this character in this case we've chosen to change the position and rotation unity will lurk from this value to this value over this time and from this value to this value if you want a more detailed animation you can add more keyframes all right if we click play you'll see that our animation is saved alright so now that we've made our pistol recoil animation we have to add it to our animator and so if you go to your animator tab what we want to do is we don't want to put it on the base layer because we want to be shooting while we're doing other stuff so maybe we're running or maybe we're jumping we also want to be shooting at the same time and if you do put it on the base layer so if you drag in your pistol shoot recoil animation there and let's say you make a transition from strafe to the recoil then in a layer there can only be one animation playing at a time so it will go from this one to this one and this will be the current state since this is a state machine which basically just means a machine with a bunch of states and it transitions between different states based on certain conditions and so if it goes from strafe to recoil then it'll be here and it won't show the running animation anymore so the solution to that is something called layers so let's delete that and under the layers tab click plus and we can add in a pistol shoot layer here crystal shoot there might be a better name for this and so this works the same in code we can call it with the play the set trigger or the crossfade function and so we can drag in our animation there now but you see it's the default one it's default you can tell because it's like orange and we don't want this to be the default because it'll just play automatically and we do not want that so as a workaround we can right click and create an empty state and right click and set this one as the default one and so from code we're gonna play this with the crossfade function and then we make a transition to our new state and here you see we have exit time checked so it'll go to the empty state whenever the animation has finished playing so this would be how we would set up our recoil however i want to go over some settings four layers which is very important and this will actually determine if our layer works or not so here next to the layer there's this setting button and there's a couple things we see here the first one is the weight which is basically like the priority how much weight do you want this to have so we want this to be one if we put it at zero then this would not play this animation if we put it on one this will play fully if we put it at point five it will kind of blend it in with the base layer evenly and so the way this works is that the pistol shoe overrides the base layer so any layer that is under the upper layers have more priority so let's say if we add another layer here and this one has a weight of one and it's set to override then this will override these two layers so i'm just going to delete that let's set the weight back to one and so what's important here are the weight and the blending mode so right now it's set to override so since it's at a weight of one it will completely override the base layer which you see doesn't let you select a weight because it is the base layer the base layer will always have to be here and so we set it to override it will completely override that animation and so what you can do is add an avatar mask where it basically lets you select what part of the body you want this animation to apply to so in our case we would probably want to make a avatar mask that only affects the upper part of the body since the recoil only affects the upper part of the body and attach it here and then since we added that mask there supposedly it will only play this recoil animation at the upper body and it will play the base layer animations in the lower body so just to show you how that's done you can go to your animations folder and we're not actually going to use this because uh it doesn't work well in this case and i'll explain a little bit later why so if you right click and create an avatar mask here and you can just call it like pistol aim and if you go to humanoid you see that we have like this little human here and if it's green that means this animation will affect that part of the body but you can click on a certain part like the legs let's say to mark it inactive so now the recoil animation will not play on the legs and so this is the root of the humanoid so we can deselect that as well because it'll change the position if we select that since as you may have seen when we made the animation the player kind of changed the position so we don't want that and ik just means uh inverse kinematics which we don't need that and it actually won't really apply for this animation rigging case anyways but you can just leave that there something else i want to mention is that if you want to be more specific you can use like individual bones to activate you can just select the transform here and you can import your avatar so in our case it's the chapter 46 and when you click import skeleton and expand all you see we have all the bones here for mixamo and you can manually select one or all of them depending on which ones you want the mask to cover and then in the animator in the pistol shoot layer you would go to the settings you can click this little circle here and put that pistol aim mask another setting here is sync so if you click that it will sync with a specific layer so in this case we have chosen the base layer since there's no other layer now if you see our pistol shoot layer was overridden to exactly mimic the base layer so if we actually change this layer it will change the base layer and so what this basically does is it completely mimics the base layer and so if you change this for example it'll change the base layer and this is useful for state machines that may have different animations but the same state machine so for example for the base layer we would have a normal jump and walking or strafing and then maybe for this one if the player is wounded or hurt we could replace the actual animation motion here with a wounded jump or as well as a strafe we can replace this with a wounded walk of some sort but we do not need that right now an ik pass is useful when you're using inverse kinematics but the animation rigging package doesn't really have to do with this ik feature that unity has so we can just kind of ignore that option for now but you see it says when active the layer will have an ik pass evaluated it will trigger an on animator ik callback okay so let's put this in script so what we want to do is basically do the same thing we did for the jump which is a crossfade so if we go up here and we can do something similar where we did the jump action we can do int recoil animation and this is to store the id of that animation so recoil animation equals animator dot string to hash and put in the name of the animation in my case i put pistol shoot recoil which i've just separated it to make it more consistent with the other animation namings so here pistol shoot recoil alright and now we go to the shoot gun function we can do animator dot cross fade so fade in to this animation recoil animation and then the speed which we used this animation play transition here you can use that one or you can specify another one if you'd like and so yeah that's basically it so i just want to remind you for the animator we currently have the mask this mask here along with override set and i want to show you something so you see if you click on the player you can see the animations kind of being enacted in the animator and we have two running at the same time so i'm gonna walk and see when i shoot the animation plays it turns blue but the animation doesn't actually play in the scene until like the fourth or fifth try which i tried my hardest to figure out why this happened it seems to be a problem with the mask and the override setting and something's overriding another thing that's needed and it's just not working properly but after many tries i figured out that if we just remove the mask entirely and we set the blending mode to additive and we press play then it works on the first try as expected once again no avatar mask additive weights of one and additive means that the animation will be added on top of the previous layers so instead of completely overwriting it it'll kind of blend in with the top layer and although this isn't ideal because we would want to override it i really could not get it to work this weird bug that was happening and there wasn't much information on this so this way it works but yeah now we have this awesome animated character that you know can do the strafe it blends between the different movements when you look down the body looks down and up with the animation rigging package the hands grip the gun with the animation rigging package so if you change the position of the gun the hands will follow may open that a little and if we shoot it does this nice recoil animation and if you'd like you can actually implement a recoil animation so that it also affects the body so it's more realistic instead of just affecting the hands and you can also add in a nice camera shake to the camera for the cinemachine there is an extension called cinemachine impulse listener which i do have a video on and you can kind of shift the camera upwards when you shoot so it kind of mimics the body being thrown backwards and you can also add in a little shake as well just notice something if we kind of look too fast our character kind of does this weird warping so you can go into your aim rig here and add in a minimum and maximum limit so it doesn't kind of do that weird warp see now it's less obvious when you set the minimum and maximum limit here and you'd also want to set this for the chest and the head because now the chest kind of rotates different from the head if you do have that issue just make sure to put the minimum and maximum limit of the multi-aim constraint so yeah i really hope you enjoyed this video this took me a really long time so if you did enjoy this video please drop a like and subscribe really helps out the algorithm and this video was actually part of one of the goals being hit on patreon along with the previous video which was the third person shooter it was actually third person shooter with animations but since the previous video was so long i decided to break it up into two parts and so thank you so much to my patrons for all their support i really appreciate it it really makes making these videos possible and with that i'd like to thank my new patrons in the supporters here we have thank you so much and before i say the enthusiastic tier i just want to apologize for the pronunciation if i say it wrong please be sure to drop the correct one in the comments below so in the enthusiastic tier we have marco zolt ich mahora kal kathleen edward michaela andrew kitadashi mark troutmaster handy jeff rusty anime dad of boy matthew rainbow sam tell thank you so much for all of your support again i apologize for the pronunciation and in the dedicated tier we have cloney fanny thank you so much for all of your support i really appreciate it it really helps me make these kind of videos and this channel possible so if you are interested the link is in the description i offer source code early access to videos and an exclusive discord chat and if you haven't already be sure to join our discord chat where you can ask questions post memes or just chat so thank you so much for watching i hope you enjoyed and see you next time [Music] you
Info
Channel: samyam
Views: 14,333
Rating: undefined out of 5
Keywords: animation unity, unity animation, unity animation tutorial, how to animate in unity, unity 3d animation tutorial, unity animator, animate characters unity, animate unity, third person shooter animation rigging, unity third person shooter, unity 3rd person shooter, animation rigging, unity rigging, unity blend tree, blend tree unity tutorial, blend tree, animation layers, animation layers unity, unity layers, mixamo to unity, mixamo, animation scripting, dynamic animations
Id: 583R9LgRZPA
Channel Id: undefined
Length: 69min 59sec (4199 seconds)
Published: Tue Jul 06 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.