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
1
Question by Fattie · Sep 07, 2012 at 04:46 PM · physicsrigidbody

Is it elegant to use .drag to stop a rigidbody?

You have a rigidbody with a velocity of 1.715 m/s,, it could be heading any direction. Perhaps think of it as drifting in space, or, moving in some way on a surface.

(It is not necessarily heading .forward - it ay be crabbing around or even tumbling etc - so AddRelativeForce may not be relevabt.)

You want this little devil to come to a stop .... in all circumstances, no matter what it is doing.

You can, I suppose, just set rigidbody.drag - in a way that;'s quite elegant. Regardless of the direction, what it's doing, etc, it will quickly stop in a gooey manner. (As a bonus angularDrag would be quite hard to write by hand.)

But in other ways it seems rather inelegant - how and when do you set the .drag back to normal? What if another process wants to kick it along in the meantime? And so on.

It would seem to be an extremely commonplace need ... "make an object slow down and stop naturalistically" Am I missing something obvious?

Should I just shut up and write this by hand in all situations? Is there an elegant way to deal with cancelling the concept I'm missing?

It's almost like .. t would be great if you could put it to "sleep + honey drag" ... and in much the same way as sleep, if someone added force to it it would completely wake up on it's own accord.)

Cheers !

Associated: I don't suppose any knows the units or sensibility of rigidbody.drag? How much to apply give velocity to stop in given time?

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

Answer by aldonaletto · Sep 07, 2012 at 05:27 PM

The rigidbody "energy" is stored in their properties velocity and angularVelocity. If you zero them, the rigidbody stops. You may implement an artificial kind of drag to kill its energy without altering drag or angularDrag, and apply it in a coroutine, like this:

public var killSpeed: float = 0.9;

function StopMe(): IEnumerator { while (!rigidbody.IsSleeping()){ // kill rigidbody energy each physics cycle: rigidbody.velocity = killSpeed; rigidbody.angularVelocity = killSpeed; yield WaitForFixedUpdate(); // resume next physics cycle } }

Comment
Add comment · Show 7 · 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 Fattie · Sep 07, 2012 at 05:48 PM 0
Share

O$$anonymous$$G ! face palm! I totally forgot ordinary numbers scale velocity!

While() ... your point here is that once the velocities reach zero it WILL in fact go to sleep right? So tha stops our routine automatically then? $$anonymous$$y God, that's ingenious! Thanks !!

You can see the only problem remaining ..... say some other agent suddenly wants to apply force to the object (or, indeed it didn't occur to me, something might bounce the object) ... we's have to stop our coroutine right?

There's really no magic way around that right?

You'd just have to carefully write routines to catch all such situations, depending on your project ... right?

there's really no way to kind of override the physics holistically, catch anything that wants to change velocity, add force, or throw it around otherwise ?

Thank you so much! Like magic ...

avatar image Owen-Reynolds · Sep 07, 2012 at 06:51 PM 0
Share

The coroutine could stop itself if it notices the velocity appears to have been changed externally. Various ways:

 float sp=rigidbody.sqr$$anonymous$$agnitude;
 while(not sleeping and rigidbody.sqr$$anonymous$$agnitude<sp)

In this case, anything that makes it go faster would kill the coroutine.

avatar image aldonaletto · Sep 07, 2012 at 06:57 PM 0
Share

Good point! This would kill any energy, even the new ones. $$anonymous$$aybe a workaround could be to abort the coroutine when any of the velocities gets increased:

public var killSpeed: float = 0.9; private lastVel: float; private lastAng: float;

function Stop$$anonymous$$e(): IEnumerator { var rb = rigdbody; lastVel = rb.velocity.sqr$$anonymous$$agnitude; lastAng = rb.angularVelocity.sqr$$anonymous$$agnitude; while (!rigidbody.IsSleeping()&& rb.velocity.sqr$$anonymous$$agnitude <= lastVel && rb.angularVelocity.sqr$$anonymous$$agnitude <= lastAng){ // kill rigidbody energy each physics cycle: rb.velocity = killSpeed; rb.angularVelocity = killSpeed; lastVel = rb.velocity.sqr$$anonymous$$agnitude; lastAng = rb.angularVelocity.sqr$$anonymous$$agnitude; yield WaitForFixedUpdate(); // resume next physics cycle } }

avatar image Fattie · Sep 07, 2012 at 06:58 PM 0
Share

Owen, I was thinking that - I guess you'd check v. the velocity of the previous frame ... however it occurred to me, in some cases something might want to move it (whether an agent or a bump) and the velocity may not actually be greater (could be reversed for instance, spun - whatever)...

avatar image Fattie · Sep 07, 2012 at 07:01 PM 0
Share

Also -- same -- it occurred to me changes could happen that would not necessarily increase the mags.

(Also there's a nuisance of, if someone wants to call for Stop$$anonymous$$e again while it's already running.)

Should be called Stop$$anonymous$$eInHoney :)

Show more comments

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

9 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Rigidbodies won't collide if mass difference is too high 0 Answers

Object jumping while moving(with addForce) 0 Answers

Bonding rigidbodies, then making them seperate when they get exploded 1 Answer

Detecting Input on ~100 Objects 2 Answers

Guidelines for using rigidbody, collider, CharacterControllerScript, etc? 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