Day 115: Jump and Animation Part 1

Objective: Set up Jump logic and add Animation.

We will start with moving our player up when we hit the space key. This deals with movement so therefore we will have our jump logic in our Movement. The first thing we will do is to check to see if the space key is hit.

After we check for input, we will add a force to jump the player. Before we do that, we need a global variable to control the amount of jump force.

Back to our movement, we will set our velocity on our y axis to equal the jump force.

Now we can jump! But we can jump forever.

Let’s fix the never ending jumping. To do this, we will look into raycasting. We will be using: https://docs.unity3d.com/ScriptReference/Physics2D.Raycast.html , to get guidance.

We will use a return type method that will return a true or false value of the raycast. We will create a method with a return type of a bool. We will name it IsGrounded.

Inside here we will create a local RaycastHit2D named hitInfo. We will assign a Physics2D.RayCast(); Raycast has 8 different overloads that we can pick from. To start we will use the origin and direction overload. For origin will be the transform.position of the player. We will use Vector2.down.

We just return the hitInfo.collider.

Now all we need to do is when we check for input, we will check to see if IsGrounded is returned true or false.

Even with the raycasting, we are still jumping forever.

We can use debug.log to see what we are hitting by using hitInfo.collider.name.

We can see in the console that we are hitting the player.

To see the raycast in action we will be using Debug.DrawRay. It will take in an origin, direction and color. The direction will go in the direction given by 1 meter. To adjust the length we will create a variable that can be adjusted in the inspector.

Now when we are in play mode, we can see a green line leaving the player going down.

It shows that the raycasting is hitting the ground but with the debug we know it’s hitting the player first. Let’s use layermask to fix this. Let’s create a Layermask variable.

Now in unity we will go to our ground and add the layer of ground to it.

On our player we will set the player mask to ground.

Back in our script we will adjust our Raycast method to take in a layer mask. To add the layer mask, we will also need a distance. We will take the distance that we created for the debug and use it here as well.

The layer mask will ignore everything but what is set on the player.

Now we can jump only once till we hit the ground!

We will continue the animation tomorrow! Thank you for reading and have a wonderful day!

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Tyler Smallwood

Tyler Smallwood

I am passonate on learning to program and use Unity to become a skillful Unity Developer