在Unity中,旋转在90到270度之间表现奇怪

时间:2015-04-13 12:54:48

标签: unity3d rotation

我正在Unity制作3D太空射击游戏,但我遇到了一个小问题。

当我在X轴上旋转我的船时,它的旋转执行以下操作:(a)如果我向下旋转,旋转将增加(通过360度)到90,但是在它达到90后再次回落,甚至虽然我还在向下转。 (B)如果我向上旋转,旋转将减少直到达到270,然后即使我仍然向上旋转再次开始增加 - 它将再次继续增加,直到它经过360度并达到90度,之后它会再次减少。

我用来移动/旋转船只的代码如下:

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

非常感谢任何帮助,因为我对此感到非常困惑。

提前致谢!

1 个答案:

答案 0 :(得分:0)