• 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 Blankzz · Jul 06, 2011 at 07:08 PM · forcerigidbodiesstopping

Stopping Rigidbodies with Opposing Forces

Hi

I'm currently toying with Unity's physics system and have been trying to use an opposing force to stop an object. I'm not that great at physics so let me know if I am doing it wrong. Here's my code

 function FixedUpdate()
 {
     if(Input.GetButton("Stop"))
     {
         doStop();
     }
 }
 
 function doStop()
 {
     var f:Vector3  = -(rigidbody.mass * rigidbody.velocity);
     rigidbody.AddForce(f, ForceMode.Impulse);
 }

   

It seems to work to a limit so I think I am doing something right but the object moves in its original direction ever so slightly. Any thoughts to why this is happening?

Comment
Add comment · Show 8
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 flaviusxvii · Jul 06, 2011 at 07:17 PM 0
Share

I fixed your formatting..

avatar image Blankzz · Jul 06, 2011 at 07:21 PM 0
Share

thanks. How did you fix it?

avatar image flaviusxvii · Jul 06, 2011 at 07:22 PM 0
Share

Well, if you paste the code in and line it up nice and then select it all and hit the little '101010' button it will indent it all a bit and it looks like a $20 bill.

avatar image Blankzz · Jul 06, 2011 at 07:39 PM 0
Share

Did that. $$anonymous$$ust be google chrome......

avatar image ProjSHiNKiROU · Jul 06, 2011 at 07:42 PM 0
Share

$$anonymous$$aybe rigidbody sleeping helps http://unity3d.com/support/documentation/Components/RigidbodySleeping.html

Show more comments

4 Replies

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

Answer by Blankzz · Jul 06, 2011 at 10:46 PM

Thought I would rephrase my solution because some might get confused why I didn't just set rigidbody.useGravity = false;. I avoided this so I could use damping on the effect like so

 private var strength:float = 0;
 private var smoothTime:float = 2.0;
 
     function FixedUpdate()
     {
         if(Input.GetButton("Stop"))
         {
             doStop();
         }
         if(Input.GetButtonUp("Stop"))
         {
             strength = 0;
         }
     }
     
     function doStop()
     {
         Mathf.SmoothDamp(0.0f, 1.0f, strength, smoothTime);
         var f:Vector3  = -(rigidbody.mass * rigidbody.velocity) * strength;
         rigidbody.AddForce(f, ForceMode.Impulse);
         rigidbody.AddForce(-(Physics.gravity) * strength, ForceMode.Acceleration);
     }
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
avatar image
1

Answer by Bunny83 · Jul 06, 2011 at 07:25 PM

You use ForceMode.Impulse so the effect will be immediately. If you relly want to stop it just zero the velocity ;)

 rigidbody.velocity = Vector3.zero;
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 Blankzz · Jul 06, 2011 at 07:35 PM 0
Share

I hadn't thought about doing that thanks. The trouble is if I have another force act on the object but the stop function gets called last the object will stop. I'm not sure how I can tell the order of function calls in unity so I suppose that's another question?

avatar image Blankzz · Jul 06, 2011 at 07:37 PM 0
Share

Just in case you were wondering. In the end the force will ease in so it would gradually come to a stop.

avatar image
1

Answer by Blankzz · Jul 06, 2011 at 09:40 PM

Ok so I've solved the problem. I needed to treat gravity separately and apply a force opposite to gravity. Here's the code. Hope it helps others.

 function FixedUpdate()
 {
     if(Input.GetButton("Stop"))
     {
         doStop();
     }
 }
 
 function doStop()
 {
     var f:Vector3  = -(rigidbody.mass * rigidbody.velocity);
     rigidbody.AddForce(f, ForceMode.Impulse);
     rigidbody.AddForce(-(Physics.gravity), ForceMode.Acceleration);
 }
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 flaviusxvii · Jul 06, 2011 at 10:33 PM 0
Share

rigidbody.useGravity = false;

avatar image shaibam · Jul 06, 2015 at 01:15 PM 0
Share

How can I apply the power with nicer ease?

avatar image
0

Answer by flaviusxvii · Jul 06, 2011 at 07:18 PM

rigidbody.velocity is accidentally the right value (since force is mass x acceleration, and you want to accelerate to a zero velocity). I think imperfection in floating point math will result in some residual movement. I think you'll need to add a script that'll dampen / eliminate the resulting movement. So anything in your system traveling slower than some "minimum" speed will stop.

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 Blankzz · Jul 06, 2011 at 07:28 PM 0
Share

Yeah I thought it might be a floating point issue by had to double I was doing it right before proceeding. I think for what I am heading towards stopping the object when the speed reaches a small amount might defeat the purpose of using physics but maybe not. Thanks

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Raycast push? How? 1 Answer

Add Force without Rotation 2 Answers

Rigidbody rotate around 2 Answers

Adding more forces vs adding sum of forces 2 Answers

Getting Force to work properly 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