AddRelativeForce with a rigidbody2D ?

Hello,

Before unity 4.3 update, I was using AddRelativeForce to make my sprite go “forward” with this line of code :

`rigidbody.AddRelativeForce(Vector3.up * Time.deltaTime * SPEED);`

Is there an easy way to do the same with a rigidbody2D (without unsing sin/cos) ?

I'm also wondering this. It seems like the 2D physics is missing many things the 3D physics has. I hope they are going to be implemented soon...

1 Answer

1

In this particular case, you should be able to use

rigidbody2d.AddForce(transform.up * Time.deltaTime * SPEED);

‘transform.up’ is the relative up transformed into world space. Alternately you could do:

ridgidbody2d.AddForce(transform.TransformDirection(Vector3.up) * Time.deltaTime * SPEED);

Transform.TransformDirection() takes a local position and transforms it into world space. This is a more general solution that works for any local vector, where by the ‘transform.up’, ‘transform.right’, ‘transform.down’ only work for directions that are axes aligned.