Nerd For Tech
Published in

Nerd For Tech

In 6 steps, Add a double jump in Unity

The exercise here demonstrates implementing jumping mechanism as part of the customized physics controller in Unity.

The jump task closely resembles to what gravity does in this project. However, the y direction gets a positive value instead of the negative one.

By following these steps, one jump could be written easily.

  1. Create a private variable to keep track of the last y value of the velocity vector.
private float _CacheYVelocity;

2. Create a private variable to add the jump height of choice.

private int _jumpHeight = 50;

3. In the if statement which checks for the player objects’ position on ground, add the following code that captures space input for jump.

if (_characterController.isGrounded == true)
{
if (Input.GetKeyDown(KeyCode.Space))
{
_CacheYVelocity = _jumpHeight;
}
}

So far, one jump seems in place.

To implement double jump, another bool variable is required to know if the player has already jumped once.

4. Create a private variable.

private bool _jumped;

5. In the if statement above, turn this flag to True.

if (_characterController.isGrounded == true)
{
if (Input.GetKeyDown(KeyCode.Space))
{
_jumped = true;
_CacheYVelocity = _jumpHeight;
}

}

6. In the else part where the player gets checked for being in the air, recheck for the space input and add jump height as per choice.

else
{

if (Input.GetKeyDown(KeyCode.Space))
{
if (_jumped == true)
{
_CacheYVelocity += _jumpHeight + 2; // MyConfig
_jumped = false;
}
}


_CacheYVelocity -= _gravity;
}

That should be it :)

Here is the final result.

Thank you very much

--

--

--

NFT is an Educational Media House. Our mission is to bring the invaluable knowledge and experiences of experts from all over the world to the novice. To know more about us, visit https://www.nerdfortech.org/.

Recommended from Medium

Developer’s Path To Greatness

AWS DevOps — Part 2 Auditing and Messaging

DevOpsLink: DevOps Technology Watch, the Right Way

Choose The Right Job in Data: 5 Signs To Look For In An Engineering Culture

LeetCode | Solution | 20. Valid Parentheses

Instrumenting code in C# for observability

Graphs and metrics showing an example of what we’re trying to achieve

LOOK Holder Airdrop | Open Source DXM 1155 NFTs

Top 20 Interview Questions for c# Programming for SDET

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
Samarth Dhroov

Samarth Dhroov

Work in Progress.

More from Medium

Cinemachine: Unique Types of Body Settings in Virtual Cameras

What layout works for me in Unity

Scene Loading — Restarting our game

My journey becoming a Unity game developer: Make a game look beautiful-Creating Columns pt.2