What is the difference between the four modes of ForceMode? I don’t understand how you can apply a force in multiple ways. For example, if I put my hand on a car and push with a specific amount of force for 5 frames, what’s the difference to hitting it with the same amount of force once each frame for 5 frames?
##History Lesson (not really)
Ok, so more than a year later I’ve had the need to come back and attempt to fully understand this problem (as I never really understood what the differences were). After staring at the formulae for a long time and not really getting anywhere, I decided to stare at the page linked by @jchangxu (thanks btw!). After a short while, one phrase jumped out at me: Think of it as ‘Force exerted per second’
Instantly I realised I understood the differences. It’s actually really simple, but everyone makes it sound so complicated that you assume it’s more complicated than it really is. Since no one has yet managed to explain it clearly, here’s my attempt from the viewpoint of someone who’s only just figured it out.
Incidentally: The official documentation could do with a better explanation since the current explanation in the docs is crappy and doesn’t actually tell you anything unless you do the maths yourself, and even then the maths alone doesn’t tell you everything about the function.
##tl;dr (…or “The Boy That Couldn’t Be Bothered”)
ForceMode.Force == Force per second
ForceMode.Impulse == Force per frame
There’s no actual difference in the way Force and Impulse apply the forces, only in how they calculate the amount of force to apply. If you do Force once every FixedUpdate for exactly one second, you will have applied the same amount of total force as doing Impulse on just one frame.
##Longer Explanation (for maximum understandings)
The real difference is that Force treats the force parameter as Newtons and Impulse treats the force parameter as Newton-seconds, but we don’t need to understand that to have a good understanding of how to use the function.
Force is calculated as:
Acceleration = Force * Time ^ 2 / Mass
Thus if the object has a mass of 2, you apply a force of 40 and the fixed timestep is left at the default of 0.02, it will look like this:
Acceleration = 40 * (0.02 * 0.02) / 2
Acceleration = 40 * 0.0004 / 2
Acceleration = 0.016 / 2
Acceleration = 0.008
So the object will gain 0.008 metres per second of velocity. In other words, it will accelerate by 0.008m/s.
Impulse is calculated as:
Acceleration = Force * Time / Mass
Thus if the object has a mass of 2, you apply a force of 40 and the fixed timestep is left at the default of 0.02, it will look like this:
Acceleration = 40 * 0.02 / 2
Acceleration = 0.8 / 2
Acceleration = 0.4
So the object will accelerate by 0.4m/s.
Notice that 0.4 is exactly 50 * 0.008 (50 being the default number of timesteps in a second). That’s because Force treats your force input as force per second, whereas Impulse treats your force input as force per timestep.
The other two ForceModes, Acceleration and VelocityChange, are even simpler because they leave mass out of the equation.
Acceleration is calculated as:
Acceleration = Force * Time ^ 2
Thus, regardless of the object’s mass, if you apply a force of 40 and the fixed timestep is left at the default of 0.02, it will look like this:
Acceleration = 40 * (0.02 * 0.02)
Acceleration = 40 * 0.0004
Acceleration = 0.016
So the object will accelerate by 0.16m/s.
VelocityChange is calculated as:
Acceleration = Force * Time
Thus, regardless of the object’s mass, if you apply a force of 40 and the fixed timestep is left at the default of 0.02, it will look like this:
Acceleration = 40 * 0.02
Acceleration = 0.8
So the object will accelerate by 0.8m/s.
And finally, it’s worth saying it again, just to be clear: no matter which ForceMode you choose, the force will not be applied on subsequent frames automatically. It will only be applied at the instant you call the function. The difference is simply in how it calculates how much force to apply, not in how it applies the force.
Okay, basic physics, Force = Mass * Acceleration
This means if you apply the same force to something with twice as much mass, the resulting change in acceleration is half as much.
Force Add a continuous force to the rigidbody, using its mass.
Does what the formula says. The force is applied evenly across the period of the frame which is how you normally push on something.
Acceleration Add a continuous acceleration to the rigidbody, ignoring its mass.
Just adds the force vector to the current object acceleration without dividing it by the mass
Impulse Add an instant force impulse to the rigidbody, using its mass.
As the description says, this is the entire force, divided by mass, added all at once. Its for simulating things that accelerate very suddenly like explosions
VelocityChange Add an instant velocity change to the rigidbody, ignoring its mass.
This is like Impulse but does not divide the force nby the object’s mass.
This is all explained on the web pages
I had difficulty understanding the 4 modes as well, especially what is the period of frames it applies to. This helped me.
The trick with ForceModes and all the physics talk is it wasn’t written for us. It’s from general physic simulators, where you don’t talk about framerates and don’t script, but you know Newtonian physics really well. When Unity borrowed the Physics Engine, those terms just came with it.
So the confusing part is two things. One, it seems like they’re using that strange Physics language because there’s no easier way to explain it (not true.) The other is how using it is pointlessly awkward. AddForce really is written in a bad way for regular game designers.
All AddForce ever does is change your speed, as a 1-time thing. Some of the options say stuff about “force over time” — we can ignore that. One ForceMode option divides by the framerate, another divides by how much you weigh. The other two divide by both, or neither.
That seems insane, to have options for math we can easily do ourselves. We all know if you want to add 10 per second, you add 10*Time.fixedDeltaTime
every frame in FixedUpdate(). If you want to divide by the weight, you put 10/rb.mass
. If you want to use both — it’s math, you just type it all out.
It happens that ForceMode.VelocityChange is the “just add it” option.
To speed up by 10/second we could put either of these in FixedUpdate:
rb.AddForce(10*Time.fixedDeltaTime, 0, 0, ForceMode.VelocityChange);
or
rb.AddForce(10, 0, 0, ForceMode.Acceleration);
// ForceMode.Acceleration multiplies by fixedDeltaTime
To a Physics guy, that star symbol and the Time-dot part is super-confusing. But they understand Acceleration means you’re only giving it a little fraction of 10 right now.
Or suppose this was in a non-Unity system with a drop-down number picker — you couldn’t even put an equation after the 10, and would have to use the right ForceMode.
The last mystery is why the default is the most complicated option. If you just use rb.AddForce(10,0,0)
it uses ForceMode.Force, which divides by the frameRate and your weight. It’s that way because it was designed by physicists —- that’s the real way pushes work.
They don’t understand that we don’t care about the real way.
Even the command-name is needlessly Physics-based. AddFORCE? Why not just AddVelocity, which is what it really does? Again, to a Physicist, using a Force is more natural. To us, giving just a speed is normal (like giving a boulder a speed of 10, no matter what it weighs.)
ForceMode.Acceleration is ForceMode.VelocityChange but
mutiplied by Time.fixedDeltaTime
ForceMode.VelocityChange just changes velocity by force parameter
ForceMode.Impulse changes velocity by force / mass
ForceMode.Force changes velocity by force / mass * Time.fixedDeltaTime
They should just write this as the unity documentation for ForceMode, hope you found it useful
Let’s talk a little about physics.
Equations:
Force = mass * acceleration, F = ma
Impulse = change in momentum, J = (delta) mv
Force = Impulse/time. = F = J/t
First, understand that adding an instant force or acceleration is mathematically impossible, so Unity applies the force/acceleration gradually over a time period for ForceMode.Force and ForceMode.Acceleration. This time period is normally 1 second. (meaning it technically adds an impulse… but enough of that)
Secondly, adding an instant impulse is mathematically possible because an impulse is a force applied over a time period. Same with ForceMode.VelocityChange.
Key:
m = rigidbody mass
Q = Vector3 number parameter
t = Force application duration, normally 1 second in Unity
ForceMode.Force adds the exact force on the object on that Frame. Therefore, the rigidbody the force is exerted on will accelerate at an acceleration of Q/m for t seconds. If you know your integrals… acceleration is the first derivative of velocity and so the velocity of the object will gradually change by (Q/m)*t = Q/m for 1 second.
ForceMode.Impulse adds an instant impulse on the object on that frame. An impulse changes the momentum of the object, and because the mass stays the same, the velocity of the object instantly changes by Q/m.
ForceMode.Acceleration directly adds an acceleration to the object, regardless of its mass. So the change in velocity of the object becomes Qt = Q for 1 second. Equivalent to applying a ForceMode.Force of m*Q.
ForceMode.Velocity instantly changes the velocity of the object by Q, regardless of its mass. Equivalent of applying an Impulse of m*Q.
Summary: the main difference is whether the amount is applied instantly or not. Impulse and VelocityChange are instant (therefore not realistic in real life), ForceMode.Force and ForceMode.Acceleration are gradual along 1 second (but are misleading in name).
My final answer
ForceMode.Force = Time.fixedDeltaTime * ForceMode.Impulse
ForceMode.Acceleration = Time.fixedDeltaTime * ForceMode.VelocityChange
ForceMode.Force = mass * velocityChange / timeChange
ForceMode.Impulse = mass * velocityChange / timeChange / Time.fixedDeltaTime
I created a cuboid in Unity and added rigidbody to the cuboid. Disabled gravity. Wrote a movable scrpit in Start() method to test the differences between these 4 forcemodes. Below is my test results and summary.
Equations
- ForceMode.VelocityChange = Number of Force
- ForceMode.Acceleration = Number of Force / PhysicsFramerate
- ForceMode.Impulse = Number of Force / Mass
- ForceMode.Force = Number of Force / Mass / PhysicsFramerate
Example
[ForceMultiplier= 200]
[Mass = 2 ]
[PhysicsFramerate = 50]
- ForceMode.VelocityChange = 200= 200
- ForceMode.Acceleration = 200 / 50 = 4
- ForceMode.Impulse = 200 / 2 = 100
- ForceMode.Force = 200 / 2 / 50 = 2
Explanation
- Any force was effective in 1 second only. So if you call the force in the Start() method, it only applies 1 second. But it will apply every second in the Update()/FixedUpdate() method.
- Unity locked the PhysicsFramerate to 50 frames per second (This is different from your application’s framerate), which does not change.
- If no other force applied(e.g. gravity), the speed will remain the same after the force applied.
- Time.deltaTime represents the actual frame time, which can be used to make the acceleration smoother.