ArrowCamera.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using DG.Tweening;
  5. public class ArrowCamera : MonoBehaviour
  6. {
  7. public Arrow arrow;
  8. ArrowCameraTemplate arrowCameraTemplate;
  9. void Awake()
  10. {
  11. BowCamera.ins.GetComponent<Camera>().enabled = false;
  12. }
  13. void Start()
  14. {
  15. if (arrow.simulatedParabola) {
  16. arrowCameraTemplate = new ArrowCameraTemplate2(this);
  17. } else {
  18. arrowCameraTemplate = new ArrowCameraTemplate1(this);
  19. }
  20. }
  21. void OnDestroy()
  22. {
  23. BowCamera.ins.GetComponent<Camera>().enabled = true;
  24. }
  25. void FixedUpdate()
  26. {
  27. arrowCameraTemplate.Update();
  28. }
  29. }
  30. class ArrowCameraTemplate2 : ArrowCameraTemplate
  31. {
  32. public ArrowCameraTemplate2(ArrowCamera arrowCamera) : base(arrowCamera) {
  33. this.arrowCamera.transform.parent = null;
  34. arrowCamera.transform.localPosition = new Vector3(8.33f, 2.45f, 6.4f);
  35. arrowCamera.transform.localEulerAngles = new Vector3(0, -56, 0);
  36. }
  37. bool isHit = false;
  38. public override void Update() {
  39. if (!isHit) {
  40. isHit = arrowCamera.arrow.isHit;
  41. if (isHit) {
  42. this.arrowCamera.transform.SetParent(this.arrowCamera.arrow.transform);
  43. arrowCamera.transform.localPosition = new Vector3(-0.6f, 0.1f, -0.8f);
  44. arrowCamera.transform.LookAt(this.arrowCamera.arrow.transform.Find("Head"));
  45. Sequence seq = DOTween.Sequence();
  46. seq.PrependInterval(1f);
  47. seq.AppendCallback(delegate() {
  48. this.arrowCamera.arrow.nextShoot();
  49. this.arrowCamera.arrow.enabled = false;
  50. GameObject.Destroy(this.arrowCamera.gameObject);
  51. });
  52. return;
  53. }
  54. }
  55. if (!arrowCamera.arrow) {
  56. GameObject.Destroy(this.arrowCamera.gameObject);
  57. }
  58. }
  59. }
  60. class ArrowCameraTemplate1 : ArrowCameraTemplate
  61. {
  62. public ArrowCameraTemplate1(ArrowCamera arrowCamera) : base(arrowCamera) {
  63. this.arrowCamera.arrow.activeEffectCyclone(true);
  64. }
  65. /**相机移动 */
  66. private bool cameraMoveFinish = false;
  67. //相机的位置xyz变化 【每秒变化量,最小值,最大值】
  68. private float[] cpcs = {
  69. //运动中
  70. 6, 0, 0.4f,
  71. 6, 0, 0.2f,
  72. -18f, -1 - Mathf.Clamp(Arrow.speed / 20 * 6, 0, 6), 0,
  73. //击中后
  74. -3.5f, -0.6f, 0.4f,
  75. -3.5f, 0.1f, 0.2f,
  76. 30, -1 - Mathf.Clamp(Arrow.speed / 20 * 6, 0, 6), -0.8f
  77. };
  78. public override void Update() {
  79. if (cameraMoveFinish) {
  80. return;
  81. }
  82. Transform cameraT = this.arrowCamera.transform;
  83. Vector3 cameraPosition = cameraT.localPosition;
  84. if (this.arrowCamera.arrow.isHit) {
  85. cameraPosition.x = Mathf.Clamp(cameraPosition.x + this.cpcs[9] * Time.deltaTime, this.cpcs[10], this.cpcs[11]);
  86. cameraPosition.y = Mathf.Clamp(cameraPosition.y + this.cpcs[12] * Time.deltaTime, this.cpcs[13], this.cpcs[14]);
  87. cameraPosition.z = Mathf.Clamp(cameraPosition.z + this.cpcs[15] * Time.deltaTime, this.cpcs[16], this.cpcs[17]);
  88. float d = Vector3.Distance(cameraPosition, new Vector3(
  89. this.cpcs[9] < 0 ? this.cpcs[10]: this.cpcs[11],
  90. this.cpcs[12] < 0 ? this.cpcs[13]: this.cpcs[14],
  91. this.cpcs[15] < 0 ? this.cpcs[16]: this.cpcs[17]
  92. ));
  93. if (d < 0.001f) {
  94. cameraMoveFinish = true;
  95. Sequence seq = DOTween.Sequence();
  96. seq.AppendInterval(1.0f);
  97. seq.AppendCallback(delegate() {
  98. this.arrowCamera.arrow.nextShoot();
  99. this.arrowCamera.arrow.enabled = false;
  100. GameObject.Destroy(this.arrowCamera.gameObject);
  101. });
  102. }
  103. } else {
  104. cameraPosition.x = Mathf.Clamp(cameraPosition.x + this.cpcs[0] * Time.deltaTime, this.cpcs[1], this.cpcs[2]);
  105. cameraPosition.y = Mathf.Clamp(cameraPosition.y + this.cpcs[3] * Time.deltaTime, this.cpcs[4], this.cpcs[5]);
  106. cameraPosition.z = Mathf.Clamp(cameraPosition.z + this.cpcs[6] * Time.deltaTime, this.cpcs[7], this.cpcs[8]);
  107. }
  108. cameraT.localPosition = cameraPosition;
  109. cameraT.LookAt(this.arrowCamera.arrow.transform.Find("Head"));
  110. }
  111. }
  112. class ArrowCameraTemplate {
  113. public ArrowCamera arrowCamera;
  114. public ArrowCameraTemplate(ArrowCamera arrowCamera)
  115. {
  116. this.arrowCamera = arrowCamera;
  117. }
  118. public virtual void Update() {}
  119. }
  120. // public class ArrowCamera : MonoBehaviour
  121. // {
  122. // public Arrow arrow;
  123. // void FixedUpdate()
  124. // {
  125. // updateMoveCamera();
  126. // }
  127. // /**相机移动 */
  128. // private Vector3 cameraPosition = new Vector3();
  129. // private bool cameraMoveFinish = false;
  130. // //相机的位置xyz变化 【每秒变化量,最小值,最大值】
  131. // private float[] cpcs = {
  132. // //运动中
  133. // 0, 0, 0,
  134. // 1.5f, 0, 0.25f,
  135. // -7, -1.5f, 0,
  136. // //击中后
  137. // -3.5f, -1.2f, 0,
  138. // -3.5f, 0, 0.5f,
  139. // 7, -1.5f, -1.5f
  140. // };
  141. // private void updateMoveCamera() {
  142. // if (cameraMoveFinish) {
  143. // return;
  144. // }
  145. // Transform cameraT = this.transform;
  146. // cameraPosition.x = cameraT.localPosition.x;
  147. // cameraPosition.y = cameraT.localPosition.y;
  148. // cameraPosition.z = cameraT.localPosition.z;
  149. // if (arrow.isHit) {
  150. // cameraPosition.x = Mathf.Clamp(cameraPosition.x + this.cpcs[9] * Time.deltaTime, this.cpcs[10], this.cpcs[11]);
  151. // cameraPosition.y = Mathf.Clamp(cameraPosition.y + this.cpcs[12] * Time.deltaTime, this.cpcs[13], this.cpcs[14]);
  152. // cameraPosition.z = Mathf.Clamp(cameraPosition.z + this.cpcs[15] * Time.deltaTime, this.cpcs[16], this.cpcs[17]);
  153. // float d = Vector3.Distance(cameraPosition, new Vector3(
  154. // this.cpcs[9] < 0 ? this.cpcs[10]: this.cpcs[11],
  155. // this.cpcs[12] < 0 ? this.cpcs[13]: this.cpcs[14],
  156. // this.cpcs[15] < 0 ? this.cpcs[16]: this.cpcs[17]
  157. // ));
  158. // if (d < 0.001f) {
  159. // cameraMoveFinish = true;
  160. // Sequence seq = DOTween.Sequence();
  161. // seq.AppendInterval(1.0f);
  162. // seq.AppendCallback(delegate() {
  163. // arrow.nextShoot();
  164. // Destroy(cameraT.gameObject);
  165. // this.enabled = false;
  166. // });
  167. // }
  168. // } else {
  169. // cameraPosition.x = Mathf.Clamp(cameraPosition.y + this.cpcs[0] * Time.deltaTime, this.cpcs[1], this.cpcs[2]);
  170. // cameraPosition.y = Mathf.Clamp(cameraPosition.y + this.cpcs[3] * Time.deltaTime, this.cpcs[4], this.cpcs[5]);
  171. // cameraPosition.z = Mathf.Clamp(cameraPosition.z + this.cpcs[6] * Time.deltaTime, this.cpcs[7], this.cpcs[8]);
  172. // }
  173. // cameraT.localPosition = cameraPosition;
  174. // cameraT.LookAt(arrow.transform.Find("Head"));
  175. // }
  176. // }