unity3d获取鼠标3个键的响应如下:
鼠标左键GetMouseButton(0)
鼠标右键GetMouseButton(1)
鼠标中键GetMouseButton(2)
知道了鼠标的响应要做到按下右键再转动摄像机就简单了。
建好一工程UNITY自带的脚本MouseLook.cs中已经把通过鼠标移动旋转摄像机的功能做好了。想要按下右键才再转动摄像机。只要在Update()的相应位置加上Input.GetMouseButton(1)就可以了。
代码如下:
void Update () { if(Input.GetMouseButton(1)) { if (axes == RotationAxes.MouseXAndY) { // Read the mouse input axis rotationX += Input.GetAxis("Mouse X") * sensitivityX; rotationY += Input.GetAxis("Mouse Y") * sensitivityY; rotationX = ClampAngle (rotationX, minimumX, maximumX); rotationY = ClampAngle (rotationY, minimumY, maximumY); Quaternion xQuaternion = Quaternion.AngleAxis (rotationX, Vector3.up); Quaternion yQuaternion = Quaternion.AngleAxis (rotationY, Vector3.left); transform.localRotation = originalRotation * xQuaternion * yQuaternion; } else if (axes == RotationAxes.MouseX) { rotationX += Input.GetAxis("Mouse X") * sensitivityX; rotationX = ClampAngle (rotationX, minimumX, maximumX); Quaternion xQuaternion = Quaternion.AngleAxis (rotationX, Vector3.up); transform.localRotation = originalRotation * xQuaternion; } else { rotationY += Input.GetAxis("Mouse Y") * sensitivityY; rotationY = ClampAngle (rotationY, minimumY, maximumY); Quaternion yQuaternion = Quaternion.AngleAxis (rotationY, Vector3.left); transform.localRotation = originalRotation * yQuaternion; } } }