Learn Double Jump in Unity

Physics Based Character Controller part 4

In this article we will add the ability to double jump on our character controller.

Double Jump Script

A new variable to keep track if we already double jumped or not.

private bool _doubleJumped;

When we are on the ground we reset the check if we already double jumped or not.
When we are not grounded we check if the space key is pressed and if we haven’t double jumped yet.
Then we add our jump height to our cached y velocity and set doubleJumped variable to true.

void Update()
{
Vector3 direction = new Vector3(Input.GetAxis(“Horizontal”), 0);
Vector3 velocity = direction * _speed;
if (_controller.isGrounded == true)
{
_doubleJumped = false;
if (Input.GetKeyDown(KeyCode.Space))
{
_yVelocity = _jumpHeight;
}
}
else
{
//check for double jump
if (Input.GetKeyDown(KeyCode.Space) && _doubleJumped == false)
{
_yVelocity += _jumpHeight;
_doubleJumped = true;
}

_yVelocity -= _gravity;
}
velocity.y = _yVelocity;
_controller.Move(velocity * Time.deltaTime);
}

--

--

--

Aspiring developer that’s self-learning Unity & C# to transition to a career with Unity. I got a passion for creating interactive experiences.

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

Recommended from Medium

Bubble Sort — Explanation & Code in C++

OTT Content Delivery– CDN Alternatives

Planning Google Cloud Platform (GCP) Compute Resources

PXN — Building alongside us

How to win in Among Us as crewmate

Xcode UI Tests for SalesForce, Facebook and Twitter login screens in WKWebView

Docker and Kubernetes

Using Phoenix Presence

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
Tristan Engel

Tristan Engel

Aspiring developer that’s self-learning Unity & C# to transition to a career with Unity. I got a passion for creating interactive experiences.

More from Medium

How to stick decals on the Spatial Mesh of Hololens2

Understanding Big O

Learn to code by making a 2D space shooter game.

What is Filebase and How do you use it?