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
0
Question by KUFgoddess · Jan 25, 2018 at 02:28 AM · 2drigidbody2djumping

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

Comment
Add comment · Show 1
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 KUFgoddess · Jan 25, 2018 at 02:32 AM 0
Share

I am intentionally setting this up so that i can jump once and if i jump while off the ground and you hold the jump button they fly.

4 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by LTonon · Jan 25, 2018 at 03:27 AM

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.

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 LTonon · Jan 25, 2018 at 03:30 AM 0
Share

As a simpler solution, you could probably save the starting position of the jump and while you are jumping you check if "(new position y - starting position y) > limit y difference".

avatar image
0

Answer by Lethael · Jan 25, 2018 at 03:27 AM

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.

Comment
Add comment · 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
0

Answer by Zwithak · Jan 25, 2018 at 04:11 AM

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

Comment
Add comment · 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
0

Answer by PersianKiller · Jan 25, 2018 at 06:08 AM

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

https://streamable.com/d7q27

Comment
Add comment · 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

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

2D walls affect my jump height 1 Answer

2D "Platformer" Movement Problems 1 Answer

2d physics for cars. The game from the top down 1 Answer

Rigidbody2d addforce isn't moving character 1 Answer

Collision of child is detected as collision of parent with Rigidbody2D 0 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