| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121 |
- using System;
- using UnityEngine;
- using UnityEngine.EventSystems;
- /* 弓的相机 */
- public class BowCamera : MonoBehaviour
- {
- //相机组件
- Camera _cameraComp;
- Camera cameraComp {
- get {
- if (!_cameraComp) _cameraComp = GetComponent<Camera>();
- return _cameraComp;
- }
- }
- //控制的手臂弓
- ArmBow _armBow;
- ArmBow armBow {
- get {
- if (!_armBow) _armBow = GetComponentInChildren<ArmBow>();
- return _armBow;
- }
- }
- //本地欧拉角记录值
- Vector3 localEulerAngles;
- //触摸模式开关
- bool isTouchMode = true;
- public static BowCamera ins;
- void Awake() {
- ins = this;
- localEulerAngles = transform.localEulerAngles;
- }
- 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;
- }
- if (Input.GetMouseButtonDown(0)) {
- armBow.mouseDown();
- }
- else if (Input.GetMouseButtonUp(0)) {
- armBow.mouseUp();
- }
- } else {
- if (isTouchMode) {
- //触摸控制镜头和拉弓射箭
- if (Input.touches.Length == 1 && Input.touches[0].phase == TouchPhase.Moved)
- {
- if (!EventSystem.current.IsPointerOverGameObject(Input.touches[0].fingerId))
- {
- this.localEulerAngles.x = Mathf.Clamp(this.localEulerAngles.x - Input.touches[0].deltaPosition.y * Time.deltaTime * 5, -36, 36);
- this.localEulerAngles.y = Mathf.Clamp(this.localEulerAngles.y + Input.touches[0].deltaPosition.x * Time.deltaTime * 5, -180, 180);
- this.transform.localEulerAngles = this.localEulerAngles;
- }
- }
- if (Input.touches.Length > 0)
- {
- foreach (Touch touch in Input.touches)
- {
- if (EventSystem.current.IsPointerOverGameObject(touch.fingerId)) return;
- }
- armBow.mouseDown();
- }
- if (Input.touches.Length == 0)
- {
- armBow.mouseUp();
- }
- } else {
- //镜头看向九轴姿态虚拟节点
- this.transform.LookAt(GameObject.Find("CameraToLook/Point").transform);
- }
- }
- }
- //---------------相机视野的相关操作---------------------
- //视野值记录
- float cameraFieldOfView = 60;
- //禁止相机视野改变
- [NonSerialized] public bool banCameraFieldOfView = false;
-
- //立马改变相机视野值
- public void SetCameraFieldOfView(float value) {
- cameraComp.fieldOfView = 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;
- }
- }
- }
|