公开变量
public float sensitivityMouse = 2f;
//按键控制相机移动的速度值
public float sensitivetyKeyBoard = 0.1f;
//鼠标拉近缩远的速度值
public float sensitivetyMouseWheel = 10f;
private float x = 0.0f;
private float y = 0.0f;
//角度定义有效角度范围的开始
float yMinLimit = -20;
//角度定义有效角度范围的结束
float yMaxLimit = 80;
#region 公有方法
void Start()
{
var angles = transform.eulerAngles;
x = angles.y;
y = angles.x;
}
static float ClampAngle(float angle, float min, float max)
{
if (angle < -360)
{
angle += 360;
}
if (angle > 360)
{
angle -= 360;
}
return Mathf.Clamp(angle, min, max);
}
下面控制时间写在Update()就可以了
//键盘按钮←/a和→/d实现视角水平移动,键盘按钮↑/w和↓/s实现视角水平旋转
if (Input.GetAxis("Horizontal") != 0)
{
transform.Translate(Input.GetAxis("Horizontal") * sensitivetyKeyBoard, 0, 0);
}
if (Input.GetAxis("Vertical") != 0)
{
transform.Translate(0,0, Input.GetAxis("Vertical") * sensitivetyKeyBoard);
}
//按着鼠标右键实现视角转动
if (Input.GetMouseButton(1))
{
x += Input.GetAxis("Mouse X") * 250 * 0.02f;
y -= Input.GetAxis("Mouse Y") * 120 * 0.02f;
y = ClampAngle(y, yMinLimit, yMaxLimit);
var rotation = Quaternion.Euler(y, x, 0);
transform.rotation = rotation;
}
//按鼠标中键实现拖动上下左右视角
if (Input.GetMouseButton(2))
{
Vector3 p0 = Camera.main.transform.position;
Vector3 p01 = p0 - Camera.main.transform.right * Input.GetAxisRaw("Mouse X") * 1 * Time.timeScale;
Vector3 p03 = p01 - Camera.main.transform.up * Input.GetAxisRaw("Mouse Y") * 1 * Time.timeScale;
Camera.main.transform.position = p03;
}
//滚轮实现镜头缩进和拉远
if (Input.GetAxis("Mouse ScrollWheel") != 0)
{
CameraControllerManager.Instance.MainCamera.fieldOfView = CameraControllerManager.Instance.MainCamera.fieldOfView - Input.GetAxis("Mouse ScrollWheel") * sensitivetyMouseWheel;
}