ArrowCamera.cs 6.9 KB

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