Arrow.cs 7.0 KB

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