123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.EventSystems;
- /// <summary>
- /// 相机漫游控制
- /// </summary>
- 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;
- /// <summary>
- /// 初始化摄像机,避免产生空引用
- /// </summary>
- 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;
- }
- /// <summary>
- /// 重置位置信息
- /// </summary>
- 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();
- }
- /// <summary>
- /// 前后移动摄像机
- /// </summary>
- 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;
- }
- /// <summary>
- /// 自身位置赋值
- /// </summary>
- 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;
- }
- /// <summary>
- /// 鼠标中键控制相机移动
- /// </summary>
- 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;
- }
- }
- /// <summary>
- /// 鼠标右键控制视角旋转
- /// </summary>
- 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;
- }
- }
- /// <summary>
- /// 滑轮控制视角移动
- /// </summary>
- void viewScale()
- {
- if (Input.GetAxis("Mouse ScrollWheel") != 0)
- {
- _movePos = this.transform.position + this.transform.forward * Input.GetAxis("Mouse ScrollWheel") * Time.deltaTime * _mouseSpeed;
- }
- }
- /// <summary>
- /// 控制摄像机的旋转角度大小
- /// </summary>
- /// <param 角度值="angle"></param>
- /// <param 最小角度="min"></param>
- /// <param 最大角度="max"></param>
- /// <returns></returns>
- 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);
- }
- }
|