Say Goodbye to the Animator! NEW Unity Plugin Showcase

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
I have made a plugin which replicates most of the animator functionality all into code and you can use it for free in the description this is actually the third video on this topic in the first video I pitched the idea of using animator doc crossay to trigger all of your animations through script instead of using the unity animator it was really well received and you all gave a lot of great feedback and new ideas and with that I made the second video this video worked on separating the animated logic into a separate script rather than mixing it together in the original script now instead of writing that logic every time you create a unit you can simply make that unit inherit this logic from anywhere and it's all set up with that video you all gave even more great feedback and now here we are at the final iteration I present to you animator coder now let's look at a scene first the animations in the scene are completely managed by Script alone it doesn't use any animat transition lines or animated parameters so here's a quick overview of how to use it create a script and use the animator coded namespace then make it inherit the animator Cod of script then call play and put in the animator State you want to play and it will start playing if you want to play multiple animations back to back just fill in the next animation parameter and now it plays both of these animations back to back so that's a dirty overview of replicating the transition lines in the animator but what about the animator parameters well animator coder comes with its own buil-in parameters here's an example in the script I'm going to create the grounded parameter similar to adding a boo animation parameter now let's say the player is in the falling animation and we only want it to transition to another animation when it hits the ground simply add the on parameter script select the target boing and what state it should be checking if that condition is met then it will play the chain of animations set below now that we've gone through its quick and dirty features let's get into a more practical example so when you download animat coder it comes with three scripts it comes with the animat coder script which is the brains of everything it comes with animator values and this is where you can put on the animation States and the animation parameters that you have and you've got on parameter and I'll get to that in a second so how do you set it up well it's actually quite simple all you got to do is on your animator I'm on player at the moment I've put all of my animation States on here and you don't need to do any transition lines between them and you don't need to add any parameters either so once you're done this then open up the animator value script and you can see I've got one animation there already it's called reset we'll just keep that one there for now and above it I just add every other animation that you'll ever use now when I'm typing everything in I'm being very careful to match the descriptions exactly the same as these ones right so run is you know spelled like that and it's also in capitals as well and down here we've got parameters and we'll get to that later on in the video and that's all the setup you actually need so let's go ahead and create our own script so here I've just Creed a new player movement script I've called player movement 2 and I've attached this to the player here so here's a samurai here the first thing you need to do is you need to use the namespace so I'm going to go SG do animated coder and then the next step is we need to inherit the animat coder so I'm going to do that here and the second to last thing is we need to initialize the animat coder and you can see this is a method that goes animat coder do initialize so I'm going to call that and then you can see there's some scgly HDs here because it says play movement 2 does not implement the default animation method so I'm going to do that now and where's it gone there it is I'm going to put this to the very bottom all right and that's all you need to do to set up for it so let's get into actually playing something so I'm going to go down to the default animation method so what is this method so let's highlight over it it's the Baseline animation Logic on a specific layer so we can put in logic like uh if you are not moving then we will play the idle animation but if we are moving then we'll play The Run animation this is just the Baseline Logic for any animation so I'm just going to get the player movement right now so let's go if the movement if it's zero so we are not moving then we want to play something so I'm going to use the play Method here so let's have a look what's the two two things that we need first of all we have the animation data and that just holds all the animation data about the animation you want to play and you've got the layer that you want to play it on fortunately it's very easy in this case because we only have one animation layer but if we had multiple animation layers then that's where we would change this so here's animation data so let's click on it and let's see what it holds so it holds four things so I'm going to highlight over each of them so the first one is the complete list of Animation state names so if we go back to animator values you can see that it chooses one of these to play so this very first one this is basically what animation do you want to play now the second one is do you want to lock the layer when a layer is locked then no other animation can play now this is very important if you want an animation to play all the way through such as uh let's say you have a melee animation while you want that melee animation to play all the way through and you don't want it to be interrupted by anything else so you're going to lock the layer here now in this next one you have animation data and you can see it actually calls itself so what you can do is when you play an animation you can chain animations together through this recursion and you've also got the cross fade and this is the transition speed between the animations so that was a lot so I'll break that down so let's pass through a new animation data so if I go new uh what do I have to pass in well let's first pass in the animation that we want to play well what animation do we want to play we want to play the idle animation and that's actually all we need to do there if we just want to play an animation so I'm going to keep it there for now and then let's go El and if we are moving then I want to play The Run animation and of course let's make our play of move so I'm going to link it to the rigid body and now the very last thing is we want to actually play this so in the update method I'm going to call default animation and I'm going to send through the layer as being zero now if I click play you can see if I move and then stop it will play the idol animation and it will play The Run animation straight after of course let's make the player Sprite flip when we're facing the other direction now let's give it a play and you can see it goes idle and then to the Run animation now let's see if we can do the attack animation so over here I'm going to make the check attack Method All right so I've just made the check attack method and if the left shift button is not pressed then we're going to return so I'm going to add the logic here if you do actually press the left shift button so I'm I'm going to go play now what do we want to play so we're going to play the animation data so I'm going to go new and what do we put in will we put in the animation that we want to play so I'm going to put in Attack One so if we were to do this this would not be enough well let's have a look at what this looks like so I've clicked play and you'll notice if I start swinging then I'll keep on swinging and also if I start swinging and start walking it will cancel the attack animation which we don't want it to do we want to make it attack all the way through so how do we solve this well let's lock the animation layer you can see that we have put the animations in as our first parameter and our second one is whether we should lock the layer or not so in this case I do want to lock the layer so I'm going to set this to true so if I start by now if I start swinging then it will keep on swinging and you can notice that it won't do the idle or the Run animation because the layer is locked as not allowing any other animations to go through so when the attack animation finishes we want to unlock the layer and basically just play the run and idle animations again so how do we do that well if we have a look at what we pass in we pass in what animation we want to put through we put in whether we should lock the layer or not and then we put in whether this should be a next animation so I'm going to do that now so in the next one I'm going to make a new animation data so I'm going to go new and now this is actually all I need to do because now I'm telling it that I want the next animation to be a new animation data and when I make it new then it automatically puts it to reset so what does this mean I'm playing the attack animation and I'm locking the layer and then when the animation finishes then I want the next animation to be the reset animation and the reset animation simply just plays this again and it unlocks the layer so let's try this out I'm going to attack and you can see it goes back to the idle animation so it plays the attack all the way through with no complaints and I'm also down here I'm going to make it so you cannot flip the Sprite when you are attacking I'm going to check if the current animation and I've got this current get current animation method and I'm going to pass through which layer I want to get the current animation on which is layer zero if that equals Attack One then I don't want it to play there we go so if we are attacking then it should not flip the Sprite let's click play and swing and you can see I can walk backwards and it doesn't flip the Sprite all right so now let's get into the jump and the falling animations so this would require animation parameters so here let's go to our animation values and you can see this is where you put all the animations in and now let's put in our parameters so some parameters I want to check is if we are grounded and if we are falling so let's go back to our player movement script and let's set both of these all right so I've made a box collider called Ground check underneath the player I have ground and it's this little box collider down here so in our fixed update let's set a ball and you can see that in the animator coder that's a method here that takes in the parameter and what we're setting it to so which parameter are we setting we are setting the grounded parameter and what are we setting it to ground check dot is touching layers let's put a layer mask in here and pull it ground and so we're saying if it's touching ground and now in fixed update let's do one more and let's check if we are currently falling first of all you can't be grounded so I'm going to put that in there and your velocity your y velocity has to be less than zero so up over here I'm going to make a check jump method let's do a check first let's first check if we are grounded and if we are grounded then we'll check if we are pressing the space key and if these two conditions are met then we're going to send the player in the air all right let's test if this works so let's exit out if I hit play and if I hit space you can see that the player now jumps in the air and to actually check that these parameters are working I'm going to go into debug mode and you can see I've got the current animation down here I've got if the layer is locked and I've got the parameters so first of all if I start walking you can see it changes from idle to run and if I start swinging you can see that layer gets locked and it becomes unlocked and then I've got two parameters here I've got grounded and I've got falling and you can see ground it is currently checked if I jump in the air it gets unchecked and then falling gets checked the moment we start falling now if we did actually jump in the air let's play The Jump animation now we get to the same case as last time we do actually want to lock the layer and we're just going to keep it at that for now so if we go back now and we start jumping you can see that the player now does the jump animation and the layer is locked so no other animations will start playing you can see jump is just going over and over again so this is where we use the on parameter script so if I go add behavior and I go on parameter and I do that now let's have a look so what is the Target Perimeter that we want to check well when we are in the jump St we want to check if we are falling or not so if we are falling then I want the next animation to be the full animation and I want to lock the layer and I don't want to add any transition time let's click play and the player starts falling and now they go to the falling animation now what happens when we hit the ground when we're in the fall animation State well let add a behavior let's go to on parameter and I'm going to keep this as grounded so if we are grounded then I want to transition to reset and I don't want to lock the layer so let's play again I'm going to jump goes in the air starts falling and then it goes back to walking and Swinging again so just for the fun of it let's go to the four state here and I'm going to add a few more so let's make it so the player attacks does attack one then the player does attack two and then the player gets hit and then the player does attack one again and I'm going to lock all these layers and then at the very end they are going to reset to the original animation line so let's give this go let's jump Swing Swing hit swing and it goes back now I'll give you one more example what if the player gets hit so I'm going to add another method in here called check hit so for this one I'm going to check if the control key is pressed and if so then I want to play the hit animation and I want to lock the layer but here's the thing with a hit animation is that we want the hit animation to play regardless of if the player is locked like what if the player Oh Unity is loading but what if the player gets hit while they are swinging right well if the myid swing and they get hit well we want the hit animation to play immediately so what do we do well above it let's go set lot and this is a animator coder method so we're going to set if the layer should be locked or not and I want to unlock the layer and what layer do we want to do it on we want to do it on the first layer ler so now just putting this line before we play something will mean that it always gets played regardless of if the layer is locked or not so it's give it a play you know I can be jumping around and I press control and the play gets hit and of course the play gets hit over and over again so what do we need to add well we need to make the next animation to be the reset animation so I'm going to go new and that's it so now if I get hit I just get hit once and that's it I can be jumping I can be mid swing but then I get hit even though I'm mid swing and there we go that's how to do all your animations through code I'll put all these scripts on GitHub so you can all download them
Info
Channel: Small Hedge Games
Views: 8,488
Rating: undefined out of 5
Keywords: game development, indie game development, Unity, unity game development, unity tutorial, unity animator, unity animation, animator hell
Id: 9tvDtS1vYuM
Channel Id: undefined
Length: 15min 40sec (940 seconds)
Published: Mon Apr 08 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.