【问题标题】:unity rotate object on x-axis when moving mouse up / down向上/向下移动鼠标时,统一在 x 轴上旋转对象
【发布时间】:2018-06-06 22:39:04
【问题描述】:

当我向上或向下移动鼠标时,我想在 x 轴上旋转一个对象(向上移动鼠标时增加 x 旋转,向下移动鼠标时减少)。

但我不知道该怎么做。

我试过这个脚本:

public float mouseSensitivity = 100.0F;
public float clampAngle = 80.0F;

float rotX = 0.0F, rotY = 0.0F;

void Update()
{
    // Mouse Look
    float mouseX = Input.GetAxis("Mouse X");
    float mouseY = Input.GetAxis("Mouse Y");

    rotX += mouseY * mouseSensitivity * Time.deltaTime;
    rotY += mouseX * mouseSensitivity * Time.deltaTime;

    rotX = Mathf.Clamp(rotX, -clampAngle, clampAngle);

    Quaternion localRotation = Quaternion.Euler(rotX, 0, 0.0F);
    transform.rotation = localRotation;
}

但这也控制 y 轴上的旋转。我在 y 轴上也有旋转,但它们是通过键盘输入控制的:

if (Input.GetKey(KeyCode.W) && !Input.GetKey(KeyCode.S))
    transform.Translate(Vector3.forward * Time.deltaTime * moveSpeed);
else if (Input.GetKey(KeyCode.S) && !Input.GetKey(KeyCode.W))
    transform.Translate(Vector3.forward * Time.deltaTime * -moveSpeed);

if (Input.GetKey(KeyCode.A) && !Input.GetKey(KeyCode.D))
    transform.Rotate(Vector3.up * Time.deltaTime * -rotateSpeed, Space.World);
else if (Input.GetKey(KeyCode.D) && !Input.GetKey(KeyCode.A))
    transform.Rotate(Vector3.up * Time.deltaTime * rotateSpeed, Space.World);

我希望鼠标向上/向下只控制 x 轴上的旋转。

如果有人可以帮助我,那就太好了!

【问题讨论】:

  • 除非您更关心鼠标移动的速度而不是距离,否则在将 GetAxis 用于鼠标轴的计算中不要包含 deltaTime。鼠标外观一般来说不是这样的!!

标签: c# unity3d


【解决方案1】:

无效更新() {

// Mouse Look

float mouseX = Input.GetAxis("Mouse X");

float mouseY = Input.GetAxis("鼠标 Y");

rotX = mouseY * mouseSensitivity;

rotY = mouseX * mouseSensitivity;

transform.rotation *= Quaternion.Euler(rotX, 0, 0.0f);

}

【讨论】:

    猜你喜欢
    • 2013-11-04
    • 1970-01-01
    • 2023-03-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多