- Home /
Why use AddForce rather than modify velocity?
I've looked all over, and while I've seen comments about modifying rigidbody.velocity being a bad idea, I haven't really noticed why. Assuming the velocity change is calculated properly, is there any difference between using it and using addForce?
If I add a force that will result in a velocity change of 1, or simply modify the velocity by 1, is there any difference in what actually happens in terms of gameplay and/or performance?
Answer by Bunny83 · Dec 05, 2012 at 12:57 AM
No, there's no difference. AddForce has actually different modes:
http://docs.unity3d.com/Documentation/ScriptReference/ForceMode.html
They all result in an immediate velocity change, but with a different scale.
Force and Impulse are divided by the objects mass, acceleration and velocitychange are not.
Force(`m*kg/s²`) and acceleration(`m/s²`) are computed so the value represents a change per second when applied every physics frame (in FixedUpdate). Velocitychange and Impulse are in `Δm/s` or `Δm*kg/s`
This all assumes the guideline that mass is in kg and 1 unit is 1 meter.
Answer by Iammnhamza · Sep 02, 2017 at 01:21 PM
Rigidbody Velocity and Rigidbody Addforce are two confusing functions in Unity 3D and often beginners fail to understand their difference. In This Article, we are going to discuss the difference between RigidBody.velocity and RigidBody.addforce.
In both cases, whether it is Addforce or velocity function we are going to use the term force to explain them.
When we are using Rigidbody.velocity , then, in that case, we are adding force to our object but this force will only move the object unless and until we keep applying force.
For example
body.velocity= new Vector3(0,0,5);
Let say you have added 5f in z position and that force will be added when you'll press W key. So When you will press W key the object will instantly start at the speed of 5. It's like the same if you are adding force using this function on a car, that car will start at the speed of 5.
body.velocity= new Vector3(0,0,200);
And if you let say change the value to 200 and then after saving press W. Car will start running at the speed of 200 from starting which is not possible in real world.
Now If you talk about Rigidbody.addforce
By Continuing our Car example. if you add the force of 200 to car
body.addforce(0,0,200);
The Car will not start moving at the speed of 200 if we press W but it starts from slow and then increases its speeds and stops according to the value of Drag.
Rigidbody.addforce starts slow and then speed up, just like you are dragging a heavy table, first you will start pushing that table, the table will just move a little bit from its position but if you keep pushing that table it will starting moving & if you leave that table it will cover some distance depending on surface and that's the same rigidbody.addforce do.
You can use Rigidbody.velocity where you just want to move your object to react instantly like player jump & the result of that force will vanish just after the jump and you can use Rigidbody.addforce where you need slow start and then the continuous movement like a rocket. If you use Rigidbody.addforce in jump , Player/Object will remain in space for a while and then will come back to ground .
You're confusing a 1-time change with a change over time, and also confusing = and += for velocity.
Using velocity+= every frame is the same as using AddForce every frame - gradually increasing speed. But you can use a 1-time large AddForce to instantly change speed to a large value. The 200 vs. 5 is merely due to the default addforce mode, which divides inputs by about 60, just because (see various Qs over the forcemodes.)
that's how i understand that difference . Its my point of view :)
Answer by Janibasha · Nov 27, 2018 at 08:51 AM
Rigidbody Velocity and Rigidbody Addforce are two confusing functions in Unity 3D and often beginners fail to understand their difference. In This Article, we are going to discuss the difference between RigidBody.velocity and RigidBody.addforce.
In both cases, whether it is Addforce or velocity function we are going to use the term force to explain them.
When we are using Rigidbody.velocity , then, in that case, we are adding force to our object but this force will only move the object unless and until we keep applying force.
For example
body.velocity= new Vector3(0,0,5);
Let say you have added 5f in z position and that force will be added when you'll press W key. So When you will press W key the object will instantly start at the speed of 5. It's like the same if you are adding force using this function on a car, that car will start at the speed of 5.
body.velocity= new Vector3(0,0,200);
And if you let say change the value to 200 and then after saving press W. Car will start running at the speed of 200 from starting which is not possible in real world.
Now If you talk about Rigidbody.addforce
By Continuing our Car example. if you add the force of 200 to car
body.addforce(0,0,200);
The Car will not start moving at the speed of 200 if we press W but it starts from slow and then increases its speeds and stops according to the value of Drag.
Rigidbody.addforce starts slow and then speed up, just like you are dragging a heavy table, first you will start pushing that table, the table will just move a little bit from its position but if you keep pushing that table it will starting moving & if you leave that table it will cover some distance depending on surface and that's the same rigidbody.addforce do.
You can use Rigidbody.velocity where you just want to move your object to react instantly like player jump & the result of that force will vanish just after the jump and you can use Rigidbody.addforce where you need slow start and then the continuous movement like a rocket. If you use Rigidbody.addforce in jump , Player/Object will remain in space for a while and then will come back to ground .
This is plagiarized from $$anonymous$$ Numan Hamza on StackExchange. It's also wrong -- +=velocity can easily be used for a gradual increase.
"+=velocity can easily be used for a gradual increase." -- While true, this is handled by the programmer in a script. It's not the fundamental difference between the two. I can write a transform algorithm to act like either velocity or addforce, but that's really beside the point.
Read Bunny's answer. There isn't a fundamental difference between the two. That's the whole point. But they're different from directly changing transform. AddForce/velocity uses the physics system, which makes the object move automatically.
Answer by mmcguffi · Dec 05, 2012 at 01:28 AM
one thing i can think of is that modifying the velocity would instantaneously set that velocity yea?
i suppose you would achieve a similar effect if the object in question was as rest, but if you were to apply this to a moving object per se, addforce would have to oppose the other force first while velocity would just immediately change it
tl;dr addforce would display more accurate physics
It actually makes no difference. The Rigidbody has only those state variables:
velocity, angularVelocity and position, rotation (which are shared with the transform). No other state information is saved in the Rigidbody. All actions are immediately applied to those values. So AddForceAtPosition for example can affect all 4 at the same time.
Actually, this is true. velocity change is instant, code-wise, but AddForce isn't. See http://answers.unity3d.com/questions/800443/why-does-writing-to-rigidbodyvelocity-after-addfor.html. Skip my partly-wrong answer (even though it's green, which means nothing here) and go to Bored$$anonymous$$ormon and Chris'.
But one thing to try, is have a Cube race using the same velocity and AddForce's. I'd guess it's still a tie.