BowCamera.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. using System;
  2. using UnityEngine;
  3. using UnityEngine.EventSystems;
  4. /* 弓的相机 */
  5. public class BowCamera : MonoBehaviour
  6. {
  7. //相机组件
  8. Camera _cameraComp;
  9. Camera cameraComp {
  10. get {
  11. if (!_cameraComp) _cameraComp = GetComponent<Camera>();
  12. return _cameraComp;
  13. }
  14. }
  15. //控制的手臂弓
  16. ArmBow _armBow;
  17. ArmBow armBow {
  18. get {
  19. if (!_armBow) _armBow = GetComponentInChildren<ArmBow>();
  20. return _armBow;
  21. }
  22. }
  23. //本地欧拉角记录值
  24. Vector3 localEulerAngles;
  25. //触摸检测器
  26. JC.Unity.TouchChecker touchChecker = new JC.Unity.TouchChecker();
  27. //触摸模式开关
  28. public static bool isTouchMode = true;
  29. public static BowCamera ins;
  30. void Awake() {
  31. ins = this;
  32. localEulerAngles = transform.localEulerAngles;
  33. }
  34. void Start() {
  35. touchChecker.onMoved += delegate(Touch t, bool isOnUI) {
  36. if (isOnUI) return;
  37. //触摸控制镜头和拉弓射箭
  38. this.localEulerAngles.x = Mathf.Clamp(this.localEulerAngles.x - t.deltaPosition.y * Time.deltaTime * 5, -36, 36);
  39. this.localEulerAngles.y = Mathf.Clamp(this.localEulerAngles.y + t.deltaPosition.x * Time.deltaTime * 5, -180, 180);
  40. this.transform.localEulerAngles = this.localEulerAngles;
  41. };
  42. touchChecker.onEnded += delegate(Touch t, bool isOnUI) {
  43. if (!isOnUI) armBow.ADS_fire();
  44. };
  45. }
  46. void Update()
  47. {
  48. //更新相机视野
  49. if (cameraComp && !banCameraFieldOfView) {
  50. cameraComp.fieldOfView = cameraFieldOfView;
  51. }
  52. //满足以下条件则阻止控制输入
  53. if (GameMgr.ins.gameOver) {
  54. return;
  55. }
  56. if (GameMgr.debugInEditor) {
  57. //鼠标控制镜头和拉弓射箭
  58. this.localEulerAngles.x = Mathf.Clamp(this.localEulerAngles.x - 2f * Input.GetAxis("Mouse Y"), -36, 36);
  59. this.localEulerAngles.y = Mathf.Clamp(this.localEulerAngles.y + 2f * Input.GetAxis("Mouse X"), -180, 180);
  60. this.transform.localEulerAngles = this.localEulerAngles;
  61. if (EventSystem.current.IsPointerOverGameObject()) return;
  62. }
  63. else if (isTouchMode) {
  64. touchChecker.Update();
  65. } else {
  66. //镜头看向九轴姿态虚拟节点
  67. this.transform.LookAt(CameraToLook.ins.point);
  68. }
  69. }
  70. //---------------相机视野的相关操作---------------------
  71. //视野值记录
  72. float cameraFieldOfView = 60;
  73. //禁止相机视野改变
  74. [NonSerialized] public bool banCameraFieldOfView = false;
  75. public void SetCameraFieldOfView(float value) {
  76. cameraComp.fieldOfView = value;
  77. }
  78. public void SetCameraFieldOfViewRecord(float value) {
  79. cameraFieldOfView = value;
  80. }
  81. //拉弓时的相机视野值变化
  82. public void updateFollowPullBow() {
  83. if (cameraFieldOfView > 40) {
  84. cameraFieldOfView -= 20 * Time.deltaTime;
  85. } else {
  86. cameraFieldOfView = 40;
  87. }
  88. }
  89. //松开拉弓时的相机视野值变化
  90. public void updateGiveUpPullBow() {
  91. if (cameraFieldOfView < 60) {
  92. cameraFieldOfView += 20 * Time.deltaTime;
  93. } else {
  94. cameraFieldOfView = 60;
  95. }
  96. }
  97. }