- Home /
Add Local Force with a Rigidbody2D
Hello, I was wondering how to add local force to a 2D rigidbody. Normally I would use AddRelativeForce but that does not exist for 2d rigidbodys only normal rigidbodys. Basicly I want this to be local.
gameObject.rigidbody2D.AddForce(Vector2.right*power);
Thanks!
Answer by Huacanacha · Nov 16, 2013 at 02:04 AM
Do you mean you want the force to be applied to the 'right' relative to the object? If so just use the objects transform.right vector:
gameObject.rigidbody2D.AddForce(gameObject.transform.right*power);
This should work for 2D as there is built in conversion from Vector3's to Vector2's, and you only lose the Z component which isn't relevant for 2D. Note that I'm still very new to Unity 2D so I'm not 100% sure :)
the z component of a vector in 2d is only relevant if you have 2 objects overlaping and you want to describe wich one should be rendered above the other. A z value that is smaller than another will render closer to the camera. note that this only applies if both objects are on the same sorting layer.
Answer by Pyrian · Jun 20, 2014 at 05:11 PM
Are you sure the Rigidbody2D.AddRelativeForce doesn't exist?
This was added along with many other 2d functions in 4.5
Your answer
![](https://koobas.hobune.stream/wayback/20220613122005im_/https://answers.unity.com/themes/thub/images/avi.jpg)