How to JumpLearn How to Make Your Character Jump in Unity

Author Waldo
Published April 27, 2018

In this quick Unity tutorial I go over 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

  • 0:45 setup our animator transitions
  • 1:08 setup an animator parameter for isGrounded
  • 2:29 setup our jump variables
  • 3:37 create a ground check child game object
  • 4:30 set our ground tiles to the layer "Ground"
  • 5:05 create a linecast to check if grounded
  • 6:32 Detect inputs for jump
  • 7:14 mobile jump inputs
  • 8:23 create a jump character function

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;


	// 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) {
			rb2d.AddForce (new Vector2 (0f, jumpForce));
			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