[Unity2D] Jumping Animation

Planning
Step 1
In this lesson we are going to learn how to make a jumping animation to our player!
Ad377f89fea8e81b378f09c9f8e968f1
Step 2
Let's go ahead and create our player jump anim file. We only need a jump.anim file, delete the other things.
Temp1551041596
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").
Temp1551485608
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.
Temp1551043494

Do this to all 4 lines.

Planning
Step 8
What is a Trigger?
Temp1551043978

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.
Planning
Step 10
Before we start coding the jumping, think about how do we know if the bottom of the player is touching the ground?
Temp1551427843
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.
Temp1551487035
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.
Temp1551430452
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().
Temp1551481626

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.

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

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.
Temp1551484692
Planning
Step 21
Inside our OverlapCircleAll() parameter space (inside the parentheses), we will have 3 things.
Temp1551488198

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.
Temp1551489227
Step 23
Now we can add in the 3 required parameters. Add this to your code.
Temp1551490331
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.
Temp1551490659
Planning
Step 25
We want to know if our colliders are touching a gameObject or not.
Temp1551491830
Challenge
Step 26
In the for loop, add this if statement. We want know if the collider's gameObject is null or a GameObject
Temp1551492495

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.

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

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

Temp1551652612

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.
Temp1551647496
Step 31
When we press the spaceBar, we want our player to jump with a jumpHeight, so let's make a jumpHeight variable.
Temp1551648928
Step 32
Now in our Move() function, we want to check if we're grounded and pressing jump, then we jump. Add this code.
Temp1551649109
  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.
Temp1551652489
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.
Temp1551652865
Check Your Work
Step 35
Our player should jump but... The animation doesn't seem to look right.
Planning
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

Account

MVCode Clubs

Created By

Nepgear tina (instructor)

Course:

2D Platformer Game

Access Level

premium

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