using System; using UnityEngine; using UnityEngine.EventSystems; /* 弓的相机 */ public class BowCamera : MonoBehaviour { //相机组件 Camera _cameraComp; Camera cameraComp { get { if (!_cameraComp) _cameraComp = GetComponent(); return _cameraComp; } } //控制的手臂弓 ArmBow _armBow; ArmBow armBow { get { if (!_armBow) _armBow = GetComponentInChildren(); return _armBow; } } //本地欧拉角记录值 Vector3 localEulerAngles; //触摸检测器 JC.Unity.TouchChecker touchChecker = new JC.Unity.TouchChecker(); //触摸模式开关 public static bool isTouchMode = true; public static BowCamera ins; void Awake() { ins = this; localEulerAngles = transform.localEulerAngles; } void Start() { touchChecker.onMoved += delegate(Touch t, bool isOnUI) { if (isOnUI) return; //触摸控制镜头和拉弓射箭 this.localEulerAngles.x = Mathf.Clamp(this.localEulerAngles.x - t.deltaPosition.y * Time.deltaTime * 5, -36, 36); this.localEulerAngles.y = Mathf.Clamp(this.localEulerAngles.y + t.deltaPosition.x * Time.deltaTime * 5, -180, 180); this.transform.localEulerAngles = this.localEulerAngles; }; touchChecker.onEnded += delegate(Touch t, bool isOnUI) { if (!isOnUI) armBow.ADS_fire(); }; } void Update() { //更新相机视野 if (cameraComp && !banCameraFieldOfView) { cameraComp.fieldOfView = cameraFieldOfView; } //满足以下条件则阻止控制输入 if (GameMgr.ins.gameOver) { return; } if (GameMgr.debugInEditor) { //鼠标控制镜头和拉弓射箭 this.localEulerAngles.x = Mathf.Clamp(this.localEulerAngles.x - 2f * Input.GetAxis("Mouse Y"), -36, 36); this.localEulerAngles.y = Mathf.Clamp(this.localEulerAngles.y + 2f * Input.GetAxis("Mouse X"), -180, 180); this.transform.localEulerAngles = this.localEulerAngles; if (EventSystem.current.IsPointerOverGameObject()) return; } else if (isTouchMode) { touchChecker.Update(); } else { //镜头看向九轴姿态虚拟节点 this.transform.LookAt(CameraToLook.ins.point); } } //---------------相机视野的相关操作--------------------- //视野值记录 float cameraFieldOfView = 60; //禁止相机视野改变 [NonSerialized] public bool banCameraFieldOfView = false; public void SetCameraFieldOfView(float value) { cameraComp.fieldOfView = value; } public void SetCameraFieldOfViewRecord(float value) { cameraFieldOfView = value; } //拉弓时的相机视野值变化 public void updateFollowPullBow() { if (cameraFieldOfView > 40) { cameraFieldOfView -= 20 * Time.deltaTime; } else { cameraFieldOfView = 40; } } //松开拉弓时的相机视野值变化 public void updateGiveUpPullBow() { if (cameraFieldOfView < 60) { cameraFieldOfView += 20 * Time.deltaTime; } else { cameraFieldOfView = 60; } } }