• 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 /
This question was closed May 11, 2015 at 07:45 PM by AlwaysSunny for the following reason:

Question closed. This shouldn't have been resurrected to begin with.

avatar image
14
Question by Ehren · Jan 11, 2010 at 04:26 PM · physicsrigidbody

Limiting rigidbody velocity

When working with non-kinematic rigidbodies (especially bouncy ones), there can be situations where collisions with static colliders and other rigidbodies result in large increases in velocity. This can lead to undesirable effects, such as objects suddenly "teleporting" to random parts of the screen or breaking through static collider scenery.

What's the best way to limit the velocity of a non-kinematic rigidbody?

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 dingben · Sep 17, 2010 at 07:38 AM 0
Share

Could someone take a look at my post and let me know if this thread is really one and the same issue? You only mention collisions with known objects in this thread... in my scenario I have no collisions with known objects except, if any, the terrain itself... and also do see in my post that it happens while vehicle is at complete idle. http://answers.unity3d.com/questions/20456/terrain-unexpected-collisions-loopidy-loops

7 Replies

  • Sort: 
avatar image
0

Answer by Spiff McShizly · Jan 14, 2013 at 09:59 PM

here's how I did it quick & dirty:

 // clamp velocity:
 Vector3 newVelocity = rigidbody.velocity.normalized;
 newVelocity *= m_MaximumVelocity;
 rigidbody.velocity = newVelocity;
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 3itg · Mar 07, 2014 at 09:38 PM 1
Share

This was the best answer for me, only slightly modified (my game is 2d / c#).

         if($$anonymous$$athf.Abs(rigidbody.velocity.x) > maxSpeed || $$anonymous$$athf.Abs(rigidbody.velocity.y) > maxSpeed)
         {
             // clamp velocity:
             Vector3 newVelocity = rigidbody.velocity.normalized;
             newVelocity *= maxSpeed;
             rigidbody.velocity = newVelocity;
         }
avatar image
16

Answer by Ehren · Feb 02, 2010 at 05:31 PM

I just posted a couple scripts for limiting velocity, along with an example project, to my blog. Here is the script for Duck's proposed solution.

#pragma strict

// This MonoBehaviour uses hard clamping to limit the velocity of a rigidbody.

// The maximum allowed velocity. The velocity will be clamped to keep // it from exceeding this value. var maxVelocity : float;

// The cached rigidbody reference. private var rb : Rigidbody; // A cached copy of the squared max velocity. Used in FixedUpdate. private var sqrMaxVelocity : float;

// Awake is a built-in unity function that is called called only once during the lifetime of the script instance. // It is called after all objects are initialized. // For more info, see: // http://unity3d.com/support/documentation/ScriptReference/MonoBehaviour.Awake.html function Awake() { rb = rigidbody; SetMaxVelocity(maxVelocity); }

// Sets the max velocity and calculates the squared max velocity for use in FixedUpdate. // Outside callers who wish to modify the max velocity should use this function. Otherwise, // the cached squared velocity will not be recalculated. function SetMaxVelocity(maxVelocity : float){ this.maxVelocity = maxVelocity; sqrMaxVelocity = maxVelocity * maxVelocity; }

// FixedUpdate is a built-in unity function that is called every fixed framerate frame. // We use FixedUpdate instead of Update here because the docs recommend doing so when // dealing with rigidbodies. // For more info, see: // http://unity3d.com/support/documentation/ScriptReference/MonoBehaviour.FixedUpdate.html function FixedUpdate() { var v = rb.velocity; // Clamp the velocity, if necessary // Use sqrMagnitude instead of magnitude for performance reasons. if(v.sqrMagnitude > sqrMaxVelocity){ // Equivalent to: rigidbody.velocity.magnitude > maxVelocity, but faster. // Vector3.normalized returns this vector with a magnitude // of 1. This ensures that we're not messing with the // direction of the vector, only its magnitude. rb.velocity = v.normalized * maxVelocity; }
}

// Require a Rigidbody component to be attached to the same GameObject. @script RequireComponent(Rigidbody)

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 Bravini · May 06, 2011 at 06:24 PM 0
Share

nice blog too btw!

avatar image Develax · May 20, 2019 at 10:48 AM 0
Share

The logic and calculations are correct.

  • ‹
  • 1
  • 2

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

19 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

Related Questions

How to make Rigidbody.AddForce less delayed in Unity3D? 0 Answers

How can I constrain physics in all directions except one? 2 Answers

Avoid bouncing when ground move down 1 Answer

How do you attach moving parts (moved by physics) to an animated character? (Secondary Animations) 1 Answer

Earth quake with physics? 1 Answer


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