Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 Leier · Dec 29, 2020 at 03:22 AM · movementrigidbodyaddforcespeedlimit

How to limit the force from player input without limiting speed

I want to stop the player from moving the Rigidbody faster than the speed limit, but I don't want to limit the rigidbody speed, so that the Rigidbody can go faster than the speed limit when on ice or when being launched in a ramp. I had one soluting that I didn't like too much and I'm looking for other solutions, here it is:

 input = new Vector2(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"));
 if (Mathf.Abs(rb.velocity.x) > maxSpeed | Mathf.Abs(rb.velocity.z) > maxSpeed)
         {
             input = Vector2.ClampMagnitude(input, 0);
         }
         else
         {
             input = Vector2.ClampMagnitude(input, 1);
         }

I dislike this solution because it messes with the movement, the max speed is never correct and floats too much, this solution overall feels like a work around. For those who know, think of CS:GO movement, where while holding a knife, the max speed is 250, but that doens't stop the player from surfing at thousands of units per second.

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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by sacredgeometry · Dec 29, 2020 at 05:34 AM

Why do you need to limit the speed in that case? Its only ever going to go as fast as you have told it to go. So tell it to go slower and let the physics engine solve for the other cases.

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 Leier · Dec 29, 2020 at 07:59 PM 0
Share

Well, I'm using AddForce to move the object, and my guess would be that while I keep adding force with the input, the speed is going to keep increasing.

Would you suggest that I try to move my object (Player) without AddForce?

avatar image
0

Answer by Eno-Khaon · Dec 29, 2020 at 08:20 PM

While this approach isn't perfect and doesn't properly enforce reaching a top speed, it will still allow you to maintain a high speed without completely giving up control:

 // Get the current velocity and flatten it onto movement axes
 Vector3 lastFlatVel = rb.velocity;
 lastFlatVel.y = 0f;

 // Apply player input force(s), then get the flattened vector for the result
 rb.AddForce(inputForce, ForceMode.Acceleration); // Or similar
 Vector3 flatVel = rb.Velocity;
 flatVel.y = 0f;
 
 if(flatVel.sqrMagnitude > maxSpeed * maxSpeed)
 {
     // Can be combined -- This is easier to read in the webpage
     if(flatVel.sqrMagnitude > lastFlatVel.sqrMagnitude)
     {
         // Store the current vertical speed to insert
         // after making flattened changes
         float currentY = rb.velocity.y;
         // Avoid square root calculations unless this specific condition is met
         rb.velocity = flatVel.normalized * lastFlatVel.magnitude;
         rb.velocity.y = currentY;
     }
 }
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

199 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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 add a velocity limit to an object with rigibody addforce? 1 Answer

AddForce for spaceship acceleration, but with max speed 2 Answers

[Solved]Why doesn't this Rigidbody.AddForce work the way I tell it to? 1 Answer

Pushing rigidbodies around 1 Answer

Prevent Rigidbody From Rotating 3 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