AddForceAtPosition & keep position within local space.

If I add a force using the AddForceToPosition method and that force is off-centre, then it causes both movement and torque to the rigidbody. What is the best way to maintain the position of that force as the rigidbody rotates?

public Vector3 pos=new Vector3(0,0,40);
public Vector3 forceXYZ=new Vector3(0,0,0);

void FixedUpdate () {

  rigidbody.AddForceAtPosition(forceXYZ, transform.position - pos);

 }

This simple code does not take into account the rotation of the object, so the force is always being applied at 40 world-space units from transform centre. I want to maintain the force’s position WITHIN the object’s local coordinate system.

This seems to be a fairly obvious application of this method, but I’ve been unable to find examples…

Cheers
Jason

2 Answers

2

First if you don’t want off-center force, then don’t use AddForceAtPosition(). Use Rigidbody.AddForce() or Rigidbody.AddRelativeForce(). If you need AddForceAtPosition() but need to keep it local, you will need to make the translation yourself. There is no local version of this function. See Transform.TransformDirection() and Transform.InverseTransformDirection() for making the conversion.

Hi & Thanks for the info.

I do want off-centre forces. I had an idea now to put a child object at the position of where I want the force to be, then use that object’s localPosition to do it. What do you think of that as a solution? Is it acceptable?