BowCamera.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. bool isTouchMode = true;
  27. public static BowCamera ins;
  28. void Awake() {
  29. ins = this;
  30. localEulerAngles = transform.localEulerAngles;
  31. }
  32. void Update()
  33. {
  34. //更新相机视野
  35. if (cameraComp && !banCameraFieldOfView) {
  36. cameraComp.fieldOfView = cameraFieldOfView;
  37. }
  38. //满足以下条件则阻止控制输入
  39. if (GameMgr.ins.gameOver) {
  40. return;
  41. }
  42. if (GameMgr.debugInEditor) {
  43. //鼠标控制镜头和拉弓射箭
  44. this.localEulerAngles.x = Mathf.Clamp(this.localEulerAngles.x - 2f * Input.GetAxis("Mouse Y"), -36, 36);
  45. this.localEulerAngles.y = Mathf.Clamp(this.localEulerAngles.y + 2f * Input.GetAxis("Mouse X"), -180, 180);
  46. this.transform.localEulerAngles = this.localEulerAngles;
  47. if (EventSystem.current.IsPointerOverGameObject())
  48. {
  49. return;
  50. }
  51. if (Input.GetMouseButtonDown(0)) {
  52. armBow.mouseDown();
  53. }
  54. else if (Input.GetMouseButtonUp(0)) {
  55. armBow.mouseUp();
  56. }
  57. } else {
  58. if (isTouchMode) {
  59. //触摸控制镜头和拉弓射箭
  60. if (Input.touches.Length == 1 && Input.touches[0].phase == TouchPhase.Moved)
  61. {
  62. if (!EventSystem.current.IsPointerOverGameObject(Input.touches[0].fingerId))
  63. {
  64. this.localEulerAngles.x = Mathf.Clamp(this.localEulerAngles.x - Input.touches[0].deltaPosition.y * Time.deltaTime * 5, -36, 36);
  65. this.localEulerAngles.y = Mathf.Clamp(this.localEulerAngles.y + Input.touches[0].deltaPosition.x * Time.deltaTime * 5, -180, 180);
  66. this.transform.localEulerAngles = this.localEulerAngles;
  67. }
  68. }
  69. if (Input.touches.Length > 0)
  70. {
  71. foreach (Touch touch in Input.touches)
  72. {
  73. if (EventSystem.current.IsPointerOverGameObject(touch.fingerId)) return;
  74. }
  75. armBow.mouseDown();
  76. }
  77. if (Input.touches.Length == 0)
  78. {
  79. armBow.mouseUp();
  80. }
  81. } else {
  82. //镜头看向九轴姿态虚拟节点
  83. this.transform.LookAt(GameObject.Find("CameraToLook/Point").transform);
  84. }
  85. }
  86. }
  87. //---------------相机视野的相关操作---------------------
  88. //视野值记录
  89. float cameraFieldOfView = 60;
  90. //禁止相机视野改变
  91. [NonSerialized] public bool banCameraFieldOfView = false;
  92. //立马改变相机视野值
  93. public void SetCameraFieldOfView(float value) {
  94. cameraComp.fieldOfView = cameraFieldOfView = value;
  95. }
  96. //拉弓时的相机视野值变化
  97. public void updateFollowPullBow() {
  98. if (cameraFieldOfView > 40) {
  99. cameraFieldOfView -= 20 * Time.deltaTime;
  100. } else {
  101. cameraFieldOfView = 40;
  102. }
  103. }
  104. //松开拉弓时的相机视野值变化
  105. public void updateGiveUpPullBow() {
  106. if (cameraFieldOfView < 60) {
  107. cameraFieldOfView += 20 * Time.deltaTime;
  108. } else {
  109. cameraFieldOfView = 60;
  110. }
  111. }
  112. }