ArrowCamera.cs 7.2 KB

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