Mobile Player Setup (part 4)

Player Jump script optimization

Simon Truong
3 min readNov 16, 2021

--

So far within the first 3 parts of creating the player jump mechanism we have been using a very complex format for scripting and isn’t geared towards optimization. In this article we will explore a more simple method which cleans up the script.

Objective: create a more organized and optimized script for the Jump function.

Script Organization:

Since we are going to rewrite the jump script so we have to get rid of the current jump script. Since we are going to be using an “return” method the boolean switch method isn’t going to be viable so we are going delete the “isGrounded” boolean.

Next we are going to reorganized the script layout. It is always in best practice to create individual methods instead of cluttering the Update method. Create a new method called “Movement” and copy all scripts that involve the movement of the player into this new method, then delete the old script from the Update method. Then you are going to call the custom method in the Update method. It will look more clean and easier to read for others who might be reading your script.

We will do the same for the RayCast ground check. But instead of the void method we will be using the “Bool” method since this is where the return function is used.

Setting up the Return Function:

As the example shown above, we are going to use the bool method to control the return function which allows a more simpler script. Starting with the Input command we are going change the conditions by adding the bool return and making that equal to true.

As you can see, the Is Grounded bool is now being called as a condition from the Input function.

In the bool method we are still going to check for collision with the Raycast but we are going to add another If statement which checks if the Boolean for the reset Jump is false, and if it is false then we are going to set the Boolean method to true. This will make the player only jump once and only if the player has been grounded.

Then we are going to add the reset Jump bool to true in the coroutine.

Now back in Unity, you will see that the player still jump even if the script as been simplified.

--

--

Simon Truong

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