BowCamera.cs 3.7 KB

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