unity中鼠标左键控制摄像机视角上下左右移动
enum RotationAxes { MouseXAndY, MouseX, MouseY }
RotationAxes axes = RotationAxes.MouseXAndY;
//@HideInInspector
float sensitivityX = 15;
//@HideInInspector
float sensitivityY = 15;
//private float minimumX = -360; 原文有此行但并未使用此变量
//private float maximumX = 360; 同上
float minimumY = -80;
float maximumY = 80;
private float rotationY = 0;
// Use this for initialization
void Start()
{
}
// Update is called once per frame
void Update()
{
if (Input.GetMouseButton(0))
{
if (axes == RotationAxes.MouseXAndY)
{
float rotationX = transform.localEulerAngles.y + Input.GetAxis("Mouse X") * sensitivityX;
rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
rotationY = Mathf.Clamp(rotationY, minimumY, maximumY);
transform.localEulerAngles = new Vector3(-rotationY, rotationX, 0);
}
else if (axes == RotationAxes.MouseX)
{
transform.Rotate(0, Input.GetAxis("Mouse X") * sensitivityX, 0);
}
else
{
rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
rotationY = Mathf.Clamp(rotationY, minimumY, maximumY);
transform.localEulerAngles = new Vector3(-rotationY, transform.localEulerAngles.y, 0);
}
}
}