1. Apply the effect first
2. Not much nonsense, let's go to the code
using UnityEngine;
using System.Collections;
public class DragRound : MonoBehaviour
{
//获取要旋转的模型
public Transform model;
//模型旋转的速度
public float rSpeed = 2;
//控制旋转的开始与停止
private bool mouseDown = false;
void Update()
{
//鼠标左键按下开始捕捉旋转,抬起即本轮旋转停止
if (Input.GetMouseButtonDown(0))
mouseDown = true;
else if (Input.GetMouseButtonUp(0))
mouseDown = false;
if (mouseDown)
{
//获取鼠标X轴方向上的移动量
float fMouseX = Input.GetAxis("Mouse X");
//获取鼠标Y轴方向上的移动量
float fMouseY = Input.GetAxis("Mouse Y");
model.Rotate(Vector3.up, -fMouseX * rSpeed, Space.World);
model.Rotate(Vector3.right, fMouseY * rSpeed, Space.World);
}
}
}
3. Add the Transfrom component of the object to be rotated to the model
4. Note: If the center point of your model is not in the center, you can create a new empty object, drag it to the center point of the model and use it as the service body of the model