- Home /
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?
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 } }
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 ...
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.
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 } }
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)...
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 :)
Your answer
Follow this Question
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