1. 程式人生 > >鼠標的拖動實現物體的上下左右移動

鼠標的拖動實現物體的上下左右移動

sta button locale transform min atime enter ans 實現

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

}

鼠標的拖動實現物體的上下左右移動