Arrow.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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 RaycastHit raycastHit;
  11. private float mySpeed = 0;
  12. public bool simulatedParabola = false; //模拟抛物线
  13. public ArmBow armBow;
  14. public static float speed = GameMgr.RealSizeToGameSize(60);
  15. void Awake()
  16. {
  17. GameMgr.ins.gameMode.PauseTimeCounting(this);
  18. }
  19. void Start()
  20. {
  21. newRigidbody = this.gameObject.AddComponent<Rigidbody>();
  22. mySpeed = speed;
  23. if (GameAssistUI.ins) {
  24. mySpeed *= (1 + GameAssistUI.ins.shootScaleValue);
  25. }
  26. //瞄准靶子发射时,自动调整仰角
  27. if (raycastHit.transform && raycastHit.transform.gameObject.name == "TargetBody") {
  28. ApplyParabolaAngle(raycastHit.point);
  29. float distance = TargetBody.ins.GetDistance();
  30. if (Mathf.RoundToInt(distance) >= 50) {
  31. if (Random.value < 0.5) simulatedParabola = true;
  32. }
  33. }
  34. newRigidbody.velocity = this.transform.forward * mySpeed;
  35. newRigidbody.collisionDetectionMode = CollisionDetectionMode.ContinuousDynamic;
  36. Transform cameraTF = this.transform.Find("Camera");
  37. cameraTF.gameObject.SetActive(true);
  38. cameraTF.gameObject.AddComponent<ArrowCamera>().arrow = this;
  39. this.activeEffectTrail(true);
  40. }
  41. void ApplyParabolaAngle(Vector3 destination)
  42. {
  43. float angleX = 0;
  44. float distX = Vector2.Distance(
  45. new Vector2(destination.x, destination.z),
  46. new Vector2(this.transform.position.x, this.transform.position.z)
  47. );
  48. float distY = destination.y - this.transform.position.y;
  49. float posBase = (Physics.gravity.y * Mathf.Pow(distX, 2.0f)) / (2.0f * Mathf.Pow(mySpeed, 2.0f));
  50. float posX = distX / posBase;
  51. float posY = (Mathf.Pow(posX, 2.0f) / 4.0f) - ((posBase - distY) / posBase);
  52. bool paoshe = false;
  53. if (posY >= 0.0f)
  54. {
  55. if (paoshe)
  56. angleX = Mathf.Rad2Deg * Mathf.Atan(-posX / 2.0f + Mathf.Pow(posY, 0.5f));
  57. else
  58. angleX = Mathf.Rad2Deg * Mathf.Atan(-posX / 2.0f - Mathf.Pow(posY, 0.5f));
  59. }
  60. else
  61. {
  62. return;
  63. }
  64. Vector3 finalAngle = this.transform.eulerAngles;
  65. finalAngle = new Vector3(-angleX, finalAngle.y, finalAngle.z);
  66. this.transform.localRotation = Quaternion.Euler(finalAngle);
  67. }
  68. void OnDestroy() {
  69. GameMgr.ins.gameMode.ResumeTimeCounting(this);
  70. if (this.newRigidbody)
  71. {
  72. Destroy(this.newRigidbody);
  73. }
  74. }
  75. Vector3 position;
  76. Quaternion rotation;
  77. void FixedUpdate()
  78. {
  79. if (newRigidbody) {
  80. transform.forward = newRigidbody.velocity.normalized;
  81. }
  82. if (!isHit && flyTime >= 0) {
  83. flyTime += Time.deltaTime;
  84. this.position = this.transform.position;
  85. this.rotation = this.transform.rotation;
  86. if (flyTime > 14) {
  87. Destroy(gameObject);
  88. GameMgr.ins.gameMode.HitTarget(0);
  89. AudioMgr.ins.PlayCheer(false);
  90. nextShoot();
  91. }
  92. this.UpdateRotate();
  93. }
  94. // this.UpdateShake();
  95. }
  96. public void Hit() {
  97. gameObject.GetComponent<BoxCollider>().enabled = false;
  98. if (newRigidbody) {
  99. newRigidbody.useGravity = false;
  100. newRigidbody.velocity = Vector3.zero;
  101. Destroy(newRigidbody);
  102. newRigidbody = null;
  103. }
  104. isHit = true;
  105. // this.Shake();
  106. this.activeEffectCyclone(false);
  107. this.activeEffectBomb(true);
  108. this.activeEffectTrail(false);
  109. }
  110. public Vector3 getHeadPosition() {
  111. return this.transform.Find("Head").position;
  112. }
  113. /**箭矢旋转 */
  114. private Vector3 rotateV3 = new Vector3(0, 0, 1400);
  115. private void UpdateRotate() {
  116. this.transform.Find("Head").Rotate(rotateV3 * Time.deltaTime, Space.Self);
  117. }
  118. /**箭矢震动 */
  119. private bool shaking = false;
  120. private float shakeTime = 0;
  121. private float shakeTimeMax = 0.5f;
  122. private float shakeRange = 5;
  123. private int shakeDirection = 1;
  124. private Vector3 shakeAngles = new Vector3();
  125. private void Shake()
  126. {
  127. this.shaking = true;
  128. this.shakeTime = 0;
  129. this.shakeDirection = 1;
  130. }
  131. private void UpdateShake()
  132. {
  133. if (!this.shaking) {
  134. return;
  135. }
  136. Transform transform = this.transform.Find("Head").transform;
  137. float shakeRangeNow = (1 - this.shakeTime / this.shakeTimeMax) * this.shakeRange;
  138. if (shakeRangeNow <= 0) {//震动结束
  139. this.shaking = false;
  140. this.shakeAngles.x = 0;
  141. } else {
  142. this.shakeAngles.x = -this.shakeDirection * shakeRangeNow;
  143. this.shakeDirection *= -1;
  144. }
  145. transform.localEulerAngles = this.shakeAngles;
  146. this.shakeTime += Time.deltaTime;
  147. }
  148. //进入下一轮射击
  149. bool hasDoneNextShoot = false;
  150. public void nextShoot() {
  151. if (hasDoneNextShoot) return;
  152. hasDoneNextShoot = true;
  153. GameMgr.ins.gameMode.ResumeTimeCounting(this);
  154. if (!GameMgr.ins.gameMode.DoNextShoot()) return;
  155. this.armBow.readyShoot();
  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. public void activeEffectCyclone(bool value)
  167. {
  168. this.transform.Find("Head/EF_kuosanquan").gameObject.SetActive(value);
  169. if (!value) return;
  170. ParticleSystemRenderer ps = this.transform.Find("Head/EF_kuosanquan/kuosan").GetComponent<ParticleSystemRenderer>();
  171. ParticleSystemRenderer ps1 = this.transform.Find("Head/EF_kuosanquan/kuosan (1)").GetComponent<ParticleSystemRenderer>();
  172. DOTween.To(() => ps.minParticleSize, value => {
  173. ps.minParticleSize = value;
  174. ps.maxParticleSize = value;
  175. }, 0.4f, 0.6f);
  176. DOTween.To(() => ps1.minParticleSize, value => {
  177. ps1.minParticleSize = value;
  178. ps1.maxParticleSize = value;
  179. }, 0.8f, 0.6f);
  180. }
  181. void activeEffectBomb(bool value)
  182. {
  183. this.transform.Find("Head/EF_baodian").gameObject.SetActive(value);
  184. }
  185. void activeEffectTrail(bool value)
  186. {
  187. this.transform.Find("EF_tuowei").gameObject.SetActive(value);
  188. this.transform.Find("EF_tuowei/Trail").GetComponent<TrailRenderer>().time = 1.6f / mySpeed;
  189. }
  190. }