using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
///
/// 相机漫游控制
///
public class CameraController : MonoBehaviour
{
public static CameraController instance;
[Header("是否可移动")]
public bool canMove = true;
[Header("是否可转动")]
public bool camRotate = true;
public bool _isCouldCrossCollider = false;//是否可穿碰撞器
public float _moveSpeed = 10f;//移动速度
public float _xSpeed = 250.0f;//X轴视角旋转速度
public float _ySpeed = 120.0f;//Y轴视角旋转速度
public float _mouseSpeed = 500f;//鼠标中间滚轮的拉近拉远速度
public float _yMinLimit = -20;//视角旋转向下最小限制
public float _yMaxLimit = 80;//视角旋转向上最大限制
public float _speedLimit = 0.2f;//鼠标中键移动速度限制
private float _angleX = 0.0f;
private float _angleY = 0.0f;
private float _moveV = 0.0f;
private float _moveH = 0.0f;
private Vector3 _movePos;
private float xMax;
private float xMin;
private float yMax;
private float yMin;
private float zMax;
private float zMin;
private Vector3 initPostion;
private Quaternion initRotation;
///
/// 初始化摄像机,避免产生空引用
///
void Start()
{
initPostion = transform.position;
initRotation = transform.rotation;
instance = this;
ResetInfo();
}
public void SetCamerPositionAndRotation(Vector3 positon, Vector3 rotation)
{
_movePos = positon;
this.transform.position = positon;
transform.eulerAngles = rotation;
}
public void SetInitPositionAndRotation()
{
transform.position = initPostion;
transform.rotation = initRotation;
}
///
/// 重置位置信息
///
public void ResetInfo()
{
var angles = transform.eulerAngles;
_angleX = angles.y;
_angleY = angles.x;
_movePos = this.transform.position;
}
//Update is called once per frame
void Update()
{
if (EventSystem.current.IsPointerOverGameObject()) return;
if (canMove)
{
moveCamera();
riseCamera();
}
if (camRotate) rotateView();
viewScale();
SetMovePosToThis();
}
///
/// 前后移动摄像机
///
void moveCamera()
{
_moveV = Input.GetAxis("Vertical") * Time.deltaTime * _moveSpeed;
_moveH = Input.GetAxis("Horizontal") * Time.deltaTime * _moveSpeed;
_movePos = this.transform.position + this.transform.forward * _moveV + this.transform.right * _moveH;
}
///
/// 自身位置赋值
///
private void SetMovePosToThis()
{
if (_isCouldCrossCollider)
{
this.transform.position = _movePos;
}
else
{
if (CameraRestraint.CheckThePos(this.transform, _movePos))
{
this.transform.position = _movePos;
}
}
}
public void SetCameraPosLimite(float _xMin, float _xMax, float _yMin, float _yMax, float _zMin, float _zMax)
{
xMax = _xMax; xMin = _xMin;
yMax = _yMax; yMin = _yMin;
zMax = _zMax; zMin = _zMin;
}
///
/// 鼠标中键控制相机移动
///
void riseCamera()
{
if (Input.GetMouseButton(2))
{
_moveV = -Input.GetAxis("Mouse Y") * _moveSpeed;
_moveH = -Input.GetAxis("Mouse X") * _moveSpeed;
_movePos = this.transform.position + this.transform.up * _moveV * Time.deltaTime + this.transform.right * _moveH * Time.deltaTime;
}
}
///
/// 鼠标右键控制视角旋转
///
void rotateView()
{
if (Input.GetMouseButton(1))
{
_angleX += Input.GetAxis("Mouse X") * _xSpeed * 0.02f;
_angleY -= Input.GetAxis("Mouse Y") * _ySpeed * 0.02f;
_angleY = ClampAngle(_angleY, _yMinLimit, _yMaxLimit);
var rotation = Quaternion.Euler(_angleY, _angleX, 0);
transform.rotation = rotation;
}
}
///
/// 滑轮控制视角移动
///
void viewScale()
{
if (Input.GetAxis("Mouse ScrollWheel") != 0)
{
_movePos = this.transform.position + this.transform.forward * Input.GetAxis("Mouse ScrollWheel") * Time.deltaTime * _mouseSpeed;
}
}
///
/// 控制摄像机的旋转角度大小
///
///
///
///
///
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);
}
}