ArrowCamera.cs 7.2 KB

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