ForceMode.VelocityChange not behaving as expected
I understood from the Doc, that these 2 lines should work identical:
// Works fine: rigidbody.velocity += velocityChange; // Bugs out and sets Velocity to ridicoulously high, seemingly random Values: rigidbody.AddForce(velocityChange, ForceMode.VelocityChange);
However one works and one doesn't. Anybody knows something about this?
Complete Script: https://pastebin.com/0NUSLAQL
Answer by Edy · Jun 16, 2020 at 06:22 PM
It's not the same.
Rigidbody.velocity overrides the velocity that comes from the physics calculations.
AddForce in ForceMode.VelocityChange applies the velocity change during the internal physics calculations. The resulting velocity also accounts for other forces and accelerations (i.e. gravity) applied during the physics step.
Both methods are not equivalent, therefore the results are different.
@Edy So even though rigidbody.velocity += something only increments the value, the physics update would ignore rigidbody.velocity for this physics frame? I should add, that the velocity-changing code is in the Update() ins$$anonymous$$d of the FixedUpdate() method.
All physics-related operations should be performed in FixedUpdate, otherwise the results are dependent on the frame rate. The physics steps uses your velocity, but as far as I can tell the calculation is precise when the velocity is calculated out of all the factors affecting the rigidbody. Internally, each physics step is subdivided in smaller steps (substeps) for a more precise calculation.
The framerate dependency can easily be compensated by multiplying the right things with Time.deltaTime. I use the Update() method, because Update() turned out to deliver smoother results when processing user input. The alternative would be to query the user input in Update(), set flag variables for which keys are pressed and process the key presses in FixedUpdate(). But I prefer multiplying by Time.deltaTime ins$$anonymous$$d of making 5-10 new variables (or an array).
To give a little more context of what I am trying to do:
Vector3 direction = (transform.right * Input.GetAxis("Horizontal") + transform.forward * Input.GetAxis("Vertical"));
float speed = movementSpeed;
[... apply some modifications to speed ...]
Vector3 velocityChange = ((direction * speed) - rigidbody.velocity);
rigidbody.AddForce(velocityChange, Force$$anonymous$$ode.VelocityChange);
AT$$anonymous$$ this code should set the velocity to the direction deter$$anonymous$$ed by user input. This should have the same effect as rigidbody.velocity = direction * speed and therefore indeed override all other physics. The reason I am doing this is, that I want to have this simple step working, before moving on to something like this:
Vector3 velocityChange = ((direction.normalized * 0.1f * Time.deltaTime) - rigidbody.velocity);
rigidbody.AddForce(velocityChange, Force$$anonymous$$ode.VelocityChange);
With that code I should get a linear acceleration ins$$anonymous$$d of instant setting of a target velocity and other physics forces should work fine with that. However I don't want to go that far, as long as the basic instant setting of the velocity is not working properly.
Your answer
![](https://koobas.hobune.stream/wayback/20220613001031im_/https://answers.unity.com/themes/thub/images/avi.jpg)