鼠标3D视角
using UnityEngine;
using System.Collections;
public class MyMouseLook : MonoBehaviour {
// 旋转枚举类型
public enum RotationAxes
{
MouseXAndY = 0, // 360° 全景
MouseX = 1, // 360° 左右
MouseY = 2, // 90° 上下
MouseScrollWhell = 3 // 视野拉伸
}
public RotationAxes axes = RotationAxes.MouseXAndY;
public float xSpeed = 8f; // Y 轴旋转速度
public float ySpeed = 4f; // X 轴旋转速度
public float wSpeed = 15f; // 视野拉伸速度
public float minX = -360f; // 左右旋转角度范围
public float maxX = 360f;
public float minY = -30f; // 上下旋转角度范围
public float maxY = 60f;
public float minFov = 10f; // 视野拉伸范围
public float maxFov = 30f;
float rotationY = 0f;
//public float anglex;
void Start ()
{
//anglex = transform.localEulerAngles.y;
if (GetComponent<Rigidbody> ())
{
GetComponent<Rigidbody>().freezeRotation = true;
}
}
void Update ()
{
if (Input.GetMouseButton (1))
{
if( axes == RotationAxes.MouseXAndY )
{
float rotationX = transform.localEulerAngles.y + Input.GetAxis( "Mouse X" ) * xSpeed;
rotationY += Input.GetAxis( "Mouse Y" ) * ySpeed;
rotationY = Mathf.Clamp( rotationY, minY, maxY );
transform.localEulerAngles = new Vector3( -rotationY, rotationX, 0 );
}
else
{
if( axes == RotationAxes.MouseX )
{
transform.Rotate( 0, Input.GetAxis( "Mouse X " ) * xSpeed, 0 );
}
else
{
rotationY += Input.GetAxis( "Mouse Y" ) * ySpeed;
rotationY = Mathf.Clamp( rotationY, minY, maxY );
transform.localEulerAngles = new Vector3( -rotationY, transform.localEulerAngles.y, 0 );
}
}
}
float fov = Camera.main.fieldOfView;
fov += -1 * Input.GetAxis ("Mouse ScrollWheel") * wSpeed * Time.deltaTime;
fov = Mathf.Clamp (fov, minFov, maxFov);
Camera.main.fieldOfView = fov;
}
}