How We Implemented Jumping in Our Game

Otto Wretling
3 min readNov 5, 2021

--

… started from the bottom, now we’re here!

This article is about the development process of our first game. During the process, we are learning how to use Unity & Blender from scratch to create everything you see in the article, enjoy!

Introduction

When I was working on implementing vehicle mechanics, the player character was unable to get up the jetty next to the airship. To solve this problem I decided that it was time to implement jumping in the game.

Implementation

To start, I began by adding two float variables to the movement script;

private float jumpSpeed;
private float ySpeed;
  • jumpSpeed determines how high each jump will be.
  • ySpeedis used when the jump is activated and adds the correct upward force to the player character's movement and adds downward force after the jump is completed.

After creating the variables, I wrote a small function to contain the code for jumping, including the button prompt, and placed it in the Update function.

Lastly, I added the following line to the function that moves the player character so that the character jumps when I hit the space bar.

movementOutput.y = ySpeed;
Pretty good for a first try!

Fixing Bugs

Unfortunately, it was possible to jump multiple times in a row without landing, and in addition, the player was falling super-fast when going off the edge of the island.

I made the player too light *and* too heavy! 🤔

The first problem occurs because there is nothing in the code that stops the player from jumping as many times as they want, and the second problem is caused by the code constantly adding downwards velocity, which accumulates and makes the player fall faster and faster as time passes.

Both of these problems can be solved by using Unity's built-in function CharacterController.isGrounded to check if the player is on the ground before allowing jumping or adding downwards velocity;

No more levitation or supersonic falling!

You might be wondering why I set ySpeed to the negative value -0.5f instead of 0 on line 3 in the jump function; The reason is that the CharacterController fluctuates on its own if it is set to 0, as you can see in the GIF to the left. This might make double jumping and other bugs possible, so the easy choice is just to set it to -0.5f instead.

Final Adjustment

One small step, and one small jump for the player!

As a final touch, I increased the Step offset of the Character Controller to 0.6 instead of 0.3 so that the character could step onto the jetty, even without jumping.

Conclusion

There are some things left to implement and tweak when it comes to jumping, such as animation and jump height but that will come at a later stage.

Many thanks to Ketra Games for providing me with an excellent tutorial for how to implement jumping, please check out their tutorial and other content:

Lastly, thank you so much for reading!

--

--

Otto Wretling
Otto Wretling

Written by Otto Wretling

Writing about my podcast, game development, technology, language learning, and whatever else comes to my mind!