Perry Battles
Perry Battles

Reputation: 31

Rotation behaving strangely between 90 and 270 degrees in Unity

I'm making a 3D space shooter in Unity and I've run into a slight issue.

When I rotate my ship on the X axis, its rotation does the following: (a) if I rotate downward, rotation will increase (passing 360 degrees) to 90, but then come back down again after it reaches 90, even though I'm still rotating downward. (B) If I rotate upward, rotation will decrease until it reaches 270, then begin increasing again even though I'm still rotating upward -- it will, again, continue to increase until it passes 360 degrees and reaches 90, after which point it will decrease again.

The code I'm using to move/rotate the ship is as follows:

if (Input.GetKey(KeyCode.Space)) // Fly forward.
        {
            GetComponent<Rigidbody>().velocity += transform.TransformDirection(Vector3.forward * Time.deltaTime * flightSpeed);

            audioSource.PlayScheduled(0.679);
        }
        else // If the space key is not being pressed, bring the ship's velocity closer to 0.
        {
            audioSource.Stop();

            /*
            if (rigidbody.velocity.x < 0) { rigidbody.velocity.Set(rigidbody.velocity.x + 1, rigidbody.velocity.y, rigidbody.velocity.z); }
            if (rigidbody.velocity.y < 0) { rigidbody.velocity.Set(rigidbody.velocity.x, rigidbody.velocity.y + 1, rigidbody.velocity.z); }
            if (rigidbody.velocity.z < 0) { rigidbody.velocity.Set(rigidbody.velocity.x, rigidbody.velocity.y, rigidbody.velocity.z + 1); }

            if (rigidbody.velocity.x > 0) { rigidbody.velocity.Set(rigidbody.velocity.x - 1, rigidbody.velocity.y, rigidbody.velocity.z); }
            if (rigidbody.velocity.y > 0) { rigidbody.velocity.Set(rigidbody.velocity.x, rigidbody.velocity.y - 1, rigidbody.velocity.z); }
            if (rigidbody.velocity.z > 0) { rigidbody.velocity.Set(rigidbody.velocity.x, rigidbody.velocity.y, rigidbody.velocity.z + 1); }
            */
        }

        if (Input.GetKeyDown(KeyCode.LeftShift)) // Full stop.
        {
            GetComponent<Rigidbody>().velocity = new Vector3(0, 0, 0);
            GetComponent<Rigidbody>().angularVelocity = new Vector3(0, 0, 0);
        }
        if (Input.GetKey(KeyCode.W)) // Pitch forward.
        {
            transform.Rotate(new Vector3(2, 0, 0));
        }
        if (Input.GetKey(KeyCode.S)) // Pitch back.
        {
            transform.Rotate(new Vector3(-2, 0, 0));
        }
        if (Input.GetKey(KeyCode.D)) // Rotate right.
        {
            transform.Rotate(new Vector3(0, 2, 0));
        }
        if (Input.GetKey(KeyCode.A)) // Rotate left.
        {
            transform.Rotate(new Vector3(0, -2, 0));
        }
        if (Input.GetKey(KeyCode.Q)) // Roll left.
        {
            transform.Rotate(new Vector3(0, 0, 2));
        }
        if (Input.GetKey(KeyCode.E)) // Roll right.
        {
            transform.Rotate(new Vector3(0, 0, -2));
        }
        if (Input.GetKeyDown(KeyCode.Return))
        {
            if (smartBombCount > 0 && GameObject.FindGameObjectsWithTag("Asteroid_Gigantic").Length == 0)
            {
                UseSmartBomb();
            }
            else
            {
                AudioSource.PlayClipAtPoint(buttonPress_denial, transform.position);
            }
        }
        if (Input.GetMouseButtonDown(0))
        {
            // If the touch controls are either not enabled or, if they are, if the mouse is not over a GUI button, fire.
            if (!gameManagerScript.GetTouchControlsEnabled()
                || (gameManagerScript.GetTouchControlsEnabled() && !MouseOverGUIButton()))
            {
                // Play the audio clip of the ship firing its lasers.
                AudioSource.PlayClipAtPoint(fire, transform.position);

                // Fire a number of shots based on which shooting upgrades this ship has, if any.
                if (upgrades.Contains("Triple Shot"))
                {
                    SpawnShot(-2.25f);
                    SpawnShot();
                    SpawnShot(2.25f);
                }
                else if (upgrades.Contains("Double Shot"))
                {
                    SpawnShot(-1.5f);
                    SpawnShot(1.5f);
                }
                else
                {
                    SpawnShot();
                }
            }
        }

Any help is greatly appreciated, as I'm quite puzzled with this.

Thanks in advance!

Upvotes: 0

Views: 1743

Answers (1)

C-JARP
C-JARP

Reputation: 1088

Reading this article will enlighten you.

http://answers.unity3d.com/questions/240854/transformrotate-in-x-axis.html

Also this may help you with that rotation.

http://docs.unity3d.com/ScriptReference/Transform-rotation.html

Upvotes: 0

Related Questions