- Home /
How can I limit jump height in unity?
Hello all,
I'm trying to store the position.y of the character at the beginning of the jump, and compare the new position.y of each frame update with the initial value so I can stop the jump early. ( I cannot use velocity for this since I need a fast jump and I want control the minimum height as well). I don;t know how to store the initial position since the best form I got to updates my h it each frame:
float h;
float offset = 0.2f;
void FixedUpdate{
betterJump();
}
void betterJump()
{
if (IsGrounded() && Input.GetKeyDown(KeyCode.Space))
{
h = playerRB.position.y;
Debug.Log("h is " + h);
animator.SetBool("isPjumping", true);
playerRB.velocity = Vector2.up * jumpVelocity;
jumpCount = 1;
}
//better jump
if (playerRB.velocity.y < 0)
{
playerRB.velocity += Vector2.up * Physics2D.gravity.y * fallMultiplier * Time.deltaTime;
}
else if (playerRB.velocity.y > 0 && !Input.GetKey(KeyCode.Space))
{
if (h + minJump < playerRB.position.y)
playerRB.velocity = Vector2.up * Physics2D.gravity.y * lowJumpMulti * Time.deltaTime;
}
else if (playerRB.velocity.y == 0 && !IsGrounded())
playerRB.velocity += Vector2.up * Physics2D.gravity.y * (2 * fallMultiplier) * Time.deltaTime;
if (playerRB.velocity.y>0 && playerRB.position.y >= h + maxJump)
{
Debug.Log("it works");
playerRB.velocity = Vector2.down;
playerRB.velocity += Vector2.up * Physics2D.gravity.y * fallMultiplier * Time.deltaTime;
}
}
Answer by JPhilipp · Feb 04, 2020 at 03:41 PM
You apply the velocity when the player is grounded. Then you immediately check if it's exceeding the height, but it can't, as you just confirmed it is still grounded, and the rigidbody will need some steps in the physics simulation to actually have that velocity cause a change in transform.position. Nothing yet got moved.
So, for starters, move your height limiting check outside the whole isGrounded scope; it should constantly get checked.
On a side note, in your code sample it reads fixedUpdate{
but I suppose your original uses the correct FixedUpdate(){
Good luck!
Yes, I'm using FixedUpdate() :) I think I typed that part in.
I'm not sure if this is exactly what you meant, but I got to something that works myself by initialising h within the first if (IsGrounded() && Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Space)) and moving the if (playerRB.velocity.y>0 && playerRB.position.y >= h + maxJump) outside of this.
The height limiting check was never inside the isGrounded() function.
This is working fine now, but I'm having trouble replicating it for $$anonymous$$Jump since the following gets called all the time and the character skyrockets before I even press space:
if (h + $$anonymous$$Jump > playerRB.position.y) { Debug.Log("it also works"); playerRB.transform.Translate(0, $$anonymous$$Jump, 0); }
I'll try putting this in the first GetButtonDown check and maybe even turn it into a while loop and switch back to the velocity formula since Translate doesn't really run as smooth as I'd like....
If you have any input on that I'd appreciate it :)
@$$anonymous$$haron25 Alright. You may wanna add your latest code to your question if you need aditional help. The previous height check you did was definitely (and still is in your code above) within the scope of the check that follows if isGrounded is true, which explained the issue. If you now immediately transform the position, then you're likely break smoothness and physics, so you still need to let velocity change it naturally (via Unity's physics engine) and do your height check outside that scope.
I figured it out! I've updated the code and also removed the infinite loop, this works exactly as intended now. Thanks a lot for the support :D. All that's left is to tweak the values of fall$$anonymous$$ultiplier and lowJump$$anonymous$$ulti to get the best feel
Answer by logicandchaos · Feb 04, 2020 at 04:29 PM
Check out this video: https://www.youtube.com/watch?v=7KiK0Aqtmzc&feature=youtu.be&fbclid=IwAR00K5EW5SKQHOMNtLMe7GzcEPz3NZ9J6aoZoSrFgngcDExysyGWv-1dMW0
I know this video, I am using this code. What I'm trying to do is in addition to this :)
Answer by unity_oyq4fQ2B_8I89Q · Feb 04, 2020 at 04:44 PM
I don't really know what this line is supposed to accomplish:
if (h+ maxJump + offset >= playerRB.position.y && playerRB.position.y >= h + maxJump - offset)
What you're basically saying is 20 + 5 + 0.2 >= 30 && 30 >= 20 + 5 - 0.2. That indeed is never gonna be true. If you want it to trigger while the jump is higher than the minimal offset but lower than the maximum offset you should use
if (playerRB.position.y <= h+ maxJump + offset && playerRB.position.y >= h + maxJump - offset)
This way it will trigger when the player is anywhere between "h + maxJump - offset" and h + maxJump + offset.
But if the player is never supposed to be above h + maxJump you can also just use:
if (playerRB.position.y >= h + maxJump)
Might be a lot more clear than using an offset and such. This way it'll also trigger if the velocity is so high that the character moves more than offset*2 within one frame.
(h+ maxJump + offset >= playerRB.position.y && playerRB.position.y >= h + maxJump - offset) and (playerRB.position.y <= h+ maxJump + offset && playerRB.position.y >= h + maxJump - offset) is the same thing and works the same, only the signs are inversed (what I used is the equivalent of the mathematical form h+max+offset >= pos.y >= h+max-offset which should be true whenever pos.y is between those bounds) I agree that I only need to check whenever position.y is above that value so it makes more sense to use playerRB.position.y >= h + maxJump, I've updated that
Oh, yeah, oops haha. I wrote that answer at like 23.00 so wasn't thinking too clear xP
But formatting the math like the second one might be more clear than the first.
Your answer
Follow this Question
Related Questions
Jumping only once (2D) 2 Answers
I have a problem with my jump script 1 Answer
change multiple jump to single jump 1 Answer
jump and check ground problem! 2 Answers
Can't get platforming character to jump 2 Answers