How to make the player Object Jump in Unity 3D.

How to make the player Object Jump when we press Space button on our Keyboard.

For Example, we got our player Object. The player Object ist just a simple Sphere Object with Red material on it.

If we add this Script to our Object, we make it Jump within pressing on the Space Button.

using UnityEngine;

[RequireComponent(typeof(Rigidbody))]
public class Jump : MonoBehaviour {

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

    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Space))
        {
            rb.AddForce(new Vector3(0, 10, 0), ForceMode.Impulse);
        }
    }
}

We add 10 Points of Force (on the Y axis) to our Rigidbody Function. We do it with Vector3 and ForceMode.Impulse function.

The problem is we can do multiple Jumps at the same time. When we change the Value of 10 to 5 and press the space button multiple time we get something like this.

It’s not that bad actually… but if you didn’t planned to implement this in your project then you need to ground the player Object.

We can do it when we add “isGrounded” to our if operation as bool value and set the value to true in the void function OnCollisionStay()

using UnityEngine;

[RequireComponent(typeof(Rigidbody))]
public class Jump : MonoBehaviour {

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

    void OnCollisionStay()
    {
        isGrounded = true;
    }

    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Space) && isGrounded)
        {
            rb.AddForce(new Vector3(0, 5, 0), ForceMode.Impulse);
            isGrounded = false;
        }
    }
}

This way we can do a double jump:

But if your goal was that the player got only the possibility to do a single jump than you need to replace the OnCollisionStay() function with OnCollisionEnter() function and tag your ground object with “Ground” as tag name.

Final Script for Single Jump:

using UnityEngine;

[RequireComponent(typeof(Rigidbody))]
public class Jump : MonoBehaviour {

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

    void OnCollisionEnter(Collision col)
    {
        if (col.gameObject.tag == ("Ground") && isGrounded == false)
        {
            isGrounded = true;
        }
    }

    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Space) && isGrounded)
        {
            rb.AddForce(new Vector3(0, 5, 0), ForceMode.Impulse);
            isGrounded = false;
        }
    }
}

This way we achieved the possible solutions for making the player jump.

Other things which we can do is making the jump variable public and have more “space” for modifying our jumping behavior. Or we do it with other buttons or key press.

So if we want to make the player jump with our left mouse button click or while touching the mobile screen (if we plan to release it on android) we can write it this way:

void Update()
{
    if (Input.GetKeyDown(KeyCode.Space) || Input.GetMouseButton(0) && isGrounded)
    {
        rb.AddForce(new Vector3(0, 5, 0), ForceMode.Impulse);
        isGrounded = false;
    }
}

And if we want to modify the speed of the player jumping we can write it this way:

using UnityEngine;

[RequireComponent(typeof(Rigidbody))]
public class Jump : MonoBehaviour {

    public float jumpSpeed = 5f;
    public bool isGrounded;
    Rigidbody rb;

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

    void OnCollisionEnter(Collision col)
    {
        if (col.gameObject.tag == ("Ground") && isGrounded == false)
        {
            isGrounded = true;
        }
    }

    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Space) || Input.GetMouseButton(0) && isGrounded)
        {
            rb.AddForce(new Vector3(0, 2, 0) * jumpSpeed, ForceMode.Impulse);
            isGrounded = false;
        }
    }
}

This way we can modify the speed throught inspector too!

Other posts on the Unity3D topic:

How to create a simple countdown Timer in Unity

Tutorial: How to access variable from other class in Unity

Leave a Reply

Your email address will not be published. Required fields are marked *

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