Unity3D SpaceShooter 鼠标倒置移动
Unity3D SpaceShooter Mouse movement while upside down
我正在制作一个 3D space 游戏,您可以在其中驾驶一艘船。它是驾驶舱的第一个人。
我所有的动作都像一个魅力(W/S = 速度 up/down,Q/R = 旋转 left/right),鼠标移动船。
但是如果我使用我的旋转按钮,鼠标会停留 "stuck"。最简单的解释方法是:当我正常飞行时,向上移动鼠标会使相机向上和向下平移 - 向下平移。
但是如果我颠倒飞行,鼠标似乎不会注意到它并且以错误的方式(倒置)平移。就像它不知道我的轮换。在侧面它甚至更奇怪。鼠标在初始旋转上不断上升,而不是在新旋转上 我不知道如何解决。
我基本上想做的也是在按 Q 或 E 时转动鼠标方向
这是我的 rotate/mouse 脚本:
if (Input.GetKey("q"))
{
orientationPanel.transform.Rotate(-Vector3.left * 100f * Time.deltaTime);
cam.transform.Rotate(Vector3.forward * 100f * Time.deltaTime);
transform.Rotate(Vector3.forward * 100f * Time.deltaTime);
}
if (Input.GetKey("e"))
{
orientationPanel.transform.Rotate(Vector3.left * 100f * Time.deltaTime);
cam.transform.Rotate(-Vector3.forward * 100f * Time.deltaTime);
transform.Rotate(Vector3.forward * 100f * Time.deltaTime);
}
if (Input.GetMouseButtonDown(0) && Time.time > nextFire)
{
nextFire = Time.time + fireRate;
Fire();
}
float mouseX = Input.GetAxis("Mouse X");
float mouseY = -Input.GetAxis("Mouse Y");
rotY += mouseX * mouseSensitivity * Time.deltaTime;
rotX += mouseY * mouseSensitivity * Time.deltaTime;
rotX = Mathf.Clamp(rotX, -clampAngle, clampAngle);
rotZ = orientationPanel.transform.rotation.z;
Quaternion localRotation = Quaternion.Euler(rotX, rotY, rotZ);
transform.rotation = localRotation;
您需要始终在本地旋转您的飞船 space。如果您将鼠标向右移动 - 始终将船头向右移动。
Rotate(axis, angle, Space.Self);
例如
Rotate(Vector3.up, rotY, Space.Self);
Rotate(Vector3.right, rotX, Space.Self);
这也将修复 Q/E 的旋转问题,因为
Quaternion localRotation = Quaternion.Euler(rotX, rotY, rotZ);
transform.rotation = localRotation;
你基本上覆盖了用
所做的更改
transform.Rotate(Vector3.forward * 100f * Time.deltaTime);
transform.Rotate(Vector3.forward * 100f * Time.deltaTime);
我正在制作一个 3D space 游戏,您可以在其中驾驶一艘船。它是驾驶舱的第一个人。 我所有的动作都像一个魅力(W/S = 速度 up/down,Q/R = 旋转 left/right),鼠标移动船。
但是如果我使用我的旋转按钮,鼠标会停留 "stuck"。最简单的解释方法是:当我正常飞行时,向上移动鼠标会使相机向上和向下平移 - 向下平移。 但是如果我颠倒飞行,鼠标似乎不会注意到它并且以错误的方式(倒置)平移。就像它不知道我的轮换。在侧面它甚至更奇怪。鼠标在初始旋转上不断上升,而不是在新旋转上 我不知道如何解决。
我基本上想做的也是在按 Q 或 E 时转动鼠标方向
这是我的 rotate/mouse 脚本:
if (Input.GetKey("q"))
{
orientationPanel.transform.Rotate(-Vector3.left * 100f * Time.deltaTime);
cam.transform.Rotate(Vector3.forward * 100f * Time.deltaTime);
transform.Rotate(Vector3.forward * 100f * Time.deltaTime);
}
if (Input.GetKey("e"))
{
orientationPanel.transform.Rotate(Vector3.left * 100f * Time.deltaTime);
cam.transform.Rotate(-Vector3.forward * 100f * Time.deltaTime);
transform.Rotate(Vector3.forward * 100f * Time.deltaTime);
}
if (Input.GetMouseButtonDown(0) && Time.time > nextFire)
{
nextFire = Time.time + fireRate;
Fire();
}
float mouseX = Input.GetAxis("Mouse X");
float mouseY = -Input.GetAxis("Mouse Y");
rotY += mouseX * mouseSensitivity * Time.deltaTime;
rotX += mouseY * mouseSensitivity * Time.deltaTime;
rotX = Mathf.Clamp(rotX, -clampAngle, clampAngle);
rotZ = orientationPanel.transform.rotation.z;
Quaternion localRotation = Quaternion.Euler(rotX, rotY, rotZ);
transform.rotation = localRotation;
您需要始终在本地旋转您的飞船 space。如果您将鼠标向右移动 - 始终将船头向右移动。
Rotate(axis, angle, Space.Self);
例如
Rotate(Vector3.up, rotY, Space.Self);
Rotate(Vector3.right, rotX, Space.Self);
这也将修复 Q/E 的旋转问题,因为
Quaternion localRotation = Quaternion.Euler(rotX, rotY, rotZ);
transform.rotation = localRotation;
你基本上覆盖了用
所做的更改transform.Rotate(Vector3.forward * 100f * Time.deltaTime);
transform.Rotate(Vector3.forward * 100f * Time.deltaTime);