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



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

当我在 X 轴上旋转我的船时,它的旋转执行以下操作:(a) 如果我向下旋转,旋转将增加到 90(通过 360 度),但在达到 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();
                }
            }
        }

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

提前感谢!

阅读这篇文章会启发你。

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

这也可能有助于您进行轮换。

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

最新更新