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.GetKeyDown(KeyCode.Space))
            {
                rb.AddForce(Vector3.up * jumpforce,ForceMode2D.Impulse);
                
            }
        
    }
    
}

Avoiding Mid Air jump with Ground check in Unity

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. You can check the offset for difference in heigh like in the video above but it will work only for plane grounds.

The image below shows the other scenarios that you can use

Ground check methods

So, the best way to check if the object is on ground is using the box cast with a layer mask.

To Check if character is on the ground

This trick will work in case of a Plain ground with no ups and down.

  1. Create a new layer for the ground. We will use using it for the box cast.
  2. Add the script below and set the size of box, max distance and layerMask.

If you want to learn how to create this script then check out this video

Jump script with isgrounded check

public class Character_jump : MonoBehaviour
{
    Rigidbody rb;
    public Vector3 boxSize;
    public float maxDistance;
    public LayerMask layerMask;
    public float jumpforce = 10f;

    // Start is called before the first frame update
    void Start()
    {
        rb=GetComponent<Rigidbody>();
    }

    // Update is called once per frame
    void Update()
    {
        
        if(Input.GetKeyDown(KeyCode.Space) && GroundCheck())
        {
            rb.AddForce(transform.up*jumpforce,ForceMode.Impulse);
        }
        
        
    }
    void OnDrawGizmos()
    {
        Gizmos.color=Color.red;
        Gizmos.DrawCube(transform.position-transform.up*maxDistance,boxSize);
    }
    bool GroundCheck()
    {
        if(Physics.BoxCast(transform.position,boxSize,-transform.up,transform.rotation,maxDistance,layerMask))
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    
     
    
}

Jump script for Unity 2D with ground check

public class Character_jump : MonoBehaviour
{
    Rigidbody2D rb;
    public Vector3 boxSize;
    public float maxDistance;
    public LayerMask layerMask;
    public float jumpforce = 10f;

    // Start is called before the first frame update
    void Start()
    {
        rb=GetComponent<Rigidbody2D>();
        
    }

    // Update is called once per frame
    void Update()
    {
        if(Input.GetKeyDown(KeyCode.Space) && GroundCheck() )
        {
            rb.AddForce(transform.up*jumpforce,ForceMode2D.Impulse);
        }
        
    }

    void OnDrawGizmos()
    {
        Gizmos.color=Color.red;
        Gizmos.DrawCube(transform.position-transform.up*maxDistance,boxSize);
    }
    private bool GroundCheck()
    {
        if(Physics2D.BoxCast(transform.position,boxSize,0,-transform.up,maxDistance,layerMask))
        {
            return true;
        }
        else{
            return false;
        }
    }
    
}

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

Recommended Asset

You can get this Physics based character controller from the Unity Asset store if you don’t want to code. It contains all the required features that you need in a 3D character controller. The player can easily interact with the physical environment, move and push objects, slide on surfaces, climb, wall jump and so on.

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.

%d bloggers like this: