Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
7
Question by ShinyTaco · Apr 08, 2014 at 11:19 PM · rigidbodyspeedlimit

How to Limit Speed of a Rigidbody?

Hey everybody,

Can somebody help out with this bit of code? I'm not sure why it doesn't work.

I am trying to limit the speed of the rigidbody to 10.

     // Trying to Limit Speed
     if(rigidbody.velocity.magnitude > 10){
         rigidbody.velocity = Vector3.ClampMagnitude(rigidbody.velocity, maxSpeed);
         print("Why Doesn't this Work?");
         }

Why Doesn't this Work? is showing up in the Console, but my speed is not being limited.

Any help is really appreciated.

Thanks!

Comment
Add comment · Show 3
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 Owen-Reynolds · Apr 09, 2014 at 12:01 AM 0
Share

Is it in Update? Is maxSpeed super big? (and why do you have 10 in the IF, but maxSpeed down below?) $$anonymous$$aybe it does work -- does the ball ever go faster than 10 meters/sec? (if the world is a 2x2 box, a fast-looking ball might be speed 5.) Is some other code increasing the speed again?

avatar image ShinyTaco · Apr 09, 2014 at 01:08 AM 0
Share

It's in FixedUpdate.

Does it matter if 10 or maxSpeed is in the IF? I'm testing different numbers to see if I can get the code to work.

Yes, the Player continues to accelerate.

avatar image Radivarig · Apr 09, 2014 at 02:43 AM 0
Share

Have you tried my scipt? I tested on a cube, but it should work for any object

2 Replies

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

Answer by Radivarig · Apr 08, 2014 at 11:36 PM

Drag this on the cube:

 using UnityEngine;
 using System.Collections;
 
 [RequireComponent (typeof (Rigidbody))]
 public class NewBehaviourScript : MonoBehaviour {
     public float maxSpeed = 10f;
 
     void Update()
     {
         // Trying to Limit Speed
         if(rigidbody.velocity.magnitude > maxSpeed){
             rigidbody.velocity = Vector3.ClampMagnitude(rigidbody.velocity, maxSpeed);
         }
     }
 
     void OnGUI()
     {
         GUI.Label(new Rect(20, 20, 200, 200), "rigidbody velocity: " + rigidbody.velocity);
     }
     }
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 ShinyTaco · Apr 09, 2014 at 03:19 AM 1
Share

Works great, thank you.

avatar image Owen-Reynolds · Apr 09, 2014 at 03:15 PM 1
Share

Did you figure out what the original problem was? Seems like that might help you later.

This is 3 changes to the code you started with -- FixedUpdate to Update, replace 10 in the IF, delete/reinstall. So one of them was probably the fix.

avatar image
0

Answer by xpxilom · Jul 30, 2021 at 03:33 AM

 diff.Normalize();
         rb2d.AddForce(diff * Aceleracion * Time.fixedDeltaTime); 
         float speed = Vector3.Magnitude(rb2d.velocity);  // test current object speed
         if (speed > VelocidadMaxima)

         {
             float brakeSpeed = speed - VelocidadMaxima;  // calculate the speed decrease

             Vector3 normalisedVelocity = rb2d.velocity.normalized;
             Vector3 brakeVelocity = normalisedVelocity * brakeSpeed;  // make the brake Vector3 value

             rb2d.AddForce(-brakeVelocity);  // apply opposing brake force
         }
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

25 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

Related Questions

capping the speed of a rigidbody 2 Answers

How to limit the force from player input without limiting speed 2 Answers

Cap rigid body move speed, without capping rotation speed 1 Answer

Limit Speed One Axis 2 Answers

AddForce for spaceship acceleration, but with max speed 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