Limit Jump/Fly Height? (2D)

how could I limit my player from jumping too high? when i double jump and hold they keep going upwards but i would like to constrain or restrict the player from flying / jump too high.

if (Input.GetButtonDown("Jump") && isGrounded) //so if im on the ground
                {
    
                    myRigidbody.velocity = new Vector3(myRigidbody.velocity.x, jumpSpeed, 0f);
                    jumpSound.Play();
                    secondJumpAvail = true;  //jump and then set my double jump/fly to true!
                    Instantiate(burst2, gameObject.transform.position, burst.rotation); //if i am on the ground
                }
                  if (Input.GetButton("Jump") && !isGrounded) // then if i am jumping and im NOT on the ground do this here
                {
                    StartCoroutine("CanJump");
                    secondJumpAvail = true;
                    if (secondJumpAvail) // if my second jump is avail then do this in the curlys
                    {                    
                        myRigidbody.velocity = new Vector3(myRigidbody.velocity.x, jumpSpeed, 0f);             
                        if (transform.localPosition.y > 5)
                            secondJumpAvail = false;
                    }
                    else               
                    secondJumpAvail = false;
                }

You could make the variable jumpSpeed decrease depending on the time the button “Jump” is being pressed. For example, you probably could do something like this (not tested):

if (Input.GetButton("Jump") && !isGrounded)
{
    StartCoroutine("CanJump");
    secondJumpAvail = true;
    
    if (secondJumpAvail)
    {                    
        pressTimer += Time.deltaTime;
        newJumpSpeed  = Mathf.Lerp(jumpMaxSpeed, jumpMinSpeed, pressTimer / maxPressTime);

        myRigidbody.velocity = new Vector3(myRigidbody.velocity.x, newJumpSpeed , 0f);             
         
        
    } else 
    {
        pressTimer = 0f;
    }

    if (pressTimer > maxPressTime)
            secondJumpAvail = false;
   }

Basically the idea consists in having a variable controlling the time the user is pressing the jump button and another one to be the limit of time the jump button can be pressed in this manner. Doing the way I did with lerp you can go from an interval of [jumpMaxSpeed, jumpMinSpeed], meaning that as long as the button is being pressed the object velocity will decrease in time. Hope it helps.

Maybe try using Mathf.Clamp() to clamp the y-velocity?

// Not tested:
const float minVelo = 1000; // The lowest the y-velocity can get.
const float maxVelo = 5; // The highest the y-velocity can get.
Vector3 velo = myRigidbody.velocity;
float clampedY = Math.Clamp(velo.y, minVelo, maxVelo);
myRigidbody.velocity = new Vector3 (velo.x, clampedY, 0);

LTonon’s idea would probably work better, depends on what you want to achieve.

If you don’t want to limit your velocity to prevent you from exceeding your desired height-limit, you can perform distance checking to stop the player from exceeding it.

E.g., here is an untested code snippet for doing it in a coroutine:

    float maxJumpDist = 5;
    Rigidbody rbody = this.getComponent<Rigidbody>();
    
    IEnumerator playerJump()
    {
    	Vector3 p0 = this.transform.position;	//	assuming 'this' is the player
    	
    	yield return new WaitUntil(
    		(() => Vector3.Distance(p0, this.transform.position) >= maxJumpDist) ||	//	stop the player
    		isGrounded()	//	in case the player never exceeds the full height.
    	);
	
	rbody.velocity.y = 0;	//	redundant if already grounded, but affect anything.

	yield break;
}

(Code not tested.)

Though this does not necessarily guarantee the player will not surpass the limit. It stops them the frame after they reach/exceed the limit.

To stop them from reaching it, simply add the velocity of their jump for the next frame (using Time.deltaTime) to the current distance in the check to see if they reached/exceeded the bounds.

In this event, you could set their position to be at the exact limit in the next frame and reduce their jump-velocity to 0 still so they don’t fall a few fractions of a unit short (which helps guarantee lower frame rate machines achieve the same result as a higher frame rate, though this effect is marginal in most cases).

@ KUFgoddess , watch this tutorial , it will help you.