“In most cases you should not modify the velocity directly” → Could you give me an example about setting velocity directly and indirectly ?
“Don’t set the velocity of an object every physics step, this will lead to unrealistic physics simulation” → It means we should not set velocity in FixedUpdated(), is it right?
The point of the physics simulation is to simulate objects interacting physically. If you constantly set the velocity of an object, the physics simulation’s results are being overwritten every frame.
Generally the idea is to use AddForce, and then let the physics simulation determine the resulting velocity for you.
If you need your object to stop on a dime, you could set velocity=0 for instance.
If you want to make an unrealistic physics simulation, you could do your own math and set velocity directly for situations where it would be inconvenient to work with the built-in simulation to achieve your desired results.
Is a “proper” examples of when to manually set the velocity, when a character hits the ground in an unrealistic (tile based, no real physics) platformer, or when a character runs into a wall and you don’t want it to bounce, but simply stop up against it (set position to the wall, set velocity to 0)?
Assuming you were working on a bullet hell - would applying a force for the initial fire of a bullet be the proper route, assuming no other forces were being applied (assuming a constant velocity, until collision)?
I’ve been contemplating these uses of the physics engine vs not for awhile now, curious to see / hear real game applications.
Well your character won’t bounce off a wall unless the collider has a bouncy physics material, but yes, if you want your character to unrealistically stop, setting velocity once is a reasonable use case.
There’s almost no difference between setting velocity vs. AddForce to the bullet example if there are no other outside forces. I believe the only other factor is mass, which AddForce takes into account. However it’s best to be consistent in your api usage, just in case down the line, you do include outside forces that could alter the launch velocity.
Personally, I avoid setting velocity as much as possible to avoid conflicting code. AddForce calls will blend in the physics sim, so it’s pretty safe to use in many places. I have in the past written my own simple physics for an unrealistic game, using a rigidbody for collisions only, but maintaining my own internal velocity and accelerations etc. That is akin to setting velocity manually too.
Gotcha, that makes sense. I thought of outside forces, I just didn’t want to complicate my question, but staying consistent is reason enough on its own. As for AddForce vs. setting velocity manually, it’s more or less identical to my previous experience with getters and setters (AS3), where you want to go through the function, rather than manipulate public variables directly, for various reasons, debugging being a solid one.
I’m still trying to decide how to go about my own 2D platforming code, realistic, or completely simulated, but I’ll figure it out eventually.