Player Animations & Flip Sprite | Unity 2D Platformer Tutorial #4

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys more Blakey here and welcome back to another video today is episode 4 of our 2D platformer series and we're going to be covering animations making our player stand still run and jump with some visual bling I'm also going to cover our player looking in the direction it's going as always all scripts from this video are available on my patreon as well as all previous scripts and assets from this platformer series now let's get started so with these two Sprite sheets that we imported and set up in previous platformer videos we're going to be making three animations so let's go on to our player and then find the animation window and then I'm going to grab my project folder and Dot this on the right so we can access our animations and our animator and then let's make our first animation press create then in our assets let's make a new folder for animations and we can just call this player Idol and with this I'm going to find my two idle frames and I'm going to select both and then just drag them in here and we can just set these sample size to something like two and we have this nice simple up and down animation you may have way more frames in your animation feel free to adjust them however you want but because mine is incredibly simple I can have a very low sample size and just two frames so let's make a run so we do the same again create animation player run subscribe all of our run frames and drag them in and you can see if I scroll through these we have a nice little player run for this one let's set these sample size to something like 12 then we can press play and see what this looks like so I actually think that's going to be a little bit slow so let's try 24 and that should be a little bit better so with those two animations made let's grab our animator window and then let's select our player and you can see we have these two states here now our animator window is where we handle the transitions between our different animations when we created these animations on our player it automatically added this animated component as well as that it creates an animation controller which is what you're seeing right now so with these animations we can create transitions between the two and then we can Define parameters but we can control via code to decide when our player should switch from idle to run and vice versa so let's right click on our player idle press make transition and then drag it to player run and then right here we have this arrow and this is where we can decide how we go from idle to run so firstly we want to disable exit time and we want our transition duration to be zero because we want the transition from animation one to two to be instant and then it's looking for a condition so we create conditions by going to parameters at the top of our window and then we can press the plus sign and we're going to use a ball and we're just going to type in is running and then under our conditions we can add this is running bull and set it to true but now we need to do the same in the reverse order so let's right click player idle disable exit time transition duration to zero and then add is running but this time we're going to set it to Force so basically the only reason we will go from idle to run is if it's running is set to truth and we will go back to idle if his running is set to false so now we need to go into our player movement script and essentially reference this is running Boolean and change it as the player moves so I'm going to double click on my player movement script that we have been making throughout this series now one thing you may know is when we was handling a ground check we used two methods another one I'm using for the remainder of this tutorial series is the raycast method so you can see I don't have an on collision enter function as we no longer need to use that so without the way what we're going to do is at the top let's grab a reference to our animator components let's do private animator and just call this anime and then in our start function let's assign this anim to the component on our player so anime equals get component animator and then in our update function all we need to do we're going to do if our move floats so remember our move is assigned to our input of our left and right axes so if move is not equal to zero then we want to get a reference to our anime and then use set pull and then we're going to set the is running ball and then we use a comma and then we assign it to either true or false in this case it is true and then we just want to do if this isn't the case then we set an imp.set ball equal to false so basically if our move flow is less or bigger than zero then that means our player is either moving left or right so we want our player to run otherwise we want it to be playing the idle animation and you can see now while standing still our player is Idle but when we move we do activate our run animation now obviously there is the issue that our player is not flipping but we are going to fix this towards the end of the video once we have our jump animation out of the way so let's do that next so back on my animation tab let's select our player and create a new animation and I'm just going to call this player jump so as you can see I have a set of Sprites for my jump animation but I don't actually want to use all of them so I can just pick and choose the ones I want so in my case I want to use the first four so I'm going to select those and drag them in and you can see as I scroll through these it's a nice little jump animation so I'm going to configure this a little bit so it suits my current jump so I'm going to set the sample size to 24 and then I'm going to drag some of these Sprites a little bit later on so now when I play my animation you can see my player starts off on its back foot and then has some hang time and then on its way down it starts to prepare to learn and with its forward foot but for the last frame I want it to hover on this Sprite right here and this is where it differs from our current animations because with our current animations we want them to loop as our player runs whereas we don't actually want that for our jump animation so in our project tab I'm going to go to my animations folder go to my player jump and just disable Loop time so now we need to configure this in our animator tab so with our player selected let's go to our animator and you can see we have this new player jump so let's rearrange these just to make it a little bit neater looking so now we want to make transitions from our idle and I'll run to our player jump so now we could use any state for this so regardless of the state we're in we go to our jump when the player is in the air but I'm just going to do it from R2 animations if you had more animations and there was many different states for your player using this any state node may be a better option for you so just right click on our idle go to mate transition and now we need another condition here so let's go to our parameters do another Boolean and just do is jumping and then we can disable exit time remove transition duration and then all we need to do is add one condition that bin is jumping and we don't add in its running condition because quite frankly we don't care if our player is running or not if our player jumps we want our player to jump but on the other and when we're going back to our idle to decide what animation our jump could go to that is where I is running ball comes back into play so let's remove exit time no transition duration and then two conditions one being is jumping sets are false and the second one being is running sets Force but now we need to replicate this on the other side foreign except when we're going back to play a run we need is running set to true and is jumping sets are false and now let's configure this in our script so now what we can do we can actually utilize our is grounded ball here and access our is jumping Boolean in one line of code so we can just do an in dot set ball is jumping and we can just set this Boolean equal to the opposite of whatever R is grounded ball is if we want is jumping set to true and we set it to just is grounded that means jumping is set to true because the player is grounded and that doesn't make sense so we want it to be the opposite of whatever this value is and now if we test this out you can see I'm currently idling when I stand still when I move I'll run animation plays and when I jump I'll jump animation plays and these will blend together very nicely and of course it's important to remember these animations are rough and the actual game feel for it can be configured to your game and should be so now the last prominent issue we have is the fact that we are running backwards and more that is impressive that's not quite what we want for this game so let's go into our script and fix it so the first one we're going to do is create a brand new Boolean so we're going to do private full is facing right as we want our script to know what way our player is facing so when we want it to flip it can assign the correct direction then down here we're going to create a new function we're going to do public void and call this flip and firstly we're going to set is facing right equal to the opposite of that we're essentially going to get the transform of our player and flip it on the x axis so let's get a reference to the local scale of our player so let's do Vector 3 local scale and we can set this new Vector 3 equal to transform dot local scale and we do this because we only want to access the x value we cannot access this directly so we have to make a separate variable for this then we can just do local scale dot X and then we multiply this by minus one that's because 1 times -1 is minus one but minus one times minus one is one so we will always be flipping our player regardless of the direction it's facing and then finally we actually need to set our transform dot local scale equal to local scale but this isn't enough we actually need to trigger this function in our update so what we're going to do we're going to check if the player is not facing right and our move float is bigger than zero then we're we want our player to flip so basically if our player is looking left but he's moving right let's flip our player and alternatively let's do else if the player is facing right and on move flow is smaller than zero meaning our player is moving left let's flip again and now the last thing to do to run this off in our start function because our player Sprite is facing right we need to initially set his facing right equal to True before our player is given any input so with that done you can see when I move right our player is already looking right so it will not use the flip function but now our player is looking right and we move left our player is flipped and we can do this in the air we can do this when idling we can do this one running and it works all very nicely and smoothly so there you go that is our three animation States done as well as a flip function everything you need for an animation tutorial don't forget all of the assets and the script from this video is available from my patreon speaking of which thank you very much to my patreon supporters that you can see on screen right now make sure to stay tuned for the next episode and more of this tutorial series as well as many others on this channel and lastly I just released my first main Channel video in over nine months so feel free to check that out as it took an absolute ton of time but apart from that oh thank you all very much for watching and I will see you all in the next one bye foreign
Info
Channel: MoreBBlakeyyy
Views: 4,736
Rating: undefined out of 5
Keywords: unity, unity 2d animation, unity 2d, unity animation, unity animation 2d, unity 2d tutorial, unity animation tutorial, 2d animation, animation, unity tutorial, 2d animation unity, unity 2d animation sprite sheet, unity 2d animation tutorial, unity animator, unity run animation, unity jump animation, unity animations, unity 2d animation sprite, unity 2d character animation, 2d character animation unity, unity 2d animation sprite swap
Id: ux80fiJshsc
Channel Id: undefined
Length: 8min 45sec (525 seconds)
Published: Tue Jun 13 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.