1D Animation Blend Trees Explained (Unity Tutorial)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi guys today we're going to look at how animation blend trees can be used to blend two or more similar animations we'll see how we can use a simple one-dimensional blend tree to make a character's animation blend between idle walking and running okay we're going to start with this scene that has a character that can run around based on player input currently we just have a simple animation controller that transitions from idle to running when the character is moving we created this scene in the earlier videos of our 3d platformer series so take a look if you want to know how it was done at the moment the character looks okay when running at full speed we run into a problem though when using a gamepad if we only push the thumbstick a small amount our character is moving at a fraction of the speed but the run animation is still playing at full speed we're going to see how to fix this using a blend tree first of all we'll go to the animation folder in the project panel we'll delete the animation controller for the character so we can start afresh we'll then right click and add a new animation controller call this controller doozy to match the name of the character next we'll drag this animation controller onto the character in the hierarchy to assign it we can then double-click the controller to open it in the animator tab we'll create a new blend tree by right clicking and selecting create state from new blend tree this has created a new blend tree and automatically assigned it as the entry animation state next we'll double click on the blend tree to configure the animations if we click on the blend tree node we can see the details in the inspector the blend type is set to 1d which means the blending is controlled by a single parameter we're going to blend the animations as the character speed increases so 1d will work well for this there are other two-dimensional options that allow blending based on two parameters an example of when you might use this is if you wanted to blend based on the direction of movement as well as the speed we'll explore these options in future videos but for now we'll leave it set to 1d below this is the parameter it's going to use for blending unity automatically created this parameter called blend we'll go to the parameters tab and rename this to input magnitude the next step is to add the animations to the blend tree we'll click the plus icon and select add motion field then we'll assign the idle animation to the new motion next we'll add another motion this time we'll select the running animation we now have a blend tree with two animations when the input magnitude parameter is set to zero then the idle animation will play and when it's set to one then the running animation will play anything in between will be a blend of the two animations let's press play on the preview panel to see how this looks if we slide the red bar along we can see how the animation will look for different values of the input magnitude parameter as we move it towards the right more and more of the run animation is blended in until the character is running at full speed when the parameter is set to 0.5 the animation is half idle and half running which gives us what looks like a very slow jog to improve the appearance of this we're going to add a walk animation into the mix to get the animation we're going to head over to miximo miximo is a free library containing thousands of character animations we have a dedicated video on this that goes into detail on all the available options if you want to know more first of all we'll go to the characters tab and select the character doozy next we'll go to the animations tab we'll search for a walking animation we'll select the in-place checkbox as we're controlling the movement from a script you'll notice that the character's hands go through the body as it walks we can fix this by increasing the character arm space now that we've got the animation how we want we'll click download in the format drop-down we'll select fbx for unity in the skin drop-down we'll select without skin to just download the animation without the character then we'll click download and save the animation to the animations folder of our project the animation has now automatically imported into our project but we need to change some of the settings we'll check the loop time and loop pose checkboxes to have the animation loop forever we'll click the apply button to set these changes next we'll click back on the blend tree and add in our walk animation we need to drag this up so that it's between idle and run you can see that unity has automatically set the walk animation to fully blend in when the parameter is at 0.5 we can modify these values if needed by unchecking the automate thresholds checkbox for our purpose the automatic values are fine though the animation now nicely blends from idle to walking to running the final step is to set the blend parameter from a script we already have a movement script assigned to the character we'll double-click this to open it in visual studio let's go over the relevant parts of this script in the start method we're getting the animator component and assigning it to a field we'll need this so that we can update the input magnitude parameter on the animator in the update method we're getting input on the horizontal and vertical axis which we're then using to determine the movement direction then we get the input magnitude clamped in the range 0 to 1 and multiply it with the speed field to get the magnitude of movement we'll refactor this section a little to make it clearer and allow us to set the parameter on the animator first of all we'll rename the speed field to maximum speed this is the speed that the character will move if the input magnitude is 1. then we'll rename the magnitude variable to speed this is the speed the character will move based on the input magnitude and the maximum speed next we'll extract the input magnitude calculation into its own variable now that we have the input magnitude we can use it to set the animator parameter we'll do this using the set float method while we're here we'll add in a way to make the character walk using keyboard input we'll check if the left or right shift key is pressed if it is we'll halve the input magnitude to slow the character to half speed let's save the script and switch back to unity we'll set the maximum speed to 3 and press play to try it out now when holding down the shift key the character will walk and when we let go of the shift key the character will run when we switch between walking and running you'll notice that the character snaps between the animations we can make this look much better with a minor change to the script when setting the parameter there is the option to pass a value for damp time this will add a bit of damping that ensures the animation blending doesn't change too quickly we'll set this to 0.05 we also need to pass through time.deltatime let's save the script and switch back to unity to try this out now when we switch between walking and running it no longer snaps between animations if we go back to our gamepad we can change the amount we press the thumbstick to get the character to move at different speeds and the animation changes accordingly you may notice that the animation still doesn't line up exactly with the movement especially when moving really slowly in a future video we'll look at how we can make this line up perfectly using a feature called root motion that covers everything for now though hope you found this video useful please leave any questions or feedback in the comments and subscribe and click the bell icon so you don't miss the next one if you find the channel useful and would like to help support our work you can find us on patreon and coffee.com thanks guys
Info
Channel: Ketra Games
Views: 34,200
Rating: undefined out of 5
Keywords: Animation Blending Unity, Unity Blend Tree, Animation Blend Tree Unity, Walk Animation Unity, Run Animation Unity, Unity Animation, Unity 3D Animation, Unity, Unity Tutorial, unity3d, development, programming, coding, C#, Unity3d Tutorial, Learn Unity, Indie Game Development, Game Development, Learn Unity3D, Unity 3d, Unity Tutorials, unity 3d Tutorial, Tutorial, Tutorials, Game Development Unity, Unity Game Tutorial, Unity Tutorial for Beginners, Unity Beginner Tutorial
Id: a3CoTgOx5hY
Channel Id: undefined
Length: 9min 40sec (580 seconds)
Published: Tue Jun 15 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.