- Home /
If I use AddForce twice in one update frame, with different forceModes on each, how does Unity handle it?
Say I have an object and I do
Vector3 forceToAdd = (1,1,1)
myRigidbody.AddForce(forceToAdd, ForceMode.Force);
myRigidbody.AddForce(forceToAdd, ForceMode.VelocityChange);
Does VelocityChange just override the ForceMode.Force? Or does it add the first force, and then alter the velocity after?
And what if instead of this, I do:
Vector3 forceToAdd = (1,1,1)
myRigidbody.AddForce(forceToAdd, ForceMode.Force);
myRigidbody.velocity = new Vector3(1,1,1);
Does this add the force to the object (moving it a little) and then set its velocity? What is the difference between using these 2 blocks of code?
Answer by Baste · Aug 24, 2015 at 10:24 PM
If you set the velocity, you override any forces before that. So the second example would simply discard the AddForce call.
The force modes changes the input for different simulation setups. I'm pretty sure that they simply scale them in some way. So this:
myRigidbody.AddForce(forceToAdd, ForceMode.Force);
myRigidbody.AddForce(forceToAdd, ForceMode.VelocityChange);
Probably turns into something along the lines of:
myRigidbody.velocity = myRigidbody.velocity + (forceToAdd * X) + (forceToAdd * Y)
where X is dependent on the mass of the object, and Y is not.
In testing, it seems like AddForce(x) 5 times in the same FixedUpdate call gives the same result as AddForce(5x) once.
Thanks, that was informative. Also, whats the difference between these two lines of code?
myRigidbody.AddForce(force, Force$$anonymous$$ode.VelocityChange);
and
myRigidbody.velocity = myRigidbody.velocity + (force/myRigidbody.mass)*time.DeltaTime;
(so basically v = v_0 + a*t)
Answer by sglindme · Aug 26, 2015 at 12:40 PM
Nevermind, figured it out. All of these lines do the same thing:
myRigidbody.AddForce(force * Time.deltaTime, ForceMode.VelocityChange);
myRigidbody.AddForce(force, ForceMode.Acceleration);
myRigidbody.velocity = myRigidbody.velocity + Time.fixedDeltaTime*force;
myRigidbody.AddForce(force * myRigidbody.mass, ForceMode.Force);
myRigidbody.AddForce(force * myRigidbody.mass * Time.fixedDeltaTime,ForceMode.Impulse);