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;
}
}