- Home /
How can I move an object a specific distance using AddForce?
I'm trying to move a Rigid body a specific distance every frame and I would like it to move by that distance, plus any other physics calculations that would affect it (Which is why I don't want to turn on IsKinematic). I have the distance I would like it to travel stored in a Vector3 already and I can see that there are ForceModes that allow me to move the Rigidbody while ignoring the mass of my object, but how do I get it to move the distance I would like?
What I've tried:
void FixedUpdate()
{
Vector3 Offset = Vector3(0f, 0f, 0.25f);
mCollider.AddForce(Offset * Time.fixedDeltaTime, ForceMode.VelocityChange);
}
Answer by aldonaletto · Sep 10, 2011 at 03:18 AM
It's better to set rigidbody.velocity instead - a force generates an acceleration dependent on the mass that will increase the velocity linearly (if the force is constant), and this complicate things a lot. Setting the velocity produces the same effect we get with CharacterController using Move or SimpleMove. The docs say that this can produce "artificial" results - but that's exactly what you want: artificially predictable results!
Answer by NeilM0 · Sep 10, 2011 at 04:37 PM
I think I just solved it. The trick is to use MovePosition like this:
void FixedUpdate()
{
mCollider.MovePosition(rigidbody.position + Offset);
}
Just as long as you don't hit a slope, it moves the character the correct amount.