Mobile Player Setup (part 2)

Making our player Jump

Simon Truong
4 min readNov 14, 2021

--

Now that we have the basic movement for our player, it is time to set up the Jump mechanic. Within this article we will focus on setting up an Input for the space key to trigger the player jump along with an ray casting system to limit the amount of jumps and when to allow jumping.

Objective: Creating a Jumping system with an Ray Cast system

Scripting the Jump:

Within our Player script we are going to set up the required variables and input to create the jumping mechanisms. To begin we must understand what we must script, we need to create a float variable to house the jump force (aka the velocity for the Y value) to create the illusion to the player jumping. Then we need to tell Unity when the Jumping mechanism is able to toggle the jumping feature, which is set to when the player is “grounded”.

The two variables which we need to determine is the jump force which gives us the height of the jump and also the boolean which determines when the player can jump.

Then in the Update Method we are going to script the Jump system. Using the Input Get Key down ability we are able to state which button needs to be pressed before we can toggle an action. We are also going to add another condition which is, the player must be grounded before the jump mechanic can be called. Inside the statement we are going to take our current position and give it a new position with an alternated Y value (aka Jump force). The we also set the boolean of “isGrounded” back to false to limit the player to only one single jump.

RayCasting:

Now that we have our jump mechanic written it is time to work with the “isGrounded” boolean, we need to check whenever the player is grounded. Therefore we are going to cast a Ray (aka. Laser) that checks if it has collided with the ground layer. If that collision is true, when we are trigger the jump, if false then no jump can happen.

To cast a Ray 2D, we need to use the “RaycastHit2D” function. The script requires the vector origin (aka current position), the vector direction (aka, where the ray is directed), the Distance (aka, how long the ray is going to be), and finally the Int LayerMask (aka. the Layer that the Ray is checking for).

Then to visualize the Ray since by default the Ray is invisible, we can use the Debug Draw Ray to visualize the laser. Remember if you set the Distances of the Ray to be 0.4f then the Draw Ray distance should also follow the same length otherwise there will be some confusion.

Finally we are going to add the If statement which toggles the boolean switch “isGrounded” that is only toggled when the Ray Cast collider hits the ground layer.

Finally we need to create a new Layer Mask to allow the the Ray Cast to hit something. Back in Unity we are going to create a new layer and named it “Ground”. Then make sure that the Floor object selects that ground layer.

In the Player script we need identify the Ground Layer so the RayCast to target it. To do so, we can use the LayerMask variable with an Serialized Field which we can adjust within the Inspector.

In the Update Method, we need to add one more condition to the Ray Cast, which is the layer mask identification. Using the “LayerMask Variable name and value we are able to tell Unity to check what we have selected in the Inspector as the selected value.

Back in Unity, the player script should have updated the scripting component to contain a new dropdown menu. Select the Ground layer as the default layer for the Ray Casting as a target.

--

--

Simon Truong

A Designer, an Illustrator and a massive tech geek aspiring to become a professional Unity Developer.