- Home /
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?
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.
$$anonymous$$aybe rigidbody sleeping helps http://unity3d.com/support/documentation/Components/RigidbodySleeping.html
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);
}
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;
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?
Just in case you were wondering. In the end the force will ease in so it would gradually come to a stop.
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);
}
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.
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
Follow this Question
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