鼠标的拖动实现物体的上下左右移动

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);  

}

posted @ 2017-07-06 16:11  U3DEngineer  阅读(1616)  评论(0编辑  收藏  举报