【发布时间】:2015-06-18 19:05:30
【问题描述】:
我正在使用 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();
}
}
}
非常感谢任何帮助,因为我对此感到很困惑。
提前致谢!
【问题讨论】: