Arrow.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. using System.Collections.Generic;
  2. using UnityEngine;
  3. using DG.Tweening;
  4. public class Arrow : MonoBehaviour
  5. {
  6. private Rigidbody newRigidbody;
  7. public Vector3 shootOutPosition;
  8. public float flyTime = 0;
  9. public bool isHit = false;
  10. public ArmBow armBow;
  11. public static float speed = GameMgr.RealSizeToGameSize(60);
  12. void Awake()
  13. {
  14. GameMgr.ins.gameMode.PauseTimeCounting(this);
  15. }
  16. void Start()
  17. {
  18. newRigidbody = this.gameObject.AddComponent<Rigidbody>();
  19. float speedCopy = speed;
  20. if (GameAssistUI.ins) {
  21. speedCopy *= (1 + GameAssistUI.ins.shootScaleValue);
  22. }
  23. //箭刚射出去时,角度往上抬一点
  24. float distance = TargetBody.ins.GetDistance();
  25. this.transform.Rotate(Vector3.left, Mathf.Clamp(distance / 50 * 20, 0, 20));
  26. newRigidbody.velocity = this.transform.forward * speedCopy;
  27. newRigidbody.collisionDetectionMode = CollisionDetectionMode.ContinuousDynamic;
  28. Transform cameraTF = this.transform.Find("Camera");
  29. cameraTF.gameObject.SetActive(true);
  30. cameraTF.gameObject.AddComponent<ArrowCamera>().arrow = this;
  31. this.activeEffectTrail(true);
  32. }
  33. void OnDestroy() {
  34. GameMgr.ins.gameMode.ResumeTimeCounting(this);
  35. if (this.newRigidbody)
  36. {
  37. Destroy(this.newRigidbody);
  38. }
  39. }
  40. Vector3 position;
  41. Quaternion rotation;
  42. void FixedUpdate()
  43. {
  44. if (newRigidbody) {
  45. transform.forward = newRigidbody.velocity.normalized;
  46. }
  47. if (!isHit && flyTime >= 0) {
  48. flyTime += Time.deltaTime;
  49. this.position = this.transform.position;
  50. this.rotation = this.transform.rotation;
  51. if (flyTime > 4) {
  52. Destroy(gameObject);
  53. GameMgr.ins.gameMode.HitTarget(0);
  54. AudioMgr.ins.PlayCheer(false);
  55. nextShoot();
  56. }
  57. this.UpdateRotate();
  58. }
  59. // this.UpdateShake();
  60. }
  61. public void Hit() {
  62. gameObject.GetComponent<BoxCollider>().enabled = false;
  63. if (newRigidbody) {
  64. newRigidbody.useGravity = false;
  65. newRigidbody.velocity = Vector3.zero;
  66. Destroy(newRigidbody);
  67. newRigidbody = null;
  68. }
  69. isHit = true;
  70. // this.Shake();
  71. this.activeEffectCyclone(false);
  72. this.activeEffectBomb(true);
  73. this.activeEffectTrail(false);
  74. Debug.Log("Speed " + GameMgr.GameSizeToRealSize(Arrow.speed));
  75. Debug.Log("Distance " + GameMgr.GameSizeToRealSize(Vector3.Distance(this.getHeadPosition(), shootOutPosition)));
  76. }
  77. public Vector3 getHeadPosition() {
  78. return this.transform.Find("Head").position;
  79. }
  80. /**箭矢旋转 */
  81. private Vector3 rotateV3 = new Vector3(0, 0, 1400);
  82. private void UpdateRotate() {
  83. this.transform.Find("Head").Rotate(rotateV3 * Time.deltaTime, Space.Self);
  84. }
  85. /**箭矢震动 */
  86. private bool shaking = false;
  87. private float shakeTime = 0;
  88. private float shakeTimeMax = 0.5f;
  89. private float shakeRange = 5;
  90. private int shakeDirection = 1;
  91. private Vector3 shakeAngles = new Vector3();
  92. private void Shake()
  93. {
  94. this.shaking = true;
  95. this.shakeTime = 0;
  96. this.shakeDirection = 1;
  97. }
  98. private void UpdateShake()
  99. {
  100. if (!this.shaking) {
  101. return;
  102. }
  103. Transform transform = this.transform.Find("Head").transform;
  104. float shakeRangeNow = (1 - this.shakeTime / this.shakeTimeMax) * this.shakeRange;
  105. if (shakeRangeNow <= 0) {//震动结束
  106. this.shaking = false;
  107. this.shakeAngles.x = 0;
  108. } else {
  109. this.shakeAngles.x = -this.shakeDirection * shakeRangeNow;
  110. this.shakeDirection *= -1;
  111. }
  112. transform.localEulerAngles = this.shakeAngles;
  113. this.shakeTime += Time.deltaTime;
  114. }
  115. //进入下一轮射击
  116. bool hasDoneNextShoot = false;
  117. public void nextShoot() {
  118. if (hasDoneNextShoot) return;
  119. hasDoneNextShoot = true;
  120. GameMgr.ins.gameMode.ResumeTimeCounting(this);
  121. if (!GameMgr.ins.gameMode.DoNextShoot()) return;
  122. this.armBow.readyShoot();
  123. }
  124. void OnCollisionEnter(Collision collision) {
  125. if ((1 << collision.gameObject.layer) != LayerMask.GetMask("Target"))
  126. {
  127. this.Hit();
  128. GameMgr.ins.gameMode.HitTarget(0);
  129. AudioMgr.ins.PlayCheer(false);
  130. }
  131. this.transform.SetParent(collision.transform.parent);
  132. }
  133. public void activeEffectCyclone(bool value)
  134. {
  135. this.transform.Find("Head/EF_kuosanquan").gameObject.SetActive(value);
  136. if (!value) return;
  137. ParticleSystemRenderer ps = this.transform.Find("Head/EF_kuosanquan/kuosan").GetComponent<ParticleSystemRenderer>();
  138. ParticleSystemRenderer ps1 = this.transform.Find("Head/EF_kuosanquan/kuosan (1)").GetComponent<ParticleSystemRenderer>();
  139. DOTween.To(() => ps.minParticleSize, value => {
  140. ps.minParticleSize = value;
  141. ps.maxParticleSize = value;
  142. }, 0.4f, 0.6f);
  143. DOTween.To(() => ps1.minParticleSize, value => {
  144. ps1.minParticleSize = value;
  145. ps1.maxParticleSize = value;
  146. }, 0.8f, 0.6f);
  147. }
  148. void activeEffectBomb(bool value)
  149. {
  150. this.transform.Find("Head/EF_baodian").gameObject.SetActive(value);
  151. }
  152. void activeEffectTrail(bool value)
  153. {
  154. this.transform.Find("EF_tuowei").gameObject.SetActive(value);
  155. this.transform.Find("EF_tuowei/Trail").GetComponent<TrailRenderer>().time = 1.6f / speed;
  156. }
  157. }