Is there a way to limit the max height of the Physics 2D bounce?

At the moment, the player can sit on the mushroom and bounce higher and higher until they hit the ceiling of the game. Is there a way to prevent this?

3 Likes

Hi,

You could clamp the player’s velocity like this:

float maxSpeed = 2f;

void ClampVelocity()
{
    if (myRigidbody.velocity.sqrMagnitude > (maxSpeed * maxSpeed))
    {
        Vector3 velocity = myRigidbody.velocity;
        Vector3 newVelocity = velocity.normalized * maxSpeed;
        myRigidbody.velocity = newVelocity;
    }
}

Of course, you have to call the ClampVelocity method somewhere. I’m leaving this challenge for you.

Did this help?


See also:

5 Likes

Perfect, thanks Nina!

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms