首页 > 代码库 > 鼠标的拖动实现物体的上下左右移动
鼠标的拖动实现物体的上下左右移动
private GameObject center;
private float rotateSpeed = 40f;
private float rotateZ = 0;
private float minR = -15f;
private float maxR = 15f;
private float sensitity = 50f;
void Start()
{
//获取物体
center = GameObject.Find("center");
}
//鼠标拖动事件写在Update()中
if (Input.GetMouseButton(0))
{
float rotateY = center.transform.localEulerAngles.y - Time.deltaTime * sensitity*Input.GetAxis("Mouse X");
if (rotateY>=0&&rotateY<=180)
{
rotateZ += Time.deltaTime * sensitity * Input.GetAxis("Mouse Y");
}
else
{
rotateZ -= Time.deltaTime * sensitity * Input.GetAxis("Mouse Y");
}
//控制上下移动的范围
rotateZ = Mathf.Clamp(rotateZ, minR, maxR);
center.transform.eulerAngles = new Vector3(0, rotateY, rotateZ);
}
鼠标的拖动实现物体的上下左右移动