Unity 2D Animation Tutorial - Idle Run Jump Animations And Transitions Between Them Using C# Script

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everyone in this video I will show you how to create a set of animations for your character they will be idle run and jump animations also we will create transitions between them and I will show you how to control them with simple c-sharp script let's create this project from the scratch so you could follow the steps and see how it's done here in art folder I have a set of character sprites which represent idle jump and drawn States also here I have a sprite sheet with some environment you can get all of these art assets by the link in the description ok I want to use grass image from this set now it's one solid picture so to take a grass image from it I need to slice it so with this sprite sheet selected I set sprite mode to multiple also as our sprite sheet is kind of pixel art I set its filter mode to point no filter so images will not look blurred hit apply to save the changes now I can slice it go to sprite editor and click slice drop down here automatic type will be okay so I can hit slice button it's hard to see here but now each image has its own white boundary that separates one image from another each sprite can be renamed I select my grass sprite and rename it as ground so I could easily find it in the inspector to save all of the changes I must hit apply now as you can see I have each sprite from spreadsheet separated from another and here is my ground sprite I put it into the scene it will be a ground to make this ground act as a ground let's add a box Collider to it and edit it a bit like so now our character will be able to work on it let's add a character take one of the sprite from idle state and drag and drop it into the scene rename this new game object as a character now let's set characters order a new layer option to 5 so it will be rendered in front of the ground also our character will need a box Collider 2d component to walk on the ground add one and edit it so it appears at feet level also the character is going to need a rigidbody 2d component so we can move it with the C sharp script I add one and set gravity scale to 3 so character will be falling down faster also I set collision detection to continues for more accurate detection and I freeze Z rotation so my character will not tilt left or right when moving now it's time to create some animations for our character select character open animation window and click create new animation I create a new folder for those animations open this folder up and save this new animation as idle animation now as its idle animation I select sprites from idle state and drag and drop them into idle animation timeline now if I hit play then you can see this idle animation running to slow it down let's set number of samples to 5 hit enter to save the change hit play there we go idle animation is done let's create new animation clip which will be run animation here it is now I select all of the sprites from run state and drag and drop them into timeline here is our run animation which is quite to fast let's slow it down 10 samples per second should be fine ok now let's create new animation clip which will be jumped up animation it will be playing when our character goes up in the air select one jump up sprite from jump state and drag and drop it into timeline as here is only one frame so let's set number of samples to one here is jump up animation create new animation clip save it as jump down animation drag and drop jump full sprite into timeline number of samples will be one as well here is our jump down animation ok now to be able to control these animations with the script we must set transitions between them and declare some conditions make sure the character is selected and open up animator window if you don't have this or any of the other windows then you can find it in window menu section here are our animations I rearranged them a bit ok first of all we need to create some parameters which we will refer in the script they will be three boolean parameters first one will be is running second one will be is jumping and third one will be is falling latter casing is important make sure these parameters are spelt the same way in animator and in the script otherwise transitions will not work now let's create transitions between these animations right-click on idle and make transition to run animation left click this white arrow and here in condition section create new condition so this transition takes place when is running is true also I uncheck has exit time option so run animation will not switch back to idle automatically when run animation is over and they set transition duration to zero so idle will switch to run immediately next transition will be from run to idle State select this transition condition will be one is running is false and check as exit time and set transition duration to zero next transition will be from idle to jump up state selected condition will be one is jumping is true no exit time transition duration is zero next transition is from jump up to jump down state condition will be when is falling is true I will set his exit time and durations later on next transition is from jump down to run it will take place when is falling is false next one is from idle to jump down which will occur when is falling is true next transition is from jump down to idle which will take place when is falling is false next one is from run to jump up state with a condition when is jumping is true looks like we are done with conditions now I make sure that all of the transitions have exit time option unchecked and transition duration set to zero here we go now to move our character and to control those transitions we use the script which I named character let's take a look at it first we have rigidbody and animator references to control character's movement and animations transitions then he removes speed and direction X variables that help to move a character facing right and local scale variables help to flip character sprite so it faces two words it goes in start method we get those rigidbody and animator components set local scale variable s current transform local scale and set move speed to five in update method we pass a value to the direction X variable depending on left or right arrow button is pressed x move speed so the character should move left or right here then we check if jump button is pressed and if it is and our character is not going up or down already then we can perform jump by passing a force to rigidbody in up Direction multiplied by some value equals to 700 in our case next several if statements are responsible for animations transitions first one if Direction X absolute value is greater than zero so character moves left or right and if it not in the air then we set is running parameter in animator to true and animation is switched to run state else if our character is standing still then is running is set to false and idle animation should run next if statement if rigid bodies vertical velocity equals to zero which means that our character is not going up or down then is jumping and is falling is set to false and animation is switched to running or to idle state depending on if character is running or not next if statement if rigidbody vertical velocity is greater than 0 which means that character goes up then is jumping is set to true and animation is switched to jump up state and if vertical velocity is less than 0 which means that character falls down then jumping is set to is falling is set to true ant animation is switched to jump down state that's all about transitions in fixedupdate we just pass a velocity to rigidbody depending on direction x value in late update method with leap a character depending on where it goes at the moment and where it currently is phase two that's the script drag-and-drop the script to our character that's it now we can hit play and see how it works our character can run jump and idle hope this tutorial was useful for you thank you for watching see you next time you
Info
Channel: Alexander Zotov
Views: 73,519
Rating: undefined out of 5
Keywords: alexander zotov, unity tutorial, Unity 2D Animation Tutorial - Idle Run Jump Animations And Transitions Between Them Using C# Script, unity animation tutorial, unity idle animation, unity run animation, unity jump animation, unity animation transition, unity animation transition script, unity animator, unity animation 2d, unity animation character, unity animation controller, unity animation script, unity animation trigger, unity animation, unity animation event
Id: BlalhxSTUWo
Channel Id: undefined
Length: 8min 40sec (520 seconds)
Published: Mon Oct 01 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.