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
0
Question by Hyperion · Aug 03, 2013 at 11:10 PM · rigidbodymomentumkey pressedslowing-down

Rigidbody Momentum

Hello Everyone,

I have a vehicle that's a rigidbody. The issue is that when I release the movement key, it still has too much momentum and flies off the map.

My movement code:

 if (grounded){
         rigidbody.AddRelativeForce(Vector3(0,0,movement *400000));    //move forward/backward
         rigidbody.AddRelativeTorque (Vector3(0,rotate* 20000,0 ));    //turn left/right
     }

So how do I make the vehicle slow down when not pressing the movement key? My mass is 5000; otherwise the vehicle falls too slowly.

I appreciate your help- Hyperion

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
5
Best Answer

Answer by aldonaletto · Aug 03, 2013 at 11:35 PM

You can increase the Drag field (rigidbody.drag property) - this will reduce the momentum faster. Another possibility is to implement your own friction, which may become active only when you want. Since all the momentum is stored in rigidbody.velocity and rigidbody.angularVelocity, simply multiplying them by a number slightly below 1 each FixedUpdate kills the momentum - like this:

 var brake = false; // set it to true to kill the momentum
 var friction: float = 0.95; // 1 means no friction, 0 stops instantly
 
 function FixedUpdate(){
   if (break){
     rigidbody.velocity *= friction;
     rigidbody.angularVelocity *= friction;
   }
 }

NOTE: There's a problem with this approach: it works even when the vehicle is falling, making it fall slowly like in a tar pit. If the world vertical direction is Y, a possible way to work around this is to multiply only X and Z, like this:

 function FixedUpdate(){
   if (brake){
     var vel = rigidbody.velocity;
     vel.x *= friction; // modify only x and z components
     vel.z *= friction;
     rigidbody.velocity = vel;
     // angularVelocity should be reduced as a whole:
     rigidbody.angularVelocity *= friction;
   }
 }

EDITED: Oops! I wrote break, which is a reserved word, and it should be brake - answer fixed now.

Comment
Add comment · Show 3 · 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 Hyperion · Aug 04, 2013 at 12:11 AM 0
Share

Hi, thanks for the answer. One issue: it says "break" is an unexpected token, so may I replace break with something else?

avatar image Hyperion · Aug 04, 2013 at 12:12 AM 0
Share

Ok, I replaced break and set it to true and it works! Thank you very much!

avatar image aldonaletto · Aug 04, 2013 at 12:21 AM 0
Share

$$anonymous$$y bad! It should be brake ins$$anonymous$$d of break, which is a reserved word. Answer fixed.

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

15 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

Related Questions

create momentum on a player that should always moves forward 2 Answers

[Physics] Compensating for Moving/Rotating Terrain 0 Answers

Is it possible to increase stopping distance with forcemode.velocitychange? 0 Answers

How to Gain Forward Speed and Momentum with a Rigidbody? 1 Answer

Rigidbody momentum? 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