2D ANIMATION RIGGING IN UNITY 🎮 | PNG & PSB FILES | Bone Tool Sprite Rigging | Unity Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
today i'm going to show you how to animate your 2d characters inside unity since it is kind of a i don't know kind of a boring thing to have a character running around and not actually animating while it's running now before we get started i want to mention that i'm going to be explaining how to do this using both a png image as well as when it comes to a psb file which is a photoshop file the process is slightly different along the way there's a lot of material online showing how to do this using a psb file but there's not as much when it comes to showing how to do it using a png file now because i want my tutorial to be available to everyone even the people who don't have photoshop we will be focusing on doing this using a png file but like i said i will explain when you need to do something different if you're using a psb file instead first thing first we do need to make a small change inside our character that we created last episode you don't have to use this character if you have your own just go ahead and use your own character but for my tutorial i'll be using this character here that we created together in the previous video before we import this image into unity i do want to do one thing which is probably not that big of a deal but i just want to make sure we don't have any external clippings going on so with the legs i want to make sure they don't clip inside the body and the way you can check that is by clicking the body and then you can see we have this square if the legs go inside of it or any other body part just go and drag the legs away because we don't want anything clipping on top of each other so just kind of move them out of the way and then re-export it as a png image so we can use it inside unity if you're using a psb file instead of a png image this doesn't matter you can have these underneath each other and everything and it's still gonna work perfectly inside unity but again if you're working with png images which is something that i'm quite new at because i usually do psp files as well make sure they're not clipping just to make sure so after we export it i want to go into unity and i'm just going to go and take my image and i'm going to drag it inside inside my assets folder here now typically we do have a sprites folder as well so i'm going to right click create and i'm going to create a new folder and i'm going to call this one sprites i'm just going to go ahead and take my player sprite and move it inside that folder now if you follow up my series here you will already have a player character where we have this little square that can move around and jump and do all this fancy stuff but you don't have to have this if you want to have just a completely new player character just go ahead and go into the sprite and drag it into your scene and just call it something like player and then you will be able to follow along just fine now i will be using our existing player characters so what i want to do is i want to click my player i want to go over to where it says sprites i'm going to just make sure we select the sprite that we just imported so i'm going to find it inside my project here it's right here double click it and then i'm just going to make the you know undo some of the adjustments we did in the running episode you know before the previous one so i'm going to make sure it's not red but just white because then we don't have any weird overlays going i'm also going to make sure that my scale is set to one and one and now the character is quite big i made it big inside photoshop so what i can do is i can actually scale it down and i'm not going to be using the scale tool up here instead i'm going to go into my sprite down here in the assets folder and i'm just going to go ahead and set pixels per unit to 200 and by increasing the number it is going to lessen the amount the size of the character and for now because we already included a rigid body and a couple of colliders to this character in the previous episode we don't need to touch this for now because we're going to be changing the sprite we're going to put the legs inside the sprite we're going to put the eyes on top of it so if you were to do any sort of collider scaling right now it's probably not going to match up once we're done so for now let's just don't touch the colliders and let's change a little bit later so now that we have our player inside the scene what i'm going to do is i'm going to click the player and what i want to do is i want to make sure we have everything set up so we can animate this guy right now the limbs are not inside the character because that's what it needs to be you know for it to look properly we also want to make sure we add bones and weight to this character which basically means that you know if you take like a human body i have bones and in between the bones i have joints and the reason i can move the way i can and the reason that my hand isn't just falling to the ground is because it's attached to a couple of limbs so that is what we need to create inside unity and unity has a very good built-in feature to allow this to happen you do want to double check though that inside package manager you make sure you do have a couple of things make sure everything is updated and especially make sure you have 2d animation in here because that is what is going to help us actually animate this character do also make sure that you see 2d sprite in here and if you're using a psp file you do also want to make sure that psd importer is in here and updated otherwise the psp file might not be possible to be used so after updating everything what i'm going to do is i'm going to go back to my player inside my assets window and i'm going to make sure that if i'm using a png image that it says single in such bright mode if you're using a psb file you want to make sure this one is set to multiple and it should just be done that by default like you shouldn't need to change anything but just double check that that is what it says and you haven't touched anything with all of this i'm going to go inside the sprite editor so i'm just going to click it here and i'm just going to go ahead and drag my sprite editor up to the top here just so we have a little bit more space to look at in here we're not going to do anything inside the actual sprite editor what we're going to do is we're going to click on the sprite editor tab up here in the left corner and i'm going to go down to skinning editor inside skidding editor is where we're going to apply the bones and the weight and the geometry and everything to our sprite so what i want to make sure i do here is i click visibility so we can actually see the hierarchy of everything that we're selecting and creating in here i want to make sure to start with that i'm going to be creating the bones for this sprite there's a couple of things here to note which is that right now we do have something like eyes and we have legs and you might think that hmm we probably need to take these and move them in before we start adding the bones but we actually don't get to collect and put everything together until we have added the bones and we're done inside the sprite editor so for now it needs to be separated and looks something like this what i'm going to do is i'm going to go over to the left where it says create bone going to click it and right now we can't actually create any bones and that's you know you can see if i'm clicking nothing is happening that's because you need to double click on your character for it to highlight and actually tell that oh right there's a couple of different sprite parts here that we need to do something with now i am making kind of a weird light bulb character so if you have a person or something of course this is going to be a little bit more complex than what i'm making here but it's the same general principle if you have arms as well then you need to do the same thing as we're doing to the legs so it's all the same so what i'll do is first of all i want to create the hip or the base of the body so i'm going to click at the bottom here at the center and i'm going to drag it up to around here and then i'm going to click it and then you can tell we can actually continue on building bones for this character so right now we have the body or the the hip part of the the person the light bulb we could call it and i'm going to create the head for the next part so i'm just going to go all the way up click it again and now i actually want to start with these body parts out here but as you can tell we're still continuing so i'll just right click to deselect and then i want to say okay the legs which are going to be the next thing i'm going to attach has to be connected to the hip so going to click on the hip bone and then you can see it actually continues it and then i can go ahead and click inside the sprite make sure you click inside of them not outside otherwise this is going to be a little bit messed up and then we're going to create the knee and then the lower part of the foot and then the entire foot the lower part of the leg it's called not lower part of the foot uh what we can do now is we can do the other leg so i'm going to right click to deselect and then i'm just going to continue on the other side and then i want to select the head and then i want to attach the eyes and this is going to be a little bit weird for some people because you would think that i just need to click it once and that's it then i can deselect but that's not how it works you actually need to go to the left side of the eye drag across click it and deselect do the same thing for the other eye same thing for the eyebrows so we're going to click drag across deselect so now we basically have the entire character rigged and of course if you do have more body parts you just need to keep attaching like i did it really depends on you know when i move the head you'll want the eyes to move with the head that is kind of like the logic you need to think about when you need to know okay should i attach the eyes to the head or to the to the waist because if you're not entirely sure then that is you know usually just need to think about it logically and then you can sort of figure things out the next thing we're going to do is first of all we're going to go over to auto geometry because now we're actually done with the bones there's of course a couple of other tools here where you can edit the bones and do such things for now i think this is quite good so what i'll do is i'll go to the geometry which is going to be where we actually create the borders the edges of the sprite and how complex it needs to be with the sprite you know when we twist it and turn it so if i were to go in and go down here to where it says generate four selected i can go ahead and make sure to have weight ticked on because that is something if i were to take it off we could actually add separate down here but for now usually the automated generation just does a pretty good job so i'm going to keep this on and then i can sort of select how complex i want to have my character if your character is too simple when it comes to like how you twist it and the body just doesn't follow exactly like you want it to then maybe you want to add a little bit more detail to the geometry in order for it to uh you know have a little bit more complexity but do you know that if you just you know buff this up to max with everything all the different characters inside your game it does either way of performance a general rule of thumb is just to add just enough for you to like be happy about it so what i'll do is i'll just go ahead and set the default generate for selected and then you can see we get this little color grading um thing going on here it's important to note the difference because the white lines that you see around the character and inside the character that is actually the geometry that we added the coloring is going to be the weight of the character so if i were to take and rotate my head how much weight should the the head have compared to the body because if i say that the body should not have as much weight on it the body is going to start twisting much faster with the head if i start tilling the head so you know the weight here is usually pretty good by default but you can adjust it by the weight slider inside the weight settings here if you wanted to so if i were to click it make sure we have the body selected i can decrease the weight and then you'll notice that when i start twisting my upper my head the body is a little bit more giving when it comes to twisting it so i don't want to have that like i said the default one is usually pretty good if you rotated and started doing things to test out your character because that is a good idea to do to make sure it looks right and you want to you know reset the entire pose then there is a button up here at the top you can click it and then it just resets everything so you shouldn't worry about it looking weird if you start twisting it and oh no did i put everything back to where it used to be just go ahead and click reset post and that fixes things so now if i want to edit my bordering because as you can tell i actually have a little bit of kind of a weird thing going on here so what i can do is i can say edit geometry and then i can actually just move them up if i want to a little bit closer to the character like so and if you want to add more vertices you can just go ahead and click create vertices and then you can just add a little bit more complexity to it usually i just click create vertices because you can move around the vertices even without uh having you know the edit one selected so you can't just kind of do this and just twist it around and make it look really nice now if you're using a png image you're pretty much set when it comes to the bone influences that we can mess around with in here but if you're using a psb file and you imported your character from photoshop into unity with the legs underneath the body and the eyes on top of the face then you're going to have to do some changes in here to the bone influence so down here if you were to click it you can see that right now i don't really have anything selected so i could if this was a psb file double click on one of the bones now for me nothing is going to happen because i'm using a png file but for you there will be a pop-up down here with the bone influences where it's going to tell you if this particular bone is influencing other body parts this means that if i were to take the eye and start moving it you want to make sure that it doesn't mess with the face and doesn't start stretching the face so with the eye double clicked you want to go down and click minus when you select the face you know minus off the face minus off the hip depending on you know what it has in there you only want the eye to be affecting the eye but like i said for me nothing happens when i double click because i'm using a png image and my eyes were not on top of the character when i imported inside unity so everything is fine for me you don't want to deselect anything for the face though because if it were to click the face or if you're using a psp file double click it then you do want to when you start rotating the body you want the eyes and eyebrows to start moving up and down you know like it's doing right now because when we start animating i do want the eyes to follow the body when we start animating him so what i'll do here is i'm just going to reset the pose and there's one last thing that we need to do which is the whole reason why we click visibility up here because now we can actually see the different names for the parts you know the name for the bones we can see the depth for the bones which is going to matter as well when we start to put together the character the lower the number the further back the part is going to be or the sprite part is going to be so the legs i want to be underneath the body right so the legs are going to be the lowest point of this sprite so they should just stay at zero um i want to just go ahead and you know click the leg oh that's the one called bone three i'm going to click it make sure we're not inside the bone influencer so just go ahead and click preview pose and then click it so we can actually edit it down here and here we go so with this as you can tell i made sure the base and the head were in the same layer so i set them to five i took the eyes and the eyebrows and i made them on a layer eight i also made sure to take the legs and put them on zero basically everything with the leg all the way down to the foot everything was set to zero and i didn't name the middle part because we don't need to name it or do anything to it we just need to make sure that the leg and the foot are named because that is all we need to have when it comes to the next couple of steps so for now that's all i named so with all of this i can actually go ahead and click apply and once we applied it i can go back into the scene and now we need to start putting together the character and once we've done that we can then you know make changes to the collider afterwards so what i'll do is i'll go ahead and make sure my player is selected and then i want to add a component and this one is not going to be something you want to add if you're using a psb file to a photoshop file because if you're using a psp file once you applied the different bones inside the sprite editor you're going to notice that your player is going to have a drop down arrow with a bunch of body parts and those body parts are going to be set to the names of the bones that we gave it inside the sprite editor so you need to skip this step if you're using a psb file but if you're using a png file like i am you need to go ahead and go down inside your ad component and you want to make sure you add a sprite skin and with that you need to make sure you click on create bones so once you do that you can see that all the bones that we added to it inside the sprite editor are going to be applied to our sprite so i'm just going to go ahead and zoom in here and what we need to do is we need to start moving these around so it needs to look like how we want it to look like inside our character so for example my leg just gonna go and take it i'm gonna move it in and take the other leg move it in as well the eye and we can actually if the camera is kind of in the way here we can go up and just sort of click the eye icon on the hierarchy window and just like unselect it so we can't see it like so just so we have something that you know looks like a character we can't go into the y-axis here and just copy it make sure that it is set to the same one oh not the y-axis the x-axis because they actually turned around when it comes to you know the the bones it's kind of weird x-axis is up and down and y-axis is left and right so it's opposite day so you want to make sure you have x-axis and exact x-axis the same so now that we put together the character we could go ahead and just sort of adjust the collider because we will need to animate this and have him walk around so i will click the player and we did already add colitis in the previous video so you can just if you have your own little sprite and you didn't follow the previous videos just go ahead and add a collider and you know change it so it matches with what you want it to be and then i also want to make sure that we take the box collider which is the one that checks if we can jump and we want to make sure we move that down as well there we go so now we have all the colliders like they're supposed to be so with the collider suggested the last thing we need to do in order to animate this character is that we need to add another component and this one you do need to add whether you're choosing a png file or a psb file so this has to be for both of them so i'm going to go down add components search for ik manager 2d which is going to be the component that actually takes care of having these bones work together so what i'll do is i'll go ahead and say i want to add a new to my list down here because right now as you can see the list is empty so i'll click add and i'll add a limb once we have that added i can go ahead and change the name of it so right now you can see we have this new uh game object up here called new lim solver 2d so i'm just going to go ahead and call it something like leg underscore right underscore limp solver 2d i can then click it and make sure we take the foot of my right leg so i'm just going to go ahead and to where we have the right foot which is up here i'm just going to go ahead and drag that into the effector that i have inside this game object components so with that dragged in there what i also do want to make sure is that my game object the one that was just created is actually positioned the correct place so in order to see it properly we will need to go up and make sure we have the move tool selected so as you can see we now have these that we can drag around and i want to make sure it's moved down somewhere near the foot i'm just going to put it right underneath the tip of my toe oh we do need to make sure we do it on the right foot because this is the right foot we're dealing with not the left one so with it here i'm going to say create target and with that you can see it automatically creates a target for us so what i can do is i can actually go ahead and move this and you'll actually notice that we have some magic going on the entire leg is moving by itself so we can actually animate this foot in this manner and everything is going to be dragged and animated with it which is really nice and then we want to do the same thing for the left leg so i'm going to go up and choose my player go down to the component add a new ik manager 2d limb i'm just going to call this one leg left then i'm going to go and add in the left foot inside my effector i want to make sure it's moved down right underneath the left foot and then i just want to say create target now there is something here you do need to be aware of which is if your knee is going to opposite way like this do make sure you click the flip button up here because then it's going to bend the right way so with this we now have everything set up the way we want it to of course if you do have arms if you have other different body parts that you want to animate that you did also add bones to inside the sprite editor then of course you might want to include a couple more of these limbs but for now this is all we need for this particular character here so what we can do now is we can actually start animating hooray so i'm going to go ahead and click player and i want to make sure we have the animation window open so i'm going to go to window animation have it drag down here i'm also going to open up the animator and drag it down to the bottom here and inside the animation tab with the player selected by the way that is important you want to say create and then you want to create a new animation acid now i'm going to go back inside my main directory and i'm going to create a new folder and i'm going to call this one animations in here i do also want to make sure that we have a good hierarchy so i want to create a folder called player because this is going to contain all the player animations and then i just want to rename my animation to player and then the animation that i want to create now as a default usually start out with a idle animation so what i want to do is i want to say underscore idol again you can name it whatever you want to but it is just sort of a naming convention to say the name of the object underscore and then the animation that you want to create so idle is him just standing still it's not really doing anything so we're going to save and in here we can now start animating so what i'll do is i'll take my project window just put it over to the side here because it is kind of a neat thing to have now if you were to do frame by frame animation you usually would just go ahead and open up your sprite with all the different frames and just drag them all inside the timeline and then play it and then you had like an entire animation playing so you would you know pre-make all the different frames inside and editing software like photoshop or after effects but since we're animating directly inside unity and we need to do it in a slightly different way now inside the animation tab there's a couple of different ways we can do it we could go ahead and say we want to add properties and then you know add whatever property we want to add so if i want to get the positioning transform property of my body then go down to the base which is my hip as you can see we can actually see everything inside the player object inside the properties down here so if i wanted to i could go and say i wanted to add the entire transform of the body which is the the hip base and just go ahead and add it in here right click add and then we can actually adjust the settings inside what we have here um but i don't want to do it that way let's go and do this in a much simpler way which is simply just to click the bone and then start moving it around because that seems simpler in my mind so what i'll do is i'll click the record button and then what you can do is you can move it out to let's say 30 seconds and then you can just go ahead and take the body move it down slightly and then go over to maybe one minute copy the keyframes and paste them in if i were to play this using my space key is that we actually have an animation going and it's just basically recording the keyframes that we're adding in because we hit the record button but maybe i want the eyes to bounce as well so what i can do is i can go forward to around 40 seconds or 40 frames it's actually called and i can actually take the eye just move it down slightly so it actually bounces down when we you know move so something like this does that make sense does that make sense to anyone so what i'll do is i'll just go ahead and start doing this with the rest of the body as well so we have the eyebrow we have the other eye the other eyebrow and i'll just do everything like i just did here so i'll just go ahead and do that really quick and now i have something like this there's something else i want to do which i don't know i think could be kind of cool to do so what i'll do as well is i will i will i will i will i'm gonna go ahead and make sure the head bounces forward and backwards while he's doing this so what i can do is i can also go forward to where we have this little leg bounce and i want to make sure that i select my head which is right here and i want to rotate it so once we get over here let's make sure we choose the right one i do think it is the set axis so we want to bounce a little bit so once you start bouncing you can see the head bounces down and then we want to bounce back up again so now we have something look something like this now of course you can spend a lot more time on animating it i'm doing a tutorial so i'm just going to do some really rough sketching here or some rough animations but i think this is fine for now so what i want to do now is i want to add a running animation so what i can do is i can go down to where it says player idle we have this little drop down i'm going to click it and then i'm going to add a new clip so i'm going to save this one inside my player animations folder i'm going to call this one player underscore run and then we're going to do the exact same thing we're going to make a running animation so what i can do let's go ahead and do it the other way this time so what i'll do is i'll add property i'll go down to the base and i want to say that i want to add the entire transform of the base so i'm going to click it then i'm going to open up the head then i'm going to hold down control click transform for my head then i also want to open up the eyes and for this one i just want to have the height going i don't want to have the rotation or anything else so i'll open up the transform and say i want the positioning hold down control as well select it left eye position eyebrow swell position open up position and then we don't want to have the leg but we want to have the leg limb solver which is down here so i'm going to make sure we hold down control select the transform and then the transform for the other leg gonna right click add properties and now we have all these properties down here so now you can see we have keyframes going at zero frames and also at uh 60 frames over here so what i'm going to do is i'm going to go forward to 40 frames and i want to say that i want to add a little bit of bobbing again for the body so i just want to make sure i have my recording enabled there we go move them down slightly so now we have this little bounce going on from the beginning uh what i'll do as well is i'll add a little bit of rotation to it so once we start running we want to rotate so we start running and going up again now i do want to make sure that we also bounce backwards so i'm going to drag these frames over to 20 instead so once we get to 40 i actually want this to rotate the other way so with this i want to make sure that my legs are moving as well so i want to make sure i step up somewhere around here so we step up we stomp down step up stomp down and slide and at this sliding motion i probably want to copy the keyframe and i just want to go over and make sure that is the starting frame as well so we have a start and slides over then i want to do the other leg and if you know a little bit about animation which i know little about then i want to make sure that my other foot is actually sliding as he steps forward so i want to make sure that once we get to the step the other foot is sliding back so we step you know we start sliding we step and step steps step down i'm concentrating okay so it is hard to talk while i'm doing it just kind of like move the timeline and feel your way and it's gonna look right at some point and i think something like this just to begin with i messed around with it for about a minute or so and now i have something looks like this so we have a walking cycle i do also want to make sure i add the eyes and the eyebrows because they should bounce as well so let me just go ahead and fix that so something like this if you were to play it you can see the eyes also doing something and then the last thing is i want to make sure that the head is you know doing the bouncing thing that we did with the idle animation as well so something like this so if we were to play it again you can see we have a little bit of bounce going on which looks a little bit more organic i think and of course if you wanted to add a jump animation or anything else to this character you can just go ahead and go in and say you want to create a new clip and just call it player underscore jump and then you can add a jump animation so it is really pretty simple to do um so what i'll do is i'll go inside the animator and what you'll notice is that we need to make sure we have our player selected and inside the animator with the player selected we will now see that we have a player idle which is the first one that we created and then we have a player run and these are different animation states that we can actually change between depending on what we're doing inside the game now if you accidentally created a player run to begin with you can just right click on the other one and say you want to set it as a default layer state and then that one is going to be your idle animation so just know that you can change your mind if you change something if you made the running first so what i'm going to do is i'm going to make a transition because in order to switch from idle to run you know when we start moving we need to have a transition setup so i'll right click make transition move it over to run then i'll right click on run and transition it back to idle this is important because if we don't add a transition back to idle then once we stop running we're not going to go back to being idle again so that is something we need to add now before we start making changes to the transition i do want to go into parameters which is over here on the left and i want to add a new parameter this one is going to be a float because it's going to be a number that determines if we're actually running or if we're standing still so i'm going to say i want to create a float and call it something like speed or run or something you know something that indicates to us what this number is going to be so this one is just going to be the speed of the character and with that created i can go in and click on the little uh transition and inside my inspector i'm going to make sure that when i transition from one animation to another one that it looks correct no exit time because we want to make sure that we have a instantaneous change from idle to run we don't want the full animation to finish playing before he starts running so i want to make sure that has exit time is deselected i want to make sure the fixed duration is unchecked and i also want to have a transition duration set to zero and we want to do that for all the transitions so going to say the from run to idle unselect has exit time fixed duration set this one to zero once we've done that we do also want to make sure that the condition that we created down here which is the parameter is set up inside these transitions so i want to make sure that when i go from running to idle i need to check if my speed by clicking the little plus icon inside conditions is lesser than 0.1 and now the reason i'm writing 0.1 is because we want to have a margin of error so if we're not standing completely still according to the code but technically we are then i want to make sure it's the registers that we're standing still so 0.1 is better than 0 because you might end up not getting the transition working properly and the same thing is for the other transition so when we want to go from idle to run then we want to check if we have a parameter that is greater than 0.1 so the speed should be greater than that so what i can do is i can actually test this out so i can click play and once you start playing you can see that the idle animation is the default state that's playing i can go down inside my parameters and actually change this to one and then you can see you start running change it back to zero or nothing then we go back to idle and by doing this we can actually test that everything is working so with this i'm just gonna go ahead and start playing we're going to go inside our players player controller which is the script we have attached to our player and before we go into visual studio i just want to show something which is that if i were to click on the player you'll notice that we have a animator component which is something that we added once we actually created our first idle animation it automatically adds it to our player so we do have an animator component inside our player that is important to note because once we go inside visual studio we do need to make sure that we actually grab the animator component in order to you know change the animation so at the very top here i want to make sure that i set a private any may time i'm just going to go ahead and call this one something like animates then i want to make sure we actually grab the animator component because right now we just created a empty uh field so i want to make sure we go down inside our start function and i want to set animate equal to a game object dot get we can actually just copy paste why don't we just do that that is a little bit faster um and then i'm going to change it to the animator we want to grab once we have the animator we can actually change the state of our animation so what i'll do is i'll scroll down to fixed update and what i want to do is if i'm moving which right now is inside this if statement here then i'm just going to change the float parameter that we created inside our animator inside unity so what i can do is i can say i want to grab the animate that we just created up there dot set float which is a function that we get once we have the animator component parentheses semicolon and then i want to say what parameter are we trying to change here i want to change the one we called speed because that's the name we gave it and then i want to change the speed to whatever our move horizontal value is when we start moving the character because remember anytime we're clicking on a or d or left or right on the keyboard our horizontal value is going to go either to 1 if we're going right or minus 1 if we're going to the left and it's going to be 0 if we're standing still however there's going to be a problem here because right now inside unity inside the animator you can see that right now we're checking if our speed is greater than 0.1 or lesser than 0.1 so what happens if i go to the left with my character well right now move horizontal is actually going to become -1 so even though i'm moving to the left it's going to play the idle animation so what we can do is we can actually add in a function that will take our number and always make it into positive we don't want to have any negative numbers which means that if i'm moving to the left and i'm getting minus one it's just going to change it into one instead so i'm going to go ahead and say we want to use a function called math f dot apps parentheses and make sure that move horizontal is inside the parentheses like so now we do need to fix something here really quick because we don't want to have this inside our if statement that was a mistake on my end otherwise it will never check for idle because it's only going to check the animation once we're actually running so we want to make sure it's just inside either update or fixed update i'm just going to go and put it inside update because this is not a physics-based thing we're trying to move here so i'm just going to go and pass it inside paste it inside update going back inside unity we can actually go ahead and play see what happens and as you'll notice when i move left or right he starts running or walking or whatever we might call it but we do have quite a bit of a fast running speed so let's go back inside unity and just sort of you know damp it down a little bit so so we don't run so fast let's try and set it to to one for the move speed and then you'll notice that we have something that looks a bit more realistic of course you can speed up the animation if you want to so it matches you know if you want the character to go a bit faster than this but something else we're gonna have to fix that you might have noticed is the fact that right now we are not flipping the character if we're going the opposite direction so what i'm going to do to fix this and actually flip the character is i want to check if i'm going right because right now as a default my character is set to be looking right so that is fine but once i look to the left i want to make sure that we flip the character so inside my start here or actually inside my fields i want to create a new boolean which is going to be private which i know it is by default if you don't write anything in front of but it is just kind of neat to have it so write a private boolean and i want to set this one to facing right and i'm going to set it equal to true because like i said as a default we are going to be facing right when we start the game then i'm going to go all the way down to the bottom and i want to make sure i create a function that is going to flip my character if i'm looking to the left side so i'm going to say i have a void uh flip parentheses curly brackets and inside this flip method all i'm going to do is i'm going to say well whatever the character is right now so if i'm looking right when i call on this method i want to flip him to the other side so what i can do is i can take my facing right variable and say that i want to set it equal to the opposite of what it is right now so because facing right right now is true if i were to say exclamation mark facing right then it's going to be equal to the opposite which is going to be false next time i call on it and then what we simply need to do is to flip the character because right now we're just telling it that we want to flip it but we haven't actually done so so i'm going to create a vector 2. i'm going to call this one current scale and i'm going to set it equal to the current scaling of our player so i'm going to say transform dot local scale logo scale is just basically going to take the current scaling of our player so right now it's one and one on the x and y axis so what i want to do is i want to flip this one and we can simply do that by flipping the x axis and just to kind of show you what i mean if i were to go back inside unity if i take my player right now his scale is set to one if i were to write minus one is actually going to flip so what i'll do is inside my code is i'll say that we want to have a current scale dot x to grab the x axis i want to set it multiply equal to minus one and this is basically just me taking the current parameter which is x right now which is equal to one and then i want to multiply it by minus one which means that one times minus one is equal to minus one and then i want to set it equal to itself just like we did up here basically and then we just basically go in and say we want to transform the local scale of the current player into whatever the result is of current scale so we'll say transform to graph the player's transform component then we'll say dot local scale and set it equal to current scale and we now basically have a method that is going to flip the player so now all we need to do is we need to make sure we call on this one so inside my update i want to say if horizontal move or move horizontal is called is greater than zero and i am not currently facing right then i want to call on the flip method so i'm just going to copy it paste it in here and then i want to do the opposite for if i'm doing the other direction so i'm going to create a elsif statement so else if move is less than zero and i am facing right so now if i were to go back inside unity and were to play my game you can see that if i'm going to the right side just like before we're going the right direction but if we were to click on the left button we're now flipping the player and going the opposite direction and just like that we can animate a character inside unity and again if you want to do something about the the the speed of how he's running around you can go inside our animation and make sure you have the player selected and if you were to click on the run animation you can take everything by just making sure to select all the the top keyframes and then you can actually drag it down to be a little bit faster if you needed to so if you were to play this you can see he's running a little bit faster and if i were to go inside my code we can also change the speed of the player so you know he's actually a little bit faster inside the game point five i think should be better and i can see you know we can speed up the animation to work a little bit better we could also add a little bit more of linear drag you know in order to probably make this a little bit better like so you know adding a little bit extra drag is gonna help so basically this is how we can animate our characters and have them you know move while they're standing still and run when they're actually running so hope you enjoyed this video and i'll see you guys in the next one [Music] you
Info
Channel: Dani Krossing
Views: 79,292
Rating: undefined out of 5
Keywords: unity, learn unity, unity game development tutorial, how to create a 2d sprite in unity, create a sprite in photoshop, create a sprite to animate in unity, unity bone tools, unity 2d rigging animation, unity 2d rigging tutorial, unity 2d sprite rigging, 2D BONE RIG ANIMATION IN UNITY, Bone Tool Sprite Rigging, png rigging in unity, psb rigging in unity, Unity IK Manager 2D, Unity Skinning Editor, Unity create idle animation, Unity run animation, unity how to animate a character
Id: J1JcSp3lYiY
Channel Id: undefined
Length: 41min 32sec (2492 seconds)
Published: Fri Aug 20 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.