- Home /
Force mode and delta timing
I'm trying to make a throwable object that is throwed by pressing a key. I have been reading a lot about this, but I see nothing but contradictory explanations... What I want to do is apply a force in a single FixedUpdate cycle, so in the next ones the object would go on by itself. So, do I need to use ForceMode.Force or ForceMode.Impulse? And also very important, does this force need to be multiplied by Time.deltaTime in any of this force modes?
Answer by ByteSheep · Apr 05, 2013 at 05:42 PM
I personally use rigidbody.AddForce for this sort of thing.
You can apply the force over one frame and the physics engine will automatically carry on the movement of the object.
So you could add force over a single frame - this also means that you shouldn't have to worry about Time.deltaTime..
An example of using rigidbody.AddForce:
if(Input.GetMouseButtonDown(0))
{
ThrowObject.rigidbody.AddForce(Vector3(0, 0.5, 1) * 100);
}
Ofcourse the throw object will need a rigidbody component for this to work correctly.
Yes, I'm using AddForce. But, reading Unity's documentation about force modes, I wasn't sure if I should use Force (the default one) or Impulse. If your example is correct by calling it on just 1 frame, then I don't know the difference between those 2 force modes... http://docs.unity3d.com/Documentation/ScriptReference/Force$$anonymous$$ode.html
Ah ok I see what you mean, that is a bit confusing.
The code i use is this:
obj.rigidbody.AddForce(Vector3(0, 0.2, 2) * force, 0);
And I call this only once when a keyDown event is triggered.
The force is only applied over one frame and acts more like a "push" rather than a continues force that is being applied.
It gives a nice throw curve though.
Here I am using Force$$anonymous$$ode.Force, I have never tried Force$$anonymous$$ode.Impulse.
Not entirely sure what the difference is there, but I'd probably just create a test scene and try them both out..
If using Force$$anonymous$$ode.Force in 1 frame is correct, I think it's just a matter of testing to see which mode fits the best. Anyway, delta ti$$anonymous$$g is not necessary using either mode, isn't it?
When only applying the force over one frame you won't need to use deltaTime.
http://docs.unity3d.com/Documentation/ScriptReference/Rigidbody.AddForce.html?from=Force$$anonymous$$ode
The documentation says to use FixedUpdate() rather than Update() when applying force over several frames though.
So that should effectively do the same as multiplying by deltaTime.
Here is something interesting:
http://docs.unity3d.com/Documentation/ScriptReference/ConstantForce.html
"Rigidbody.AddForce applies a force to the Rigidbody only for one frame, thus you have to keep calling the function. ConstantForce on the other hand will apply the force every frame until you change the force or torque to a new value."