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 for jump.

If your character movements in the game does 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

  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 parameters in the inspector window.
  6. You character is ready to jump

Unity 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.

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 greater than the offset.
  3. If yes then execute the jump script.

Example script

public class Character_jump : MonoBehaviour
{
    public float jumpforce = 10f;
    bool not_grounded=false;
    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

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

  1. Change Rigidbody to Rigidbody2D.
  2. Change Vector3 to Vector2.
  3. Here is the final script.

Jump script for Unity 2D

public class Character_jump : MonoBehaviour
{
    public float jumpforce = 10f;
    bool not_grounded=false;
    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 the 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 if you change the mass.

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 does’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 clicking window>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. Click record button on the animator window.
  5. Set the ground position of the first and the last frame.
  6. Select the middle frame and set the character position for the highest position jump point.
  7. Use the animator curve to make the jump animation look natural.
  8. Create another empty animation clip called rest. This will be our default state.

Adding animator to the character

  1. Select the character in the Hierarchy window.
  2. Select the animation in the project window and uncheck loop time.
  3. Click add component in the inspector window and add animator.
  4. Add the animation clip Player_jump to animator.
  5. Add the script below to play the animation when space bar is presses.

Animator window setting

  1. Set rest clip as the default state.
  2. Make a transistion from Player_jump to rest. Check “has exit time”.

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 to control the motion of the player.

If you have any other question on player jump, leave it in the comments below.

Leave a Reply

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