Unity3D总结记录(十一) Unity中鼠标旋转三维物体

Unity中,可使用如下方法对三维世界中的物体,进行鼠标拖拽旋转,用于360度,查看模型,展示模型使用。

using System.Collections;

using System.Collections.Generic;
using UnityEngine;
public class trunObject : MonoBehaviour {


    public Transform obj;   //旋转物体
    public float speed = 2;  //旋转速度


    private bool _mouseDown = false;
    private bool drag = false;   //判断鼠标是否在被旋转物体上按下


    void Update()
    {
        if (Input.GetMouseButtonDown(0)&&drag)
            _mouseDown = true;
        else if (Input.GetMouseButtonUp(0))
            _mouseDown = false;

        if (_mouseDown)
        {
            float fMouseX = Input.GetAxis("Mouse X");
            float fMouseY = Input.GetAxis("Mouse Y");
            obj.Rotate(Vector3.up, -fMouseX * speed, Space.World);
            obj.Rotate(Vector3.right, fMouseY * speed, Space.World);
        }
    }
    void OnMouseDown()
    {
        Debug.Log("Down");
        drag = !drag;


    }
    void OnMouseUp()
    {


        Debug.Log("UP");
        drag = !drag;
    }


}

猜你喜欢

转载自blog.csdn.net/weixin_39591280/article/details/80855290