2d physics based jumping

I’m trying to have a 2d character jump based on the physics system. What’s the most efficient way to do that? Is it better to use Rigidbody2d.addforce or something else? I’ve tried to set that up, but I’m confused on the syntax (yes, I know it’s basic, but I’m still lost). Thanks.

private Rigidbody2D rb;

//Start

rb = GetComponent();

//Call this when the player hits the desired jump key

rb.velocity = new Vector2(rb.velocity.x, force);

Replace force with the amount of force you want the jump to have(float)

Also, mess around the players Mass and Gravity Scale settings until you get the perfect jump :smiley:

Oh and here is how you get the input:

if(Input.GetKeyDown(KeyCode.KEY)){

//Run this

}

Replace KEY with the desired key :smiley:

Sorry if i confused u, feel free to ask more questions

I’m going to be a little lazy this time around and direct you towards my answer to a previous question: Increase jumpheight while holding down a key

Seeing how Rigidbody2D has fewer options for “AddForce()” than 3D does, however, there are two alternative approaches which may be taken:

  1. Pre-multiply the force of the jump by the mass of the object.

    // using rb for short
    rb.AddForce(jumpVector * rb.mass, ForceMode2D.Impulse);

  2. Modify the velocity directly, but as sanely as possible.

    // using rb for short
    rb.velocity += jumpVector;
    In general, “AddForce()” would be the ideal choice, as a safer bet to ensure that you won’t inadvertantly replace your velocity vector entirely and exhibit unusual and unexpected motion.