- Home /
Rigidbody - how to stop it quickly
Hi, I am playing around with a Sphere - with rigidbody added to it. I add force to it, to make it move. The body should gradually come to rest - which it does. However it takes a lot of time to do so.
I have max'ed the friction of the physics material attached to it - it helps, but the object still takes time to die out.
I have tried adding Angular & Linear Drag to it. Which causes it to slow down quicker - but when the velocity is low , it still takes forever to come to rest (or go to Sleep).
Answer by jacobschellenberg · Mar 13, 2014 at 05:52 PM
Don't forget about the angular velocity.
function FixedUpdate () {
if (rigidbody.velocity.magnitude < .01) {
rigidbody.velocity = Vector3.zero;
rigidbody.angularVelocity = Vector3.zero;
}
}
Answer by Eric5h5 · Mar 13, 2014 at 09:44 AM
You could do something like
function FixedUpdate () {
if (rigidbody.velocity.magnitude < .01) {
rigidbody.velocity = Vector3.zero;
}
}
It would solve the issue for me. However I was looking for something more in terms of tweaking the Physics / Rigidbody properties.
Is there anything you could think of in lines of Physics / rigibody properties which can be tweaked via the editor?
Try the sleep velocity in the physics settings, but that would affect all objects.
Same trick can be extended to make it quickly slow down below some speed: if(...magnitude<1) rigid body.velocity*=0.9f;
(10% off speed/frame is a lot.)
Answer by RafaelCN · Mar 13, 2014 at 06:04 PM
You can do something like to slow down the object:
public float walkDeacceleration = 5f;
public float walkDeaccelerationOnX;
public float walkDeaccelerationOnZ;
if(Input.GetAxis("Horizontal") == 0 && Input.GetAxis("Vertical") == 0 && grounded)
{
rigidbody.velocity = new Vector3(Mathf.SmoothDamp(rigidbody.velocity.x, 0,
ref walkDeaccelerationOnX,
walkDeacceleration),
0,
Mathf.SmoothDamp(rigidbody.velocity.z, 0,
ref walkDeaccelerationOnZ,
walkDeacceleration));
}
Answer by benni05 · Mar 13, 2014 at 09:44 AM
Try to increase its mass. Other than that, the moment you start your stopping motion (or you no longer apply your forces), start a timer (store Time.time in a variable, myStopMotionStartTime) and after a defined amount of time (the maximum amount of time you want it to stop, in this example 0.3 seconds) you switch the Rigidbody to kinematic and it will stop immediately.
void Update() {
if (Time.time > myStopMotionStartTime + 0.3f) {
transform.rigidbody.isKinematic = true;
}
}
$$anonymous$$ass has nothing to do with it. Two objects with different masses (but otherwise identical properties) dropped from the same height will hit the ground at the same time, and stop rolling at the same time.
Well, he didn't say that his objects are falling, I doubt it. What you describe only happens because gravitation for an object is proportional to its mass. In his case where most likely the object moves and is accelerated on a surface the same amount of force applied to 2 objects of different mass of course leads to one object moved further and for a longer time and the other, heavier will not go as far and stop earlier.
Apart from that the solution you have suggested is probably what he needs and the easiest one to apply.
In the real world, a balloon will bounce off a chair cushion, but an apple won't. For real, mass matters, making the apple sink in. Same way light things can roll over a thick pile carpet. But rigidbody physics is a simplification. The chair cushion is rigid and has the exact same abstract "bounciness" for everything. And there is no "sink in more due to mass" friction.
In Unity, mass only matters for the two AddForces that divide by mass, and when hitting another moving object. It has no effect on friction and collision with frozen objects. Try it (been a while for me.) Let stuff bounce around (roll it down a ramp, etc... ,) over and over with different masses.
ne guides on how to bring rigidbodies to halt quicker? they keep going slowly before co$$anonymous$$g to halt.