Complex Animations made Simple with Blend Trees!

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this video we're going to look at animator blend trees this is a super useful animator feature to help you blend between various states of animation you define some parameters and some logic and it automatically chooses the right animation let's begin all right so here's what we want to make i have my simple character just standing around this is being animated using sprite based animation but you can use any animation type and right now it's on idle and as i move right it plays the right animation as i move left that animation move up and move down so as you can see all the animations are correctly chosen and as i stop moving it goes back to idle and it also chooses the correct idle animation so here is the final animator component with an idle blend tree and a movement plenty and each of these trees is composed of a bunch of animations so as you can see it takes a bunch of parameters and then decides which of these animations to play so this is a really useful feature and the character sprite that i'm using here is from the complete visual scripting course that i'm currently working on one of the games is an action rpg using this character so if you're interested in a visual scripting course check the link in the description alright so this is our goal let's get to it okay so here i am in my starting scene all i have is just a simple player character completely static with no animations okay so let's begin by selecting the player game object and we're going to add a component and it will be the animator component then as you can see it requires an animator controller so on the project files right click go into create and down here we have the animator controller so the player animator controller and here just drag the object okay now if you double click on the animator controller yep out pops out the animator window and now in here if you right click you can create a normal state so create the normal one then in here you can select the motion which is the animation so that's how you add a normal animation however you can also right click and over here create a new blend tree so there it is the blend tree and now if you double click you can go inside the blend tree all right so this is the inside of our blend tree and now with the one tree selected here in the inspector we can see a bunch of options first one we see the blend type so essentially over here we have the number of dimensions and for now let's leave it at the simplest one possible so just one dimension then we can select the parameter so by default it already added one but let's add a proper one from scratch so click on the plus icon to add new parameter and then click make it a float parameter and we're going to name this the horizontal movement so in here on the parameter click on the drop down menu and select the horizontal movement and now we can get rid of the default one so there it is okay so we select the parameter and then here we have a list of motions so these are all the separate animations and over here on the project files i already prepared some animations previously so in this case i'm using sprite based animation but you can use any type of animation and for now let's click on the plus icon in order to add a new motion field and let's select the wonk right animation so there it is you can see that it added a new state and let's add another one also make it a motion field and for the motion select the walk left alright so now here we see some parameters so the first one is over here the threshold and you might see that the value is locked if so over here you have a checkbox for automated thresholds so if you untick this one you can now manually edit the threshold so for the one left let's put it at -1 and you won't write at plus one so down here we can see the horizontal movement and we can click and drag in order to modify it and over there we can see which one is working so then here you can head on plane there you go he's walking left and if i move it to pause one now he's walking right so yep exactly as we want so in this case we're using sprite-based animations so it will only play either one or the other one but if you were using some more complex animations with actual movement on the objects then it would interpolate between them so in this case on the threshold it won't choose which animation to play depending on if the value is above the certain threshold so if the value is above minus one then we're going to play the left animation down here we can hit on plane or to preview so at minus one it's playing that one and if i push it up to one yep there you go it's playing the one right animation okay so that's the threshold and next to it we have the animation speed so this is essentially the animation time scale so using this we can make an animation go faster or slower so for example let's make the walk right go twice as fast and if there you go walk right is going really fast and one left is going normal and lastly you have the mirror setting which lets you mirror an animation all right so those are all the settings and with this we have our basic lengthy working now all we need to do in order to make this work is to modify the horizontal amount parameter through our code so we just need to do that and our animations work perfectly okay so let's go into the player script here it is just a very very basic character controller i covered something similar in a separate video so go watch that if you want to learn more essentially here we just have some keyboard inputs and then we're calculating a move direction vector then we have a test if we are idle or if we are moving so it's a very small very simple script and now it's in here that we want to modify the parameter on the animator so first let's go up here and define a field for the animator animator and on the way we grab the animator get component of type animator and then down here okay we get our inputs we make our movement direction vector and if we are moving let's go into the animator and we're going to set a float parameter so set float and then the name we used was horizontal movement and then for the value which is going to be our move direction vector dot x all right so just like this everything should be working let's test okay here we are and we something at the anal animation state so it starts off walking and right now it's on the default walking left but if i press right yep there you go please it won't write animation and if i press left yep there we go playing left alright so our basic logic is fully working now let's just add the idle animation state so for that we need to go back into the parent animator so up here you can see the layers that we are so we are inside the blend three so if we click on this one yep we go up one level all right we have our plan tree and we can also rename this so the movement entry and then we want to add the i don't state so let's right click create a new state let's make it also a blind tree and let's name it our i don't like dream and we're going to start off with this one so just right click in order to set this layer as a default state okay so this is the default state and in order to transition between idle and movement let's add another parameter so up here create a new one make it a boolean and call this is moving and then we take this we right click we make a transition onto the movement and on this transition we're going to add the condition so if the condition is the parameter is moving if it is true then we transition into movement and then from movement make a transition back and from this one we also add the is moving and in this case if it's set to false and here in the settings let's make it automatic so with no exit time no transition duration all right so we have the logic for our starter state as well as transitioning between idle and movement so now let's set up the idle blendery and in here we're going to do pretty much the exact same thing that we did with the other one so we're going to use the same horizontal movement parameter so let's add two motions so there it is the addon left and i don't write so down there we can see the preview so with this one on minus one yep there you go either left and on the right and yep i don't write alright so that's pretty much it now back in the code in here we already have a volume for is idle so the is moving is essentially going to be the opposite of this one so it's simply testing if they move x and move y if both are at zero then the character is idle if not then it is moving so in here just going to the animator in order to call set boolean and we give the name is moving so is moving in this case we set it to true and up here we set it to false we could also obviously just put it in here and put it not is idle so just like this would work but let's keep it the manual approach for now okay so with this let's test okay here we are starting off idle on the left side yep there you go and if i press the left button if there you go he's walking left and let go and yep he's back into i don't now move right yep moving right let go and yep he's walking right all right so it seems like all of our logic is fully working awesome so the logic is all working but so far we've been using a one-dimensional point tree so everything is working but only going from left to right if we go up yep everything does not work so let's fix that over here on the movement one tree again we have our blend type and we have one direction and then we have some other blend types so the next one is 2d simple directional so this one is for when you have just one animation for each direction then you have to the freeform which is when you want multiple animations for each direction and to the freeform cartesian which is used when the animations are not based on direction so for our case we want the simplest possible so let's pick just the 2d simple directional and now right away we see that our graph went from a flat graph to a 2d graph so we now have an x and y axis and up here we actually need two parameters so let's create another one for the vertical movement so in here create a new float call it the vertical movement and use this on the y axis and over here we need to add all the other motions all right there they are and now instead of having just a single threshold we have the position x and the position y all right so they're on setup all of them with their thresholds correctly set and in here you can click and drag the red position so this will modify both of our parameters so as i move to the right yep there you go it's modifying the horizontal amount so that one is at one and the vertical is pretty much at zero and we can see down here on our preview yep there we go we've got the one cryo animation and if we go up there yep we've got one cup then walk down and one left alright so you can see that everything is perfectly working so based on the values that we give our parameters we're correctly choosing the right animation so our movement one tree is fully working and now we just need to add the exact same thing over here on the island laundry alright so there it is set up exactly the same so over there we've got along right idle up idle down and item left alright so we have both of our trees working both of them dependent on the horizontal and vertical movement parameters and on the main animator we've got our basic transitions based on the is moving volume so all that's left is to set our vertical movement through our code so over here it's very simple we're just going to do pretty much the exact same thing so we're going to set another float for the vertical movement and this case is the move there dot y alright so that's it that's all we need to do and everything should be working so let's test okay center off right on left move to the right yep wonking and idle move left walking i don't move up walking and i don't move down and walking and i don't alright so over here we have our animation zone fully working so we can move in any direction and based on the parameters that we set the animator correctly chooses which animation it should correctly play so here we have our character with movement working in all directions and naturally you could expand upon this to add even more animations to the entry and add some specific animations for up left upright and so on and fit all the diagonals alright so here it is everything working awesome so here you can see the entries in action and how they help you manage multiple animations based on just a handful of parameters now like i said this character and this point tree is from the complete visual scripting course that i'm currently working on one of the games is an action rpg where i won't be using this character so if you're interested in a visual scripting course check the link in the description if you found this video helpful consider liking and subscribing this video is made possible thanks to these awesome supporters go to patreon.com unitycodemonkey to get some perks and help keep the videos free for everyone subscribe to the channel for more unity tutorials post any questions haven't come and i'll see you next time
Info
Channel: Code Monkey
Views: 39,445
Rating: undefined out of 5
Keywords: animator blend tree, unity animator, unity animation, unity animation controller, unity blend tree, unity blend animation, blend tree, animation, animator, code monkey, unity tutorial, unity game tutorial, unity tutorial for beginners, unity 2d tutorial, unity 3d, unity, game design, game development, game dev, game development unity, unity 2d, unity 3d tutorial, programming, coding, c#, code, software development, learn to code, learn programming, unity animate
Id: W2z1BvnT0-o
Channel Id: undefined
Length: 12min 6sec (726 seconds)
Published: Thu Oct 29 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.