using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.EventSystems; using ZXK.BYSS; /******************************************************************************** *Create By CG *Function 摄像机自身控制控制 *WSAD:前后左右-QE:上下-鼠标滚轮:镜头远近-按住鼠标右键,拖动鼠标:摄像机上下左右旋转 *********************************************************************************/ namespace ZXK.UTility { public class CameraControl : MonoBehaviour { public float _RotSpeed = 256; public float _MoveSpeed = 10; public float _ZoomSpeed = 30; //摄像机三维位移受限,暂时实现上下受限 private Vector3 _minPos = new Vector3(-15.0f, 0.3f, -23.0f); private Vector3 _maxPos = new Vector3(15.0f, 6.0f, 23.0f); private float _minZoom = 5.0f; private float _maxZoom = 100f; private Vector3 _translation; private float rotX = 0.0f; private float rotY = 0.0f; private void OnEnable() { Vector3 angles = transform.eulerAngles; rotX = angles.y; rotY = angles.x; } private void Update() { if (!EventSystem.current.IsPointerOverGameObject()) { _translation = GetInputTranslationDirection(); SetCamDistance(); SetCamPostion(); SetCamRotation(); } } /// /// 设置摄像机镜头远近 /// private void SetCamDistance() { //transform.localPosition += transform.forward * Input.mouseScrollDelta.y * Time.deltaTime * _ZoomSpeed; float zoom = transform.GetComponent().fieldOfView; if (zoom < _minZoom&& Input.mouseScrollDelta.y>0 || zoom > _maxZoom && Input.mouseScrollDelta.y < 0) { return; } transform.GetComponent().fieldOfView-= Input.mouseScrollDelta.y * Time.deltaTime * _ZoomSpeed; } /// /// 设置摄像机位移 /// private void SetCamPostion() { transform.position += _translation * Time.deltaTime * _MoveSpeed; } /// /// 设置摄像机旋转 /// private void SetCamRotation() { if (Input.GetKey(KeyCode.Mouse1)) { float mouse_x = Input.GetAxis("Mouse X"); float mouse_y = Input.GetAxis("Mouse Y"); rotX += mouse_x * _RotSpeed * Time.deltaTime; rotY -= mouse_y * _RotSpeed * Time.deltaTime; rotY = Mathf.Clamp(rotY, -90, 90);//防止翻跟头 //Quaternion rotation = Quaternion.Euler(rotY, rotX, 0);//利用欧拉角防止万向锁 //transform.rotation = rotation; transform.localEulerAngles = new Vector3(rotY, rotX); } } Vector3 GetInputTranslationDirection() { Vector3 direction = new Vector3(); if (Input.GetKey(KeyCode.W)) { //direction += Vector3.forward; if (AppManagement.Instance._CurModel == EnumCtrl.Model.Train || AppManagement.Instance._CurModel == EnumCtrl.Model.Exam) { //不让摄像机有实际前进位移,方便拖拽出来的物体可以被看到 float zoom = transform.GetComponent().fieldOfView; if (zoom > _minZoom) { transform.GetComponent().fieldOfView -= 1 * Time.deltaTime * _ZoomSpeed; } } else { Vector3 forward = new Vector3(transform.forward.x, 0, transform.forward.z); direction += forward.normalized; } } if (Input.GetKey(KeyCode.S)) { //direction += Vector3.back; if (AppManagement.Instance._CurModel == EnumCtrl.Model.Train || AppManagement.Instance._CurModel == EnumCtrl.Model.Exam) { float zoom = transform.GetComponent().fieldOfView; if (zoom < _maxZoom) { transform.GetComponent().fieldOfView -= -1 * Time.deltaTime * _ZoomSpeed; } } else { Vector3 forward = new Vector3(transform.forward.x, 0, transform.forward.z); direction -= forward.normalized; } } if (Input.GetKey(KeyCode.A)) { //direction += Vector3.left; Vector3 right = new Vector3(transform.right.x, 0, transform.right.z); direction -= right; } if (Input.GetKey(KeyCode.D)) { //direction += Vector3.right; Vector3 right = new Vector3(transform.right.x, 0, transform.right.z); direction += right; } if (transform.position.y < _maxPos.y && Input.GetKey(KeyCode.Q)) { direction += Vector3.up; } if (transform.position.y> _minPos.y && Input.GetKey(KeyCode.E)) { direction += Vector3.down; } return direction; } } }