0
\$\begingroup\$

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

}
\$\endgroup\$
  • 1
    \$\begingroup\$ For clarification: Why have you used Sin & Cos to change the position of your ship? \$\endgroup\$ – cmprogram Nov 13 '17 at 1:01
  • \$\begingroup\$ Hi there, yes but I have just solved it this minute as it happens! Ill post the 2 things id done wrong now \$\endgroup\$ – Big T Larrity Nov 13 '17 at 1:13
  • \$\begingroup\$ @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 \$\endgroup\$ – Big T Larrity Nov 13 '17 at 1:19
0
\$\begingroup\$

Ok Ive fixed although I am puzzled as I thought that Cos was for the X axis. But I had two things incorrect: 1. I needed the angle to be in rads not degrees. 2. I had Cos and Sin the wrong way around.

Here is the code that got it working perfectly for me. (Sorry if I wasted anyones time)

        transform.position += new Vector3(Mathf.Sin(thrustAngle * Mathf.Deg2Rad), 0, Mathf.Cos(thrustAngle * Mathf.Deg2Rad)) * currentThrust * Time.deltaTime;
\$\endgroup\$
  • \$\begingroup\$ it wont let me accept this as an answer, but this line was all i actually needed to change. \$\endgroup\$ – Big T Larrity Nov 13 '17 at 1:15
  • 2
    \$\begingroup\$ You'll be able to accept the answer after a short waiting period. As for the flip of Sin & Cos, it's because you subtracted 90 from your yaw. Sin & Cos differ by a quarter turn of phase. Another way to think of it is you put Cos on whichever axis you want the object to point when the angle is 0. \$\endgroup\$ – DMGregory Nov 13 '17 at 1:21
  • \$\begingroup\$ Thanks @DMGregory I always get confused as heck with rotation and trig stuff. Strangely though, I deleted the -90 part before i fixed it and the reverse Cos Sin is what got it working (after already removeing the -90). At any rate I am happy i got it working so far. On to the next hitch soon :P \$\endgroup\$ – Big T Larrity Nov 13 '17 at 1:24
  • \$\begingroup\$ yes the ship faces upwards as 0. Which is techinally the Z axis (im not really using Y at the moment). That must explain what you are saying about putting Cos whereever the 0 starts. thanks i will try to remember this from now on \$\endgroup\$ – Big T Larrity Nov 13 '17 at 1:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.