ArrowCamera.cs 6.9 KB

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