How To Double JumpLearn How to do Multiple Jumps in Unity

Author Waldo
Published May 4, 2018

In this quick Unity tutorial I go over how to do a double jump, or X amount of jumps while in the air. This is an extension to our previous video which explains how to detect if we are grounded, how to animate our jump and then how to move our sprite with an upwards force.

Video Walkthrough

  • 1:38 - modify our jump script to allow double jumps
  • 3:00 - zero out our vertical velocity
  • 4:00 - replay our jump animation when doing a double jump
  • 5:15 - modify our script to do 2,3,4, or unlimited amount of jumps in the air

Source Code Used in this Video

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class Character : MonoBehaviour {

	[HideInInspector] public bool facingRight = true;
	[HideInInspector] public float moveForce = 50f;
	[HideInInspector] public float maxSpeed = 10f;
	[HideInInspector] public float currentSpeed = 0.0f;
	[HideInInspector] public float touchRun = 0.0f;
	[HideInInspector] public float jumpSpeed = 0.0f;

	private Rigidbody2D rb2d;
	private float ScreenWidth;
	private Animator animator;

	public Transform groundCheck;
	public bool grounded = false;
	public float jumpForce = 800f;

	private bool jump = false;
	private int numofJumps = 0;
	private int maxJumps = 4;


	// Use this for initialization
	void Awake () {
		rb2d = GetComponent();
		ScreenWidth = Screen.width;
		animator = GetComponent ();
	}

	// Update is called once per frame
	void Update () {
		grounded = Physics2D.Linecast (transform.position, groundCheck.position, 1 << LayerMask.NameToLayer ("Ground"));
		animator.SetBool ("isGrounded", grounded);
		if (Input.GetButtonDown ("Jump")) {
			jump = true;
		}
		
		touchRun = 0.0f;
		//-----TOUCH CONTROLS------
		int i = 0;
		//loop over every touch found
		while (i < Input.touchCount) {
			if (Input.GetTouch (i).position.x < ScreenWidth / 4) {
				//move left
				touchRun = -1.0f;

			} else if (Input.GetTouch (i).position.x < ScreenWidth / 2) {
				//move right
				touchRun = 1.0f;
			}
			if (Input.GetTouch (i).phase == TouchPhase.Began && Input.GetTouch (i).position.x > ScreenWidth * 0.5) {
				jump = true;
			}
			++i;
		}

		//-----CONTROLLER------
		#if UNITY_EDITOR
		touchRun = Input.GetAxis("Horizontal");
		#endif

	}

	void FixedUpdate(){
		moveCharacter(touchRun);

		if (jump)
			JumpCharacter ();

		float characterSpeed = Mathf.Abs (rb2d.velocity.x);
		animator.SetFloat ("Speed", characterSpeed);
	}

	void JumpCharacter(){
		if (grounded)
			numofJumps = 0;
		if (grounded || numofJumps < maxJumps) {
			if (!grounded)
				animator.Play ("character-jump", -1, 0f);
			
			rb2d.velocity = new Vector2 (rb2d.velocity.x, 0);
			rb2d.AddForce (new Vector2 (0f, jumpForce));
			numofJumps += 1;
			grounded = false;
		}
		jump = false;
	}




	void moveCharacter(float h){
		//float modMoveForce = grounded ? moveForce : moveForce * 1; //slow down if in air
		if (h * rb2d.velocity.x < maxSpeed)
			rb2d.AddForce (Vector2.right * h * moveForce);

		if (Mathf.Abs (rb2d.velocity.x) > maxSpeed)
			rb2d.velocity = new Vector2 (Mathf.Sign (rb2d.velocity.x) * maxSpeed, rb2d.velocity.y);


		if (h > 0 && !facingRight)
			Flip ();
		else if (h < 0 && facingRight)
			Flip ();
	}
	void Flip()	{
		facingRight = !facingRight;
		Vector3 theScale = transform.localScale;
		theScale.x *= -1;
		transform.localScale = theScale;
		rb2d.velocity = new Vector2 (rb2d.velocity.x * 0.75f, rb2d.velocity.y);

	}
}

This tutorial is sponsored by this community

In order to stick to our mission of keeping education free, our videos and the content of this website rely on the support of this community. If you have found value in anything we provide, and if you are able to, please consider contributing to our Patreon. If you can’t afford to financially support us, please be sure to like, comment and share our content — it is equally as important.

Join The Community

Discussion

Browse Tutorials About