Mobile Player Setup (part 4)

Player Jump script optimization

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.

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

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

IndentationError: unindent does not match any outer indentation level

Kubernetes 1.18.0 is out: Now What?

Building a chat system with Flutter and Cloud Firestore

Using TestContainers for Spring Boot integration tests

Codat expands support for pushing data into on-premise accounting software — now including…

Discerning Hard Links and Soft Links in Linux

Setup your own TURN/STUN signal/relay Server on AWS EC2

Unit testing in Swift

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
Simon Truong

Simon Truong

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

More from Medium

Mobile Player Setup (part 5)

Add a custom template to Unity for new projects

2D Player Animation (part 3)

Install Unity’s new input system