通过鼠标拖动或在Unity中触摸在X和Y轴上旋转对象?

时间:2019-08-13 11:55:33

标签: c# unity3d

我有一个脚本,可以在Y轴上旋转对象,但是如何在X轴上旋转对象呢?例如,如果鼠标拖动/触摸向左/向右移动,则应沿Y方向旋转;如果向上/向下移动,则应沿X方向旋转。

public class Rotate : MonoBehaviour
{
    private float _sensitivity;
    private Vector3 _mouseReference;
    private Vector3 _mouseOffset;
    private Vector3 _rotation;
    private bool _isRotating;

    void Start()
    {
        _sensitivity = 0.4f;
        _rotation = Vector3.zero;
    }

    void Update()
    {
        if (_isRotating)
        {
            // offset
            _mouseOffset = (Input.mousePosition - _mouseReference);

            // apply rotation
            _rotation.y = -(_mouseOffset.x + _mouseOffset.y) * _sensitivity;

            // rotate
            transform.Rotate(_rotation);

            // store mouse
            _mouseReference = Input.mousePosition;
        }
    }

    void OnMouseDown()
    {
        // rotating flag
        _isRotating = true;

        // store mouse
        _mouseReference = Input.mousePosition;
    }

    void OnMouseUp()
    {
        // rotating flag
        _isRotating = false;
    }

}

0 个答案:

没有答案