Arrow.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. using System.Collections.Generic;
  2. using UnityEngine;
  3. public class Arrow : MonoBehaviour
  4. {
  5. private Rigidbody newRigidbody;
  6. public float flyTime = 0;
  7. public bool isHit = false;
  8. public ArmBow armBow;
  9. public static float speed = 4;
  10. void Start()
  11. {
  12. // CrossHair.ins.gameObject.SetActive(false);
  13. newRigidbody = this.gameObject.AddComponent<Rigidbody>();
  14. newRigidbody.velocity = this.transform.forward * speed;
  15. newRigidbody.collisionDetectionMode = CollisionDetectionMode.ContinuousDynamic;
  16. this.transform.Find("Camera").gameObject.SetActive(true);
  17. this.activeEffectCyclone(true);
  18. this.activeEffectTrail(true);
  19. }
  20. Vector3 position;
  21. Quaternion rotation;
  22. void FixedUpdate()
  23. {
  24. if (newRigidbody != null) {
  25. transform.forward = newRigidbody.velocity.normalized;
  26. }
  27. if (!isHit && flyTime >= 0) {
  28. flyTime += Time.deltaTime;
  29. this.position = this.transform.position;
  30. this.rotation = this.transform.rotation;
  31. if (flyTime > 20.0) {
  32. Destroy(gameObject);
  33. GameMgr.ins.hitTarget(0);
  34. AudioMgr.ins.PlayCheer(false);
  35. nextShoot();
  36. }
  37. this.UpdateRotate();
  38. }
  39. this.UpdateShake();
  40. this.updateMoveCamera();
  41. if (!this.isHit) {
  42. this.updateEffectCyclone();
  43. }
  44. }
  45. public void Hit() {
  46. gameObject.GetComponent<BoxCollider>().enabled = false;
  47. if (newRigidbody != null) {
  48. newRigidbody.useGravity = false;
  49. newRigidbody.velocity = Vector3.zero;
  50. Destroy(newRigidbody);
  51. newRigidbody = null;
  52. }
  53. isHit = true;
  54. // this.Shake();
  55. this.activeEffectCyclone(false);
  56. this.activeEffectBomb(true);
  57. this.activeEffectTrail(false);
  58. }
  59. public Vector3 getHeadPosition() {
  60. return this.transform.Find("Head").position;
  61. }
  62. /**箭矢旋转 */
  63. private Vector3 rotateV3 = new Vector3(0, 0, 1400);
  64. private void UpdateRotate() {
  65. this.transform.Find("Head").Rotate(rotateV3 * Time.deltaTime, Space.Self);
  66. }
  67. /**箭矢震动 */
  68. private bool shaking = false;
  69. private float shakeTime = 0;
  70. private float shakeTimeMax = 0.5f;
  71. private float shakeRange = 5;
  72. private int shakeDirection = 1;
  73. private Vector3 shakeAngles = new Vector3();
  74. private void Shake()
  75. {
  76. this.shaking = true;
  77. this.shakeTime = 0;
  78. this.shakeDirection = 1;
  79. }
  80. private void UpdateShake()
  81. {
  82. if (!this.shaking) {
  83. return;
  84. }
  85. Transform transform = this.transform.Find("Head").transform;
  86. float shakeRangeNow = (1 - this.shakeTime / this.shakeTimeMax) * this.shakeRange;
  87. if (shakeRangeNow <= 0) {//震动结束
  88. this.shaking = false;
  89. this.shakeAngles.x = 0;
  90. } else {
  91. this.shakeAngles.x = -this.shakeDirection * shakeRangeNow;
  92. this.shakeDirection *= -1;
  93. }
  94. transform.localEulerAngles = this.shakeAngles;
  95. this.shakeTime += Time.deltaTime;
  96. }
  97. /**相机移动 */
  98. private Vector3 cameraPosition = new Vector3();
  99. private bool cameraMoveFinish = false;
  100. //相机的位置xyz变化 【变化量,最小值,最大值】
  101. private float[] cpcs = {
  102. //运动中
  103. 0, 0, 0,
  104. 1.5f, 0, 0.25f,
  105. -7, -1.5f, 0,
  106. //击中后
  107. -3.5f, -1.2f, 0,
  108. -3.5f, 0, 0.5f,
  109. 7, -1.5f, -1.5f
  110. };
  111. private void updateMoveCamera() {
  112. if (cameraMoveFinish) {
  113. return;
  114. }
  115. Transform cameraT = this.transform.Find("Camera");
  116. cameraPosition.x = cameraT.localPosition.x;
  117. cameraPosition.y = cameraT.localPosition.y;
  118. cameraPosition.z = cameraT.localPosition.z;
  119. if (this.isHit) {
  120. cameraPosition.x = Mathf.Clamp(cameraPosition.x + this.cpcs[9] * Time.deltaTime, this.cpcs[10], this.cpcs[11]);
  121. cameraPosition.y = Mathf.Clamp(cameraPosition.y + this.cpcs[12] * Time.deltaTime, this.cpcs[13], this.cpcs[14]);
  122. cameraPosition.z = Mathf.Clamp(cameraPosition.z + this.cpcs[15] * Time.deltaTime, this.cpcs[16], this.cpcs[17]);
  123. float d = Vector3.Distance(cameraPosition, new Vector3(
  124. this.cpcs[9] < 0 ? this.cpcs[10]: this.cpcs[11],
  125. this.cpcs[12] < 0 ? this.cpcs[13]: this.cpcs[14],
  126. this.cpcs[15] < 0 ? this.cpcs[16]: this.cpcs[17]
  127. ));
  128. if (d < 0.001f) {
  129. cameraMoveFinish = true;
  130. this.Invoke("nextShoot", 1.0f);
  131. }
  132. } else {
  133. cameraPosition.x = Mathf.Clamp(cameraPosition.y + this.cpcs[0] * Time.deltaTime, this.cpcs[1], this.cpcs[2]);
  134. cameraPosition.y = Mathf.Clamp(cameraPosition.y + this.cpcs[3] * Time.deltaTime, this.cpcs[4], this.cpcs[5]);
  135. cameraPosition.z = Mathf.Clamp(cameraPosition.z + this.cpcs[6] * Time.deltaTime, this.cpcs[7], this.cpcs[8]);
  136. }
  137. cameraT.localPosition = cameraPosition;
  138. cameraT.LookAt(this.transform.Find("Head"));
  139. }
  140. /**延时方法 */
  141. public void nextShoot() {
  142. if (GameMgr.ins.checkFinish()) return;
  143. this.armBow.readyShoot();
  144. Destroy(this.transform.Find("Camera").gameObject);
  145. // CrossHair.ins.gameObject.SetActive(true);
  146. this.enabled = false;
  147. }
  148. void OnCollisionEnter(Collision collision) {
  149. if ((1 << collision.gameObject.layer) != LayerMask.GetMask("Target"))
  150. {
  151. this.Hit();
  152. GameMgr.ins.hitTarget(0);
  153. AudioMgr.ins.PlayCheer(false);
  154. }
  155. this.transform.SetParent(collision.transform.parent);
  156. }
  157. void activeEffectCyclone(bool value)
  158. {
  159. this.transform.Find("Head/EF_kuosanquan").gameObject.SetActive(value);
  160. }
  161. void activeEffectBomb(bool value)
  162. {
  163. this.transform.Find("Head/EF_baodian").gameObject.SetActive(value);
  164. }
  165. void activeEffectTrail(bool value)
  166. {
  167. this.transform.Find("EF_tuowei").gameObject.SetActive(value);
  168. this.transform.Find("EF_tuowei/Trail").GetComponent<TrailRenderer>().time = 1.6f / speed;
  169. }
  170. void updateEffectCyclone() {
  171. ParticleSystemRenderer ps = this.transform.Find("Head/EF_kuosanquan/kuosan").GetComponent<ParticleSystemRenderer>();
  172. if (ps.minParticleSize < 0.6f)
  173. {
  174. ps.minParticleSize += 0.03f;
  175. ps.maxParticleSize = ps.minParticleSize;
  176. }
  177. ParticleSystemRenderer ps1 = this.transform.Find("Head/EF_kuosanquan/kuosan (1)").GetComponent<ParticleSystemRenderer>();
  178. if (ps1.minParticleSize < 1.2f)
  179. {
  180. ps1.minParticleSize += 0.06f;
  181. ps1.maxParticleSize = ps1.minParticleSize;
  182. }
  183. }
  184. public void calculateSpeed(Vector3 rayPoint)
  185. {
  186. speed = Mathf.Clamp(Vector3.Distance(rayPoint, this.getHeadPosition()) / 2, 6.6f, 16);
  187. }
  188. }