2D Mobile Game: Player Jump Optimized!
In this article, I am going to cover how to clean up the previous jump code and make it more optimized using methods and returns.
Decomposition & Abstraction
We are going to look at the Update code and break it down into two smaller pieces; Movement & CheckGrounded. Then we will abstract those pieces and place them into methods to be used in the Update to clean up the mess.
Currently, we have all of the movement and jump code in the OnUpdate but it is good practice to keep the Update as clean as possible. So, we are going to break down the code and abstract the movement with jump and grounded into their own separate methods.
We created a method called Movement, which will store our movement and jump code .
Once the movement code has been abstracted from Update and put into the the Movement method, we simple just need to call the method in the Update to make use of it.
Lets do the same for the grounded check, by creating a method called CheckGrounded, and we will abstract the code from the Update and place it into the method.
Again, to make use of it we need to call CheckGrounded in Update. This will work the same as before, but the Update now is cleaned up.
Out with the Old, In with the New
We are going a step further and use to a Return Type Function or Method that will return True or False with the Ground Check, instead of using a global bool variable.
In the Movement method, we are going to change one thing. Instead of checking for the space bar pressed and the global _grounded variable is true, we will check the return of the IsGrounded method is true. You see the _grounded variable is commented out and will eventually be deleted.
IsGrounded is a bool return type method. This will return a true or false value depending on the raycast. We keep the RaycastHit2D line from the previous method. If the raycast is not null (player is touching the ground), and the player didn’t jump it will return a value or true. Anything else it will return false.
I just rename the ResetJumpNeeded variable and coroutine to ResetJump. This coroutine allows the program to breathe between frames, so the Raycast doesn’t detect the ground while jumping.
In the Movement method, when the player jumps we just need to set the _resetJump true to trigger the coroutine.
You can see the Update now has only one method being called now making it more efficient.
Update Bug Fix
The issue was when trying to add an Idle Jump animation, it would only play the Jump animation once.
The reason is we were only checking to see if the Player was touching the ground colliders through a raycast when the jump button was pressed and not constantly. This prevents keeps the Jump animation to true and never returns back to false. You do not see the Jump animation continually being played because Loop is off, so it will only play once.
The fix is simple. We will call the IsGrounded method, which is casting the raycast to determine if we are touching the ground, and put it into the Movement meth0d, which is being called in Update. Long story short, we are always casting the raycast to see if we are grounded.