2
votes

I made a rigid body controller that is based on controlling the velocity of the object inside of fixedupdate. Only when i finished did i realize that this link says that you SHOULDNT used rigidbody.velocity inside of fixed update because it causes undesirable behavior.

Is there a mathematical equation or some sort of solution I can use so that i can use rigidbody.AddForce inside of fixed update to calculate a force per frame that will lead to the Vector3 Velocity that already has the behavior calculations?

Just to clarify, I have the velocity, i need the force to call in Fixedupdate to reach/maintain that velocity. I'm not really sure exactly what kind of solution is needed so i apologize if this is a dumb question. I just don't know how i'm supposed to handle it.

Thanks!

1

1 Answers

3
votes

To calculate the amount of force you need to get to a velocity, you need to use the equation Ft=mV - mU where F = force, t = time, m = mass, V = desired velocity, and U = current velocity.

Assuming your starting velocity is 0, the equation will look something like this:

Vector3 force = (rigidbody.mass * desiredVelocity) / Time.fixedDeltaTime;

desiredVelocity should be a Vector3 of which direction you want the object to go.

If the object is already moving, use this:

Vector3 force = ((rigidbody.mass * desiredVelocity) - (rigidbody.mass * rigidbody.velocity)) / Time.fixedDeltaTime;