Unity 鼠标拖曳物件旋转

当你希望可以透过鼠标拖曳物件旋转可以利用下方的代码

public class OnMouseDrage : MonoBehaviour {

    
    [Header("旋转速度")]
	public float rotSpeed = 4f;
    [Header("旋转开关")]
    public bool RoteX = true;
    public bool RoteY = true;


    void OnMouseDrag()
	{
        if(RoteX == true)
        {
            float rotX = Input.GetAxis("Mouse X") * rotSpeed * Mathf.Deg2Rad;
            transform.RotateAround(Vector3.up, -rotX);
        }

        if(RoteY == true)
        {
            float rotY = Input.GetAxis("Mouse Y") * rotSpeed * Mathf.Deg2Rad;
            transform.RotateAround(Vector3.right, rotY);
        }
       
	}

}

 

你可能感兴趣的:(Unity,C#)