# Moving a game object along a 2d vector by knowing the Rotation in degrees (Unity)

I am hoping one of you fine fellows can help me again. Basically I have been making a game like Asteroids in Unity. But its in a 3d world space (pretty much not relevant except I am using X and Z axes, and the rotation angle is on the Y axis).

When the user presses "W" this adds some power towards the thrust. It also records the objects current rotation in transform.eulerAngles. (i've verified that the angle of y is correct. So a value of 90 is showing when my ship is facing right).

I have used Mathf.Cos/Sin and that angle value to move the object along X,Z axes.

But for some reason it isnt moving in the same direction as it should. -I tried it with some offsets like thrustAngle - 45 (or 90 etc) and it doesnt adjust it as expected. (reducing it be 45 seems to have a bigger impact than expected for example).

Here is the lines of code, have I got the Cos Sin part incorrect?

    void HandleInput(){
// activate weapon
if (Input.GetKey(KeyCode.Space))
{
FireWeapon();
}
// Rotate left
if (Input.GetKey(KeyCode.A))
{
transform.Rotate(0, -rotationSpeed * Time.deltaTime, 0);
}
// Rotate right
if (Input.GetKey(KeyCode.D))
{
transform.Rotate(0, rotationSpeed * Time.deltaTime, 0);
}
// activate thrusters
if (Input.GetKey(KeyCode.W))
{
thrustAngle = transform.eulerAngles.y -90;
if (currentThrust < maxThrust)
{
currentThrust += accel;
}
}
else
{
if (currentThrust > 0f)
{
currentThrust -= thrustDecay;
}
}
if (Input.GetKey(KeyCode.S))
{
if (currentThrust > 0)
{
currentThrust -= decelThrust;
}
}

transform.position += new Vector3(Mathf.Cos(thrustAngle), 0, Mathf.Sin(thrustAngle)) * currentThrust * Time.deltaTime;
Debug.Log("angle: " + thrustAngle);

if (currentThrust < 0f)
{
currentThrust = 0f;
}

}

• For clarification: Why have you used Sin & Cos to change the position of your ship? Nov 13, 2017 at 1:01
• Hi there, yes but I have just solved it this minute as it happens! Ill post the 2 things id done wrong now Nov 13, 2017 at 1:13
• @cmprogram sorry i misread your question. The reason if I have used the cos and sin is because I need the ship to thrust in the rotated direction the ship is facing. I've done it in the past but completely forgotten most of my stuff and got mixed up. The reason I am not using the rigidbody Addforce, or Translate is because when I do that and the ship is rotated by the player, it will curve rather than rotating around and keeping the same trajectory. This way the game is very much feeling like the original Asteroids now. Thanks for looking also pal Nov 13, 2017 at 1:19

        transform.position += new Vector3(Mathf.Sin(thrustAngle * Mathf.Deg2Rad), 0, Mathf.Cos(thrustAngle * Mathf.Deg2Rad)) * currentThrust * Time.deltaTime;