[Unity2D] Jumping Animation

Step 1
In this lesson we are going to learn how to make a jumping animation to our player!
Step 2
Let's go ahead and create our player jump anim file. We only need a jump.anim file, delete the other things.
Step 3
Open up the Player's Animator and add a new state called "Jump".
Step 4
Select your Jump State and we want the jump.anim animation to be here. So select that.
Step 5
Next we want to create a Trigger parameter called "jump" (lower case "j").
Step 6
Go ahead and make transitions from Jump to Idle and Run, and vice versa.
Step 7
For each of these transition lines, Remove Exit Time, Remove Fixed Duration and Set Transition Duration to 0.

Do this to all 4 lines.

Step 8
What is a Trigger?

A Trigger is just like a boolean where it can be true or false. Except it automatically changes on its own.

When you press a button, the transition is working, so then the Trigger is True, but when you let go of the button, the transition is not working, so then the Trigger is false.

Step 9
Add the jump trigger to the conditions for the arrows pointing to jump.
Step 10
Before we start coding the jumping, think about how do we know if the bottom of the player is touching the ground?
Step 11
We need a way to detect if our player's bottom side is touching the floor, let's add this to our code. Then save it.
Step 12
In Unity let's create 3 empty objects on our player, name them GroundPoint1, GroundPoint2, GroundPoint3
Step 13
Let's Reposition those 3 points so that it matches this diagram.
Step 14
Now select your player and look at the player's inspector, you will see a down arrow for Ground points, set size to 3.
Step 15
Click and drag the 3 ground points to the empty element slots.
Step 16
Let's go back to our Player.cs script and create a new function called IsGrounded().

There is a red line underlinging "IsGrounded()", this is Okay, since it needs a return type of a type bool. Meaning it has to say return false or return true somewhere. Don't worry we'll add this in later.

Step 17
Let's plan this out. So when is our player touching the floor what is its Y velocity?
Step 18
Let's add in this code to our IsGrounded() function. What is our Y velocity when our player is grounded?
Step 19
So now when we know when our player's Y velocity is grounded. We want to do something with the groundPoints.

Add in this foreach loop, this will iterate or loop all of our groundpoints so we have access to all of them.

Step 20
Next we will add this new code function to our foreach() functionPhysics2D.OverlapCircleAll, this will edit our points.
Step 21
Inside our OverlapCircleAll() parameter space (inside the parentheses), we will have 3 things.

We have....

  1. Our points from the ground points we made
  2. The radius for each point.
  3. The layer these points will be on. (Should be just default)
Step 22
We'll need two new global variables (variables on top) for the radius and groundLayer, add this code.
Step 23
Now we can add in the 3 required parameters. Add this to your code.
Step 24
Now that we have a colliders array, we need to loop this array with a for loop, so let's add this for loop.
Step 25
We want to know if our colliders are touching a gameObject or not.
Step 26
In the for loop, add this if statement. We want know if the collider's gameObject is null or a GameObject

If our Collider's GameObject is not a gameObject then we would set this to true.
If our Collider's GameObject is a gameObject then we would set this to false.

This is to detect if our collider is touching something or not.

Step 27
Just to make things simple, since IsGrounded() the function is returning true or false, let's store it.

Let's add a new global bool variable called isGrounded on the top of our code.

Step 28
In the FixedUpdate() function, put this code right after our horizontal variable, so we'll always check if it's grounded
Step 29
Let's make an inputs() function to handle all of our other keyboard button inputs. Add this code to your script.

Whenever we press the [SpaceBar] button, jumping becomes true.


Don't forget to Call our Inputs() function in our Update() function!

Step 30
With jumping becoming true, we need to reset it back to false after every loop so add this code to your script.
Step 31
When we press the spaceBar, we want our player to jump with a jumpHeight, so let's make a jumpHeight variable.
Step 32
Now in our Move() function, we want to check if we're grounded and pressing jump, then we jump. Add this code.
  1. If we are grounded and if we press the jump button then...
  2. We are not grounded anymore, so isGrounded becomes false.
  3. Then we want to add force to our player to jump upwards with jumpHeight.
  4. Then we play our player's jump animation by setting the "jump" parameter variable in our player's Animator.
Step 33
Lastly, let's finish our IsGrounded() function. We want to reset our player's "jump" animation if we are grounded.
Step 34
Time to test this! Go back to Unity, Set our player.cs script's values to this and press Play, our player should jump.
Check Your Work
Step 35
Our player should jump but... The animation doesn't seem to look right.
Step 36
Let's take a closer look at our Player... We'll see that when the jump animation ends, we go back to idle animation.

We will fix this on the next lesson on Falling Animation!

Step 37
You are finished with this lesson! Click on submit and continue onto the next lesson!
[Unity2D] Jumping Animation Info


MVCode Clubs

Created By

Nepgear tina (instructor)


2D Platformer Game

Access Level


For Teachers and Schools

Teach coding to your students with MVCode Teach

MVCode offers an integrated product designed to teach coding to students.

Learn more about MVCode Teach