Godot Recipes: Animation States

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
here's the situation you have a bunch of animations on your game character but it's hard to manage the transitions between them you've been writing tons of if statements and every time you change something it all breaks you'll solve this with godot's animation state machine so we're starting out with this sprite sheet which you can get in the link below and I've already made a character and created the animations in the animation player for all of these different animations we have run we have some attacks and all of them are set up and ready to go and in our script I have some generic movement kinematic body and movement so if we were to play this scene you know the character can move around and we are reversing directions so they face in the direction we want them to face but we want to play the animations at the right time so assuming you have all that you at you're going to want to add an animation tree and in this animation tree we're going to go over here to the tree root we're gonna select a new animation node state machine and then for the animation player property we're going to click a sign and choose our animation player node so that it will be able to pull all the animations from that node and now in here we can start adding our animations if I right-click I can do the animation and here I see all the ones that I had right so I can choose my idle animation and I'm going to go ahead and I'm going to add the rest of them now notice the message down here if we click active on now this is active and it will do the animations when we click on them so running idle run attack and now that we have the animations in here we can make connections between them let's say we wanted to connect our attack to our idle this button here connects nodes so if I click that I can draw and connect a line between idle and attack now let's let's add the other one over here and so now I have a line between idle and attack check one goes there Tech 2 and then a Tech 3 goes back to idle so what happens when I click attack 1 well it plays right in a tech 2 place but if I'm on idle and I click attack too we just see a tech 2 and that's because these transitions by default are mixed a little lighter immediate the switch mode on the connections if I were to change this one the switch mode to at end which I could also do with this one now what happens is if we're in the idle State and we want to transition to a tech - it's going to play a tech one first complete then play a tech 2 let's see that again a tech one attack - but now we stopped because the tech - of course doesn't loop so when it reaches the end of the animation it goes no further well this one we can set to auto advanced so that means at the end of attack - it's going to go back to idle watch if I click attack - I'm back to idle so let's connect up all the other animations you so here our connections all made up and so now we can transition from whatever state to whatever state so when we're in idle we can go to run along this connection run goes right back to idle if you attack you're going to come right back to idle if you're hurt you're gonna come right back to idle then we have the die animation just not going to have a return we get there it's the end and we can mark that using this button right here as the end animation and we can also mark idle here as the start animation that way it'll automatically be playing this one when the game starts I just show you can double up these things we make a little more room here we can also add another copy of the attack one in that tag too to make a to make a double attack which we could do like that set these two at end auto-advance now we have a way for us to do a double attack as well as the individual single ones now how would we control this in code when we move let's go over to our adventurers script here and we're going to add some code to call those state transitions in our for our animation tree and we're going to start by getting a variable that will hold that reference to the animation state machine and in our ready we need to get that reference so state machine equals animation tree get and there's a parameter that we have to get parameters playback is the parameter we want now we have a reference to that and that's going to get us this root node this animation node state machine know which has all of the functions for calling the individual animations and making those transitions so for example let's start with the simpler ones let's say we we want to be able to play the hurt animation when the player gets hurt so whatever item is damaging the player could call the players hurt function which is going to tell the state machine to travel to the hurt animation what travel does is find the shortest path between current node and the one you're going to so if we're in idle and we said travel - attack - it has to go through attack 1 that's how travel works and then we could have the same thing with die when the players health runs out you can tell it to travel to die and we could also set physics process to false so that we can't even move anymore now we want to handle some of the movement animations so in our get input we're gonna check the current and get the current animation state which comes from state machine diet current note that'll tell us what note is currently running like idle or run and then once we check our inputs and get our velocity we can we can put here if velocity that length equals zero then we want to be in the idle state so we're going to say travel to idle and then if our velocity length length is greater than zero then we're going to travel to the run animation we can also check for our attack in input here so after we set the speed to zero we can check if his action just pressed and we'll check for the attack action and if we did then we're going to travel to attack one and I'll copy this here and then I also have an input called big attack that lets us call the attack we're going to call a tech to number two I haven't changed the names here so I'm just using the defaults so we want big attack to travel to attack to two now if we go to attack one let's say we're in this state say we're going to here but we call idle or run we're gonna jump back so I'm also going to return if I'm attacking so I don't check the other inputs and set this in try and set the state yet so let's try that out so here I am running around I'm in run when I'm moving I'm an idle when I'm not if I press attack attack happens if i press if i press big attack I get the double attack and if I attack while I'm moving I can still do that this tutorial is part of my nugudo recipes website the goal is to collect all the best tips and lessons to help make you a better go to a developer if you like this video I hope you'll go and check out the site and make sure to hit subscribe so you'll be notified whenever I release new videos thanks for watching
Info
Channel: KidsCanCode
Views: 67,189
Rating: undefined out of 5
Keywords: programming, coding, tutorial, lesson, godot, godot engine, gamedev, game development
Id: 0bq2OIjHxk4
Channel Id: undefined
Length: 10min 31sec (631 seconds)
Published: Thu May 16 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.