为什么当用鼠标旋转对象时,它朝相反的方向旋转?

时间:2019-06-22 20:07:34

标签: c# unity3d

public class Rotate : MonoBehaviour
{
    public float speed = 1.0f;


    private void Update()
    {
        transform.Rotate(new Vector3(Input.GetAxis("Mouse Y"), Input.GetAxis("Mouse X"), 0) * Time.deltaTime * speed);
    }
}

我想用鼠标平滑地旋转对象。

但是在这种情况下,如果我向右移动鼠标,它将使对象向左旋转,而如果向上移动鼠标,则将其向下旋转。

1 个答案:

答案 0 :(得分:0)

这就像我想要的那样工作:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MouseOrbit : MonoBehaviour
{
    public float speedH = 2.0f;
    public float speedV = 2.0f;

    private float yaw = 180.0f;
    private float pitch = 0.0f;

    private void Start()
    {
        Cursor.lockState = CursorLockMode.Locked;
    }

    void Update()
    {
        yaw -= speedH * Input.GetAxis("Mouse X");
        pitch -= speedV * Input.GetAxis("Mouse Y");

        transform.eulerAngles = new Vector3(pitch, yaw, 0.0f);
    }
}