Unity 拖动鼠标物体旋转

时间:2022-02-19 10:20:59

Unity 拖动鼠标物体旋转查看

通过拖动鼠标左键来旋转物体,基本原理是将鼠标拖动的距离转换为物体旋转的角度。假设Cube

using UnityEngine;
using System.Collections;

public class NewBehaviourScript : MonoBehaviour {

private Vector3 startPoint;
private Vector3 endPoint;
private int disToAngle=5;
void Update () {
if(Input.GetMouseButtonDown(0)){
startPoint=Input.mousePosition;
}
if(Input.GetMouseButton(0)){
endPoint=Input.mousePosition;
}
float dx=endPoint.x-startPoint.x;
float angle=dx/disToAngle;
this.transform.localEulerAngles=new Vector3(0,angle,0);

}
}