- Home /
Unity 4.3 - 2D - Velocity based on rotation
Hi,
i want to build a space ship game in which you rotate your ship by using the mouse and i want to apply a velocity based on the current rotation of the sprite.
First i set a rotation with the following line of code:
gameObject.transform.rotation = Quaternion.Euler(0f, 0f, angleInDegrees);
That works well, but now i have no clue how to apply a 'force' to the gameobject in the correct direction.
And i was also wondering why the 'AddForce' method does not work with the rigidbody2D, so i want to change it using 'gameObject.rigidbody2D.velocity'.
Would be nice if you guys could help me out.
thanks Avoider
Comment
Best Answer
Answer by Avoider · Nov 18, 2013 at 05:38 PM
Ok, i have found what is the problem. I used an vector2 as multiplication, but it has to be an vector3.
Here is my code:
Vector3 velocity = this.transform.rotation * Vector3.up;
gameObject.rigidbody2D.AddForce(velocity);
Thank you guys Avoider