Control jump height and double jump

Hi, this is my code for jump and double jump, it works perfectly but cannot control the height of the jump. What I’m looking for is a maximum height the character reaches if I press the jump button long enough and variable height if I let go the jump button before the max height.

 if (Input.GetKeyDown ("space"))
         {
             if (is_Grounded)
             {
                 is_Grounded = false;
                 cir_Grounded = false;
                 cir_GroundedS = false;
                 m_Rigidbody2D.velocity = new Vector2(m_Rigidbody2D.velocity.x, 0); //need to kill velocity on y axis so I can apply a full force on the doube jump
                 m_Rigidbody2D.AddForce (new Vector2(0f, m_JumpForce));
                 doubleJump = true;
             }
             else if (doubleJump)
             {
                 m_Rigidbody2D.velocity = new Vector2(m_Rigidbody2D.velocity.x, 0);
                 m_Rigidbody2D.AddForce (new Vector2(0f, m_JumpForce));
                 doubleJump = false;
             }
         }

this is my actual code, the character has a rigidbody which is affected by gravity. Does anyone have any idea how to control the height?

Simply check for your desired height and apply a negative force … For eg.

if(m_Rigidbody2D.position.y >= YOUR DESIRED HEIGHT){
    m_Rigidbody2D.AddForce(new Vector2(0f, (m_JumpForce * -1f)));
}

I know this question has been answered, but I have a solution that manages the jump curve and gives you that less floaty feel as well as handle multi jumps.


Visit my GitHub for the code: GitHub - I-Am-Err00r/Total-Jump-Solution: The scripts for the jump solution based on the YouTube video I uploaded


If you want an explanation on how this works, visit my YouTube video explaining this solution: Total Jump Solution: Less Floaty Jumps While Managing Jump Height Curve For 2D And 3D Platformers - YouTube