Godot Third Person Control - Animation Blend

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello this is Johnny and in this video we're going to blend the animation according to the players movement using the animation tree node first let me show you a diagram of the whole animation system we're going to build we're gonna have two states here aiming and not a me if we are pressing the right mouse button the player is gonna switch to the aiming state or else the player is gonna be in this not aiming state also a roll animation which would work regardless of which state the player is in in the aiming state the player is gonna look where the camera is facing also we're gonna have an eight-way strafe system which I'm gonna make using a plane space to denote this will have eight animations for each of the directions and an idle animation at the center I'm going to control the blend amount of this blend space 2d with the direction vector of the player in the previous video I showed you the direction vector changes with the keyboard input in the not aiming state we're only gonna have three animations idle walk and one I'm going to blend these three animations using a blend three node the blend amount of this blend three node is gonna smoothly change with the velocity I'm gonna use a transition no to hold these two states and smoothly change the current state between them I'm also going to use a one-shot node for the roll animation which will overwrite the animations on this side I'm not gonna use the animation third note from the imported 3d model as it gets complicated when you re import the 3d model I'm gonna save all the individual animations in a separate folder and add a new animation player node here I'm going to load all the emissions into this node I'm gonna set the root node as the imported 3d model I'm also going to create an animation tree and set the animation player we've just created as the annum player for the tree root I'm going to add a new animation node blend tree I'm going to add a blend space 2d node and inside at the center I'm gonna add the idle animation then I'm gonna add four directional walk animations or the forward backward right and left if I connect this to the output you can see how it's going to look you now I'm going to add all the diagonal walk animations you could also try making this using for animations but I found that it's much harder to blend the forward and sideways animation in the game engine than to just make the diagonal animations if you can make the forward and sideways animations you can easily make the diagonal walk animations you I'm gonna call these notes trave and if I click the animation tree expand this and hover over the blend position you can see a path for the blend position parameter of this blend space to denote we're gonna need this fat when we are referencing this node in the code I'm gonna add a timescale after this so I can control the animation speed and I'm going to add a transition note here for the transition I'm gonna set the input count is 2 and name the input 0 as aiming and the input 1 is not a me these are the two sisters that I described before I'm going to set the crossfade time is 0.1 to get a smooth transition between the states I'm also going to rename it as a Marans ition so now I'm going to add three animations idle walk and run these are the animations that the player already has we don't need to rename them as we're not gonna reference them in the code I'm gonna at a time skill to walk and run and call them walk skill and run skill this is in case we need to fix their animation speed now I'm going to add a blend three node and connect the idle walk skilled or unskilled respectively in the input I'm gonna connect the blend three node with the not aiming input over here and you can see the animations running zero means the second animation which is the walk animation - one means the idle animation and plus one means the run animation in the code we're going to move this value according to how the player moves I'm gonna rename this blend three node as IWR blend which stands for idle walk run blend I'm gonna add one shot node and connect the transition node here then I'm going to add an animation which is the roll animation of the player and of course a time scale to control the animation speed so if I turn this on you can see the rolled animation place regardless of which state the player is in if I click on this one shot node you can see there are some valleys you can change like fade in and fade out time but right now the default values seem fine I'm also going to rename this one shot node as role as I'm gonna reference this in the code so first I'm going to add to vector three variables slave dear and strafe the strafe T is going to change with the input and the strafe alert towards it the straight vector will determine the blend position in the blend space to denote the strafe dere is going to be exactly like the direction vector but without the rotation so arms is gonna remove the rotation and normalize from here assign the direction to strafe dear and then rotate and normalize the direction in the next line I'm not normalizing the direction here because the corner values of the place face to D are not in normalized positions I'm going to disable Sprint while we're in the aiming state by checking if the aim transition current is 1 you can see here 0 means the aiming state and 1 means not aiming also in the parameters of the animation tree you can see the parameter for the aim transition that I've just referenced in the code I'm also going to set the slave dear as Victor 3-0 when we are not pressing any movement keys so that the player goes to the idle animation at the center of the blendspace 2d I'm going to add an input for a right mouse button and call this aim in the physics process if the aim button is pressed I'm gonna set the aim transition current of the animation tree node as zero I'm also going to add another color which angle in the status node and call it aim and turn it red when we are pressing the M button I'm going to add a logic here to make the mesh rotation towards the direction if we're in the not aiming state and rotate the mesh node towards the H rod he fur in the aiming state the H rot is the global Y rotation of the H node this makes sure the player is looking towards the camera when we're in the aiming state I'm gonna load the straight vector just like I did with the velocity and set the parameters strafe blend position from the animation tree as a vector to conversion of the straight variable this is the blend position in the blend space 2d so if I run this now you can see if I press the M button the player starts to strafe the a directional strafe is working pretty well but there is a small problem here without pressing the movement keys if I press the M button look another way and release the M button the mesh rotation goes back to the previous direction that's because the direction vector is not updating when we are not pressing the movement Keys I fixed this simply by assigning the forward facing vector of the H node as the direction here when we are not pressing in movement keys and we in the aiming state so now it's working the way we want there's also another thing I want which is I wanna make the player animated little in the aiming state when we are moving the mouse riedel left for this I created a new variable called aim turn and change its valley with the event relative X of the mouse motion event I'm going to add a vector3 right in here and multiply it with M turn so now when we are aiming and moving the mouse right left the right or left strafe animations will play smoothly we also need to assign zero to aim turn at the bottom of the code now you see in the aiming state if we move the mouse the player animates a little so now I'm gonna blend these three animations idle walk and run according to how the player is moving it can be done using only three lines of code when we are sprinting I'm gonna set the blend amount parameter from the blend tree we name this IWR blend loopy from itself to one as one means to run admission when we are walking the blend amount will look toward zero and when no movement key suppress the blend amount will love towards minus one so now we have the aiming state with eight-way strafing and the not aiming state with idle walk and run animations you I also went ahead and created another UI which indicates the velocity mesh rotation and the direction variable I attached the script to the status node and set the position and the rotation of the corresponding sprites so now you can properly visualize what's going on inside so that's it for this video see you in the next one [Music]
Info
Channel: Johnny Rouddro
Views: 37,239
Rating: undefined out of 5
Keywords:
Id: 2gx1lfhqnFM
Channel Id: undefined
Length: 11min 31sec (691 seconds)
Published: Thu Jul 16 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.