How to make a character jump in Unity

Jump is one of the most used features in games. So, if you are learning game development you need to know how to make your player jump. There are two major ways to make an object jump. The first one is to use physics and the second method is to use animation. It totally depends on your game type whether you should use physics or animation to jump.

The general rule is, if your character movements in the game do not satisfy physics law then you should use animation to make the character jump. In this tutorial, we will walk you through the steps to implement jump in Unity.

Jump in Unity using physics

Simple jump with Space bar

We will add a Ridigbody component and apply upward force to it when space bar is pressed.

  1. Create a new script called Character _jump using create>new C# script in the project window.
  2. Copy and paste the code below to the script.
  3. Attach Rigidbody component to your character.
  4. Attach the script to your character.
  5. Set your jump force value in the inspector window.
  6. Your character is ready to jump.

Unity simple Jump script

using UnityEngine;
public class Character_jump : MonoBehaviour
{
    public float jumpforce = 10f;
    Rigidbody rb;
    void Start()
    {
        rb = GetComponent<Rigidbody>();
    }
    void FixedUpdate()
    {
        if (Input.GetButtonDown("Jump"))
            {
                rb.AddForce(Vector3.up * jumpforce);
                
            }
        
    }
    
}

Avoiding Mid Air jump

The above script works flawlessly but the character can jump even in midair. If you don’t want your character to jump in midair you need to check if the character is standing on the ground. You can use collision detection also but continuous collision monitoring will affect the performance of your game.

Free Unity asset banner

To Check if character is on the ground

  1. Save the initial offset between character and ground.
  2. Check If the distance between character and ground is less than the offset.
  3. If yes then execute the jump script.

Jump script with isgrounded check

public class Character_jump : MonoBehaviour
{
    public float jumpforce = 10f;
    public GameObject ground;
    Rigidbody rb;
    float offset;
    void Start()
    {
        rb = GetComponent<Rigidbody>();
        //We are going to jump in the Y axis
        offset=(transform.position.y-ground.transform.position.y)+1;
    }
    void FixedUpdate()
    {
        if(transform.position.y-ground.transform.position.y<offset)
        {
            if (Input.GetButtonDown("Jump"))
            {
                rb.AddForce(Vector3.up * jumpforce);
                
            }
         }
        
    }
    
}

Implementing jump with physics in Unity 2D

The above script can be used for 2D jump with little alterations. Do the following changes in the script.

  1. Change Rigidbody to Rigidbody2D.
  2. Change Vector3 to Vector2.

Jump script for Unity 2D

public class Character_jump : MonoBehaviour
{
    public float jumpforce = 10f;
    public GameObject ground;
    Rigidbody2D rb;
    float offset;
    void Start()
    {
        rb = GetComponent<Rigidbody2D>();
        //We are going to jump in the Y axis
        offset=(transform.position.y-ground.transform.position.y)+1;
    }
    void FixedUpdate()
    {
        if(transform.position.y-ground.transform.position.y<offset)
        {
            if (Input.GetButtonDown("Jump"))
            {
                rb.AddForce(Vector2.up * jumpforce);
                
            }
         }
        
    }
    
}

Controlling fall in Jump physics

We can control the jump height by changing the jump force. But the speed at which the object will fall down depends on the mass and gravity. Changing gravity will affect other objects in the game. So, you can increase or decrease the mass of the gameobject to control the speed of fall. You also need to change the jump force to achieve the same jump height as earlier after you changed the mass.

Rigidbody component Unity

Implement jump with Animation

Implementing jump with animation is recommended for non-physics games. But there are a few drawbacks of this method.

  1. You cannot change the height of jump during the game.
  2. Jump looks unnatural.

This doesn’t mean there are only disadvantages. You can control the speed of animation which in turn controls the time required for the jump. This control is not available with physics.

Animating character to simulate jump

  1. Open the animation window by going to window>Animation>Animation.
  2. Select your character in the hierarchy window and click create new animation on the Animation window.
  3. Save the animation as Player_jump.
  4. This will create an Animation clip and add an Animator to the character.
  5. Click the record button on the Animation window. Don’t change anything on the Animation window without selecting the record button. If you do then the changes will not be saved.
  6. Set the player to ground position for the first and the last keyframes.
  7. Select the middle keyframe and set the character position for the highest position jump point.
  8. Click on the Curves tab on the bottom of the Animation window.
  9. Use the Animation curve to make the jump animation look natural.
  10. Click on the Animation name and select create new.
  11. Create another empty animation clip called Rest. This will be our default state.
Animation window

Adding Animator to the character

While creating Animation, Unity would have added the Animator component to the Player. This step is only required if your player does not have an Animator component.

  1. Select the character in the Hierarchy window.
  2. Select the animation in the project window and uncheck loop time.
  3. Find the Animator component of your player and add it to the component of the player.
  4. Open the Animator window by going to WIndow>Animation>Animator.
  5. You should see the animation clips inside the Animator. If not, add the animation clip Player_jump and Rest to animator.
  6. Add the script below to play the animation when space bar is pressed.

Animator window setting

  1. Set rest clip as the default state by right clicking on the Animation state.
  2. Make a transition from Player_jump to rest. Check “has exit time”.
Animator window

Script to implement jump with animation

using UnityEngine;

public class Character_jump : MonoBehaviour
{

    Animator anim;

    void Start()
    {
        anim = GetComponent<Animator>();
    }

    void Update()
    {
        if (Input.GetButtonDown("Jump"))
            {
                anim.Play("Player_jump");
                
            }
        
    }

    
}

You can control the animation speed by selecting the Animation state and setting the speed parameter. If you are using character controller then check out our tutorial on implementing jump with Character controller in Unity.

If you have any other questions on player jump, leave them in the comments below.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.