• 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 $$anonymous$$ · Jul 01, 2019 at 08:03 PM · jumpingplayer movementcharacter movementrunning

How do I Un-Clamp a Rigidbody2D's Magnitude

I limited my Player Character's Maximum Speed. But then I had an Issue, where my Character jumps inconsistently. Now, I also managed to fix that issue.

 if (rb.velocity.magnitude > maxSpeed)
             {
                 if (jumpingScript.jumpRequest && !jumpingScript.grounded)
                 {
                     //Un-Clamping the Magnitude would be useful in this If-Section
                     //I'd also like to put a Wait 0.5 Seconds Command here. I understand that I need a Coroutine for this, so I'd move the Commands of this If-Section inside a Coroutine just to be able to do this
                     rb.velocity = Vector2.ClampMagnitude(rb.velocity, maxFallingSpeed);
                 }else if (jumpingScript.grounded)
                 {
                     rb.velocity = Vector2.ClampMagnitude(rb.velocity, maxSpeed);
                 }
             }

But now, I have a new issue, again. This time, the issue is, that the Player Character's speed isn't limited in the air anymore. The speed is still limited on the ground, but nothing stops the player from going game-breakingly fast when in the air.

If only I could Un-Clamp the Rigidbody2D's magnitude for a split second before clamping it again. Then, I'd be able to fix the issue with the gamebreaking player speeds in the air without re-introducing the problem of inconsistent jump heights.

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

1 Reply

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

Answer by Bunny83 · Jul 01, 2019 at 09:02 PM

You should split your velocity into the horizontal (planar) part and the vertical part. Then you can just clamp the horizontal part and just keep the vertical as it is. That allows infinite speed along the y axis but only a certain speed in the x-z plane.

 Vector2 vel = rb.velocity;
 
 vel.x = Mathf.Clamp(vel.x, -maxSpeed, maxSpeed),;
 rb.velocity = vel;

You don't need that "magnitude" check at all. It's actually pointless and even more expensive than the actual clamping ^^. You can (or should) execute those lines every FixedUpdate.


Note if you also want to restrict the y velocity you can just duplicate the Mathf.Clamp line and replace vel.x with vel.y and your desired max speed.

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 $$anonymous$$ · Jul 05, 2019 at 07:27 PM 0
Share

thank you very much! I put it all inside the FixedUpdate() method. I replaced this entire part and it works really well: if (rb.velocity.magnitude > maxSpeed) { if (jumpingScript.jumpRequest && !jumpingScript.grounded) { rb.velocity = Vector2.Clamp$$anonymous$$agnitude(rb.velocity, maxFallingSpeed); }else if (jumpingScript.grounded) { rb.velocity = Vector2.Clamp$$anonymous$$agnitude(rb.velocity, maxSpeed); } }

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

108 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

Related Questions

how to make bots stable? ,how to change bots movement? 0 Answers

How to make character controller forward jump a certain distance? 1 Answer

Player can jump on invisible non ground items 1 Answer

Player jump not working correctly 1 Answer

AddForce problem 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