EASIER ANIMATION USING CODE IN UNITY 🎮 | Unity Animation For Beginners | Learn Unity For Free

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
now creating animations inside unity can be quite messy from time to time because there's a lot of different things going on inside your animator when you have all these different states you need to swap between you know you need to create transitions from one to the other and all of a sudden you have this little drawing down here at the bottom inside your animator that is just very hard to overlook and and just make sense of there is actually a much easier way to do animation state changes inside your game which is using scripting even people who might find scripting a little bit intimidating inside unity will most likely find this a lot easier and much more simple to to do inside their games so i just wanted to share what i learned about creating animation using scripting instead of doing it using the animator so as you can see here i do actually have a game in front of me just to demonstrate how to do this so right now inside my game you can see i have absolutely no transitions or anything happening inside my animator down here but still i have animation going on inside my game my character is jumping he's falling you know he's running he's doing all these things here that he needs to do in order to animate inside my game so what i can do is i can go inside my player controller and i can create a simple state machine that can actually handle the animations as he's doing various things inside the game now just to show before we get started i just kind of want to give you an idea about how the code is going to look like because it is fairly simple to do inside your scripting so inside my script here i have a couple of different things i have some fields that simply takes care of the actual animation itself so you can see i have a animator that i grabbed of my player because i have an animator attached to my game object i do also have a current state string that is right now just checking what the current animation state is so i don't run the same animation constantly every single frame because if an animation is already running for example the running animation then i don't want to keep reapplying the same animation over and over again because it's just hogging up resources and then you just simply go down and create a constant field for every single animation you have so i for example have a player idle i have a player run i have a player jump and i play a fall so i just wanted to include those in there and anytime you create a new animation you just create another one of these constants here then inside my script down here i went ahead and created two methods i do have a method that is called change animation state which is this one that i have right here it's not long as you can see it's fairly simple basically this one just goes in and changes the animation if a new animation is about to get updated now i do also have another method at the bottom here which is simply going in and actually checking if the current animation is done playing because in some cases if i'm doing for example a jump animation i want to finish the jump animation before i start doing a falling animation so just to make sure my jump animation doesn't get cut off at some point before doing the falling animation i just have this method in here that i can include inside a if statement because it's going to return a true or false based on if the jump has actually been done and is done playing so how do we set all of this up it's actually fairly simple because once you've set it up once it is literally no effort to set up a new animation and actually start playing it inside your code so what you can do is you can actually go and just say you know what let's go ahead and create a new player character so i'm just going to do this from the start with you you probably already have a sprite that you want to use and want to animate since you are watching this video i'm assuming you have a character already so what you're going to do is you're going to grab that character you're going to go ahead and paste it inside your game so i'm just going to take my idol the first frame of my idol animation here just going to go ahead and slide it over so i can actually see it and what i want to do with this character here is i want to make sure of course we have an animation on this character so right now you know inside the animation window which by the way can find by going to window animation and then animation you can just go ahead and click create and then it's going to tell you where do you want to save this animation i do have an animation folder inside my project here so i can go inside player and then i can save this one as player underscore idle for example once you have the animation down here you can actually just go ahead and select your idle animation by the way you should also have this already inside your project i'm not teaching pixel light animation in this video i do have another video that teaches that so if you need to have a pixel animation for your game you can just go ahead and watch that one i'll actually go and link that in the description if you're interested but for now i'm just going to go and take my idle animation and paste it in here so now i can see if i'm actually playing this it is going to be playing an idol animation on my character here so now i do actually have a player idle state inside my character here so what i can also do is i can actually say you know what let's have a run animation as well so i can actually say create new clip and then i can go ahead and say you know what i'm going to create a player underscore run animation and then i can just go ahead and say you know what i do also have a run animation prepared beforehand so i can just take that and just paste it in and just you know play it and see how it looks like so now i have two different animation states that i can actually change between here you can also see if you go inside the animator which by the way can also find by going to window animation and then the animator is right there so if i were to actually select my new character in here you can see that i right now have two different states that i can actually do things to insert my animator now do keep in mind we're not going to be using the animator because we're going to do this the cool way which is using scripting so once you have this you're actually going to go and make a player controller script for your player so typically you would go inside your assets folder create a scripts folder and then create a player controller script that you would then add to your character inside your scene and that is basically just going to be this script that i have here where you just go in you grab the rigid body component off your player because you know you need to add physics to the player so it's moving around you're probably going to have a update that actually checks for input so if i currently press a certain key then make sure to update the input key and then inside fixed update you may be actually adding the run to your player by adding a add force to your rigid body so you know this is basic stuff i have many tutorials showing you how to actually add movement to a player inside a 2d game but you should just be having these basic basic movement for your for your player once you have that you're going to go and go to the top here and like i said we're going to create a small comment just to know exactly what this code does and you're just going to go and create a field for the animator a string for the current state you're going to create a constant and you're going to go ahead and call it with capitalized letters because this is a constant so that is the habit of doing that when it comes to constants so call is something like player underscore idle with capitalized letters and then set it equal and this is very important set this equal to the exact same name as you gave the animation state inside unity so you would go back inside unity and go into my animation you can see that i have one called player underscore run i did capitalize the p i capitalized to r if i'm going to play idle you can see i have the same thing player underscore idol with capitalized letters it's very important that you capitalize and call the exact same thing as these names inside your state machine here so doing that let's go ahead and go back inside our script and simply adding in the animations that we have so far what i'm going to do is i'm going to go down to the bottom and i'm going to create this animation change state method that is simply going to go in and actually check if a certain animation is already playing if it's not playing then play the new animation so i went and created a private void change animation state method i did also include a string inside the parameter because we need to pass in the new animation want to play so in this case it's going to be a string called new state and i just simply went in here and created a if statement that says if the new state that we're passing in meaning the new animation we want to play is equal to the current animation that is playing then just return this method without doing anything and again this is to prevent animations from being played again and again and again every single frame we're just going to hog up a lot of resources inside your game so if it's already playing the animation want it to be playing then we don't want it to be playing the same animation again the next frame so after doing this small check here just to see if we should not do anything we're going to go ahead and just apply the new animation to the current animation so we're going to grab the animator field and say we want to do dot play and then the new state that we're passing into this method here which is going to be the name of the new animation we want to play and then i want to update the current state field that we added inside our fields up there to be equal to the new state that we're playing inside our game so with this we now just simply update the current animation and don't do anything if we already have the same animation playing it is also important to note here that the animator should also be grabbed inside your start method so going up to the top here when we create the animator field you do need to go down inside your start method and say you want to set the animator equal to the current game object dot get component and then grab the animator component because you're not grabbing the animator component just simply by creating the field it's right now empty like there's nothing in it so you need to actually grab the animator by going inside your start method and then adding this line of code here with that said we do also have another method now this one is going to be checking if the current animation that is playing has finished playing and this is just a very useful method to have inside your game in some cases if you want an animation to finish playing before it starts updating the next animation for example like i said if i'm jumping then i don't want to cut off the jump and start the fall animation immediately after the first frame of jumping i want to finish the jumping animation and then play the falling animation so what i can do here is i can create a not a void type method but a boolean type method which means that we're going to be returning a true or false statement and i'm just going to call this one is animation playing now i should actually be capitalizing this that is actually a mistake on my end if you want to do things correctly and then inside the parameters here i want to include my animator which is going to be the animator that we actually grabbed inside our fields up here inside the start method and i want to include the animation state that i want to check for if it's already done playing inside the method i went ahead and created a if statement and inside the if statement we're just simply checking that the animator that we have grabbed inside our player what the current info is of that animation that is playing currently and if the name of that animation is equal to a certain name so in this case here the name that we passed in using the parameter up here after doing this i also want to check for a second thing inside this condition so i added the two ampersand symbols here and then i said the same thing i want to grab the animator i want to get the information of the current animation that is running and then i want to check how far along the actual animation is by simply checking if the time of the animation is currently less than one which means that it's not done playing yet so zero is going to be the beginning of the animation and then one is going to be the end of the animation then anything in between is going to be how far along the animation you actually currently are so just to do a quick recap here we're basically just checking if the current animation name is equal to the name that we're passing in and if it's still playing that's basically just what we're doing here and then returning it as true if it's still playing and then false if it's not still playing so doing this with these two methods here i now went up inside my script and again just to show the same thing we do actually need to update this because i did actually change my capitalization so going inside my fixed update here you can actually see that if i scroll down a little bit you can see that i currently have my running happening right here inside this line of code what you should be doing is simply going down below adding in the method that we created at the bottom there and just simply paste in what animation you want to be running so in this case here play underscore run because my player is running currently now if you want to check for a certain thing like i said if for example i'm currently jumping and i don't want to be running or doing the fall animation yet then you can add in a if statement we just simply check the second method that i created is animation playing and then i simply paste in the animator that i grabbed inside my fields and what animation i'm checking for so in this case it's going to be the player jump so if i'm currently not jumping inside my game then i'm simply running a if statement that checks if i'm grounded to the ground currently because if i'm grounded then i want to do the run animation and if i'm currently not grounded which means that i'm floating somewhere inside the air because i just jumped then i want to do the player fall animation of course after our jump animation is done playing so doing it this way you can actually add the different animations inside your code which also makes it a little bit more controllable because either way if you're using the animator you still need to add some code inside your player controller script to actually change the animation stage using the transitions inside your inside your animator so why not just do everything using a script why not just create this animation state machine that is really easy and quick to set up and now all you need to do is just add this line of code wherever you want certain animations to happen so with that said after learning how to do it this way i just started doing it this way every single time and i hope it makes it a lot easier for you too because i personally think this is a lot simpler than using the animator inside unity i think the animator is more of a a tool that new users of unity they start using just to get the hang of things and then once they get a little bit more into scripting then probably you want to do it this way instead that's my opinion at least you're welcome to have a different opinion i just think this is a lot easier so with that said i hope you enjoyed and i'll see you guys next time [Music] [Music] you
Info
Channel: Dani Krossing
Views: 21,770
Rating: undefined out of 5
Keywords: unity, learn unity, unity how to animate a character, how to animate using unity, how to animate using code, how to animate using c#, how to animate using code in unity, animate using code in unity, animate using script in unity, animation state machine in unity, animation state machine unity tutorial, easy animation using code in unity, animation using programming in unity, code animation instead in unity, unity code animation, EASY ANIMATE USING CODE IN UNITY
Id: 53Yx8C5s05c
Channel Id: undefined
Length: 13min 47sec (827 seconds)
Published: Wed Aug 24 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.