当向上/向下移动鼠标时,统一在x轴上旋转对象

时间:2018-06-05 19:36:16

标签: c# unity3d

当我向上或向下移动鼠标时,我想在x轴上旋转一个物体(当向上移动鼠标时增加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轴上的旋转。

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

1 个答案:

答案 0 :(得分:0)

void Update() {

// Mouse Look

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

float mouseY = Input.GetAxis(" Mouse Y");

rotX = mouseY * mouseSensitivity;

rotY = mouseX * mouseSensitivity;

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

}