Jump Script: How to Make an Object Jump in Unity

Please Subscribe to our YouTube Channel

Making an object Jump in Unity is easy and it should not take lots of effort. We have written a complete script that you can simply and drag and drop to any object and make it jump.

Some parts of the code might confuse you. If so, please watch this video to see it in action. You can later copy the code.

The Complete Script: Movement and Jump (Included)

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

public class BallController : MonoBehaviour
{

//Change the numbers to change jumping or moving speed

    public float speed = 5f;
    public float jumpSpeed = 5000f;
	
    Rigidbody rb;
    bool canJump;

    void Start()
    {
        rb = GetComponent<Rigidbody>();
    }

//Checking if the player is on the floor. If so, player can jump, else he cannot. The floor or any object from where you want the player to be able to jump must be tagged as "Floor"

    private void OnCollisionEnter(Collision other)
    {
        if(other.gameObject.tag == "Floor")
        {
            canJump = true;
        }
    }

    private void OnCollisionExit(Collision other)
    {
        if (other.gameObject.tag == "Floor")
        {
            canJump = false;
        }
    }

    void Update()
    {
	
	//Movement Script
 
        if (Input.GetKey(KeyCode.UpArrow) || Input.GetKey(KeyCode.W))
        {
            rb.AddForce(0f, 0f, speed * Time.deltaTime);
        }

        if (Input.GetKey(KeyCode.DownArrow) || Input.GetKey(KeyCode.s)
        {
            rb.AddForce(0f, 0f, -speed * Time.deltaTime);
        }

        if (Input.GetKey(KeyCode.LeftArrow) || Input.GetKey(KeyCode.A)
        {
            rb.AddForce(-speed * Time.deltaTime, 0f, 0f);
        }

        if (Input.GetKey(KeyCode.RightArrow) || Input.GetKey(KeyCode.D)
        {
            rb.AddForce(speed * Time.deltaTime, 0f, 0f);
        }

// Jumping Script. Player can jump only when canJump is true (means when the player is on the floor)
        if (Input.GetKey(KeyCode.Space) & canJump)
        {
            rb.AddForce(0f, jumpSpeed * Time.deltaTime, 0f);
        }

    }
}