FixedUpade Update LateUpdate I’ve written to every update I can find, but why doesn’t the velocity always stay at zero? character always falls down in empty space, I know about gravity or kinematic, I’m just curious about the answer to this question
Gravity is constantly applied, which adjusts the the velocity, during one update all your code and the editor code is applied, you set velocity to 0, but the physics engine sets it to what it needs to be to fall. If you don’t want your character to fall disable gravity.
To clarify, Unity’s automatic/built-in physics steps are applied between FixedUpdate() cycles. This means that after you’ve set velocity to zero during one cycle, it will start moving again before your can modify it again.
// FixedUpdate() rb2D.velocity = Vector2.zero; // Between frames (simplified) rb2D.velocity += Physics2D.gravity * Time.fixedDeltaTime; // Update velocity rb2D.position += rb2D.velocity * Time.fixedDeltaTime; // Apply velocity // FixedUpdate() rb2D.velocity = Vector2.zero;
As has been mentioned in comments, you can prevent the effects of physics forces outside your control (gravity) by telling the Rigidbody not to be affected by said gravity.
For Rigidbody, this means setting Rigidbody.useGravity to false.
For Rigidbody2D, this means setting Rigidbody2D.gravityScale to 0.
(Both of these are configurable in their respective editors, as well)