Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by Kharon25 · Feb 05, 2020 at 01:30 PM · positionjumpjumpingheightjumping object

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;
     }

}

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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!

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Kharon25 · Feb 05, 2020 at 12:16 PM 0
Share

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 :)

avatar image JPhilipp Kharon25 · Feb 05, 2020 at 12:23 PM 0
Share

@$$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.

avatar image Kharon25 JPhilipp · Feb 05, 2020 at 01:05 PM 0
Share

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

avatar image
0

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

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Kharon25 · Feb 05, 2020 at 11:43 AM 0
Share

I know this video, I am using this code. What I'm trying to do is in addition to this :)

avatar image
0

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Kharon25 · Feb 05, 2020 at 11:55 AM 0
Share

(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

avatar image unity_oyq4fQ2B_8I89Q Kharon25 · Feb 05, 2020 at 04:58 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

149 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges