Generate mid air impulse

I want to get an impulse while jumping.

Thanks for your time.

@Feref2 This is more popularly referred to as a “Double Jump”. To implement a double jump, firstly, you’ll want an initial jump working. Rigidbody.AddForce is perfect for this - Rigidbody.AddForce ScriptReference

Say you want to add force on the Y axis every time you press the Space key down.

Which looks something like this:

Rigidbody rb;

float jumpForce = 7f;

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

If you put this in a FixedUpdate() function and add this in the Start() function:

rb = GetComponent<Rigidbody>();

you’ll notice that you can jump and jump again, without limit. That won’t look very good in most games (unless it’s intended!).


Next, you’ll need to limit how many times the player can jump. You can create a simple system where:

  1. every time the Space key is pressed down, it counts a jump.
  2. if the number of jumps is equal to 2 or more, the player isn’t allowed to jump anymore.
  3. every time the player hits the floor, reset the amount of jumps accounted for.

Which looks something like this:

    void Update ()
    {
        (canJump = true)
        if (jumps < 2)
        {
            canJump = true;
        }
        if (jumps >= 2)
        {
            canJump = false;
        }
    }

	void FixedUpdate () 
	{
        if (Input.GetKeyDown(KeyCode.Space) && canJump == true)
        {
            jumps++;
            rb.AddForce(new Vector3(0, jumpForce, 0), ForceMode.Impulse);
        }
	}

    void OnTriggerEnter (Collider other)
    {
        if (other.gameObject.tag == "Floor")
        {
            jumps = 0;
        }
    }

Now that both elements; jumping & limiting the amount of jumps are implemented, you can put it all together like so:

public class PlayerJump : MonoBehaviour {
    
    Rigidbody rb;
    //will count how many jumps have occured
    int jumps;
    //will set a limit for the amount of jumps that can occur
    int maxJumps = 2;
    float jumpForce = 5f;
    //will be used to determine if player is allowed to jump 
    bool canJump;

	void Start () 
	{
        //get rigidbody from gameObject that is attached to this script
        rb = GetComponent<Rigidbody>();
	}

    void Update ()
    {
        //if less than 2 jumps have occured, the player is allowed to jump (canJump = true)
        if (jumps < 2)
        {
            canJump = true;
        }
        //if more than or equal to 2 jumps have occured, the player is not allowed to jump (canJump = false)
        if (jumps >= 2)
        {
            canJump = false;
        }
    }

    //always use FixedUpdate() for physics-related code 
	void FixedUpdate () 
	{
        //if Space key is pressed and canJump is true, then:
        //add +1 to "jumps" and impulsively add force to rigidbody on the Y axis
        if (Input.GetKeyDown(KeyCode.Space) && canJump == true)
        {
            jumps++;
            rb.AddForce(new Vector3(0, jumpForce, 0), ForceMode.Impulse);
        }
	}

    void OnTriggerEnter (Collider other)
    {
        //if collided object's tag is equal to "Floor", reset jumps to 0
        if (other.gameObject.tag == "Floor")
        {
            jumps = 0;
        }
    }
}

Make sure that:

  • your player has a Rigidbody component attached to it
  • the floor is tagged “Floor” and has it’s collider with isTrigger ticked.
  • the floor has a second collider with isTrigger un-ticked (so the player can walk on it).

Should look like this:

Hopefully you learned how to make a double jump from scratch from this answer.

Feel free to ask any questions on anything that isn’t clear and I’ll be glad to answer to the best of my ability!

A side note: There are many great tutorials on YouTube and the Unity Learn section, I recommend you check them out!