Arrow.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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. Transform redCircle = TargetBody.ins.transform.Find("RedCircle");
  35. redCircle.gameObject.SetActive(true);
  36. redCircle.transform.position = -redCircle.transform.forward * 0.001f + raycastHit.point;
  37. }
  38. newRigidbody.velocity = this.transform.forward * mySpeed;
  39. newRigidbody.collisionDetectionMode = CollisionDetectionMode.ContinuousDynamic;
  40. Transform cameraTF = this.transform.Find("Camera");
  41. cameraTF.gameObject.SetActive(true);
  42. cameraTF.gameObject.AddComponent<ArrowCamera>().arrow = this;
  43. this.activeEffectTrail(true);
  44. }
  45. void ApplyParabolaAngle(Vector3 destination)
  46. {
  47. float angleX = 0;
  48. Vector3 backVec = -this.transform.forward * 0.5f; //如果实际落点在上下方向出现了偏移,可以调整该参数
  49. float distX = Vector2.Distance(
  50. new Vector2(destination.x, destination.z),
  51. new Vector2(this.transform.position.x + backVec.x, this.transform.position.z + backVec.z)
  52. );
  53. float distY = destination.y - (this.transform.position.y + backVec.y);
  54. float posBase = (Physics.gravity.y * Mathf.Pow(distX, 2.0f)) / (2.0f * Mathf.Pow(mySpeed, 2.0f));
  55. float posX = distX / posBase;
  56. float posY = (Mathf.Pow(posX, 2.0f) / 4.0f) - ((posBase - distY) / posBase);
  57. bool paoshe = false;
  58. if (posY >= 0.0f)
  59. {
  60. if (paoshe)
  61. angleX = Mathf.Rad2Deg * Mathf.Atan(-posX / 2.0f + Mathf.Pow(posY, 0.5f));
  62. else
  63. angleX = Mathf.Rad2Deg * Mathf.Atan(-posX / 2.0f - Mathf.Pow(posY, 0.5f));
  64. }
  65. else
  66. {
  67. return;
  68. }
  69. Vector3 finalAngle = this.transform.eulerAngles;
  70. finalAngle = new Vector3(-angleX, finalAngle.y, finalAngle.z);
  71. this.transform.localRotation = Quaternion.Euler(finalAngle);
  72. }
  73. void OnDestroy() {
  74. GameMgr.ins.gameMode.ResumeTimeCounting(this);
  75. if (this.newRigidbody)
  76. {
  77. Destroy(this.newRigidbody);
  78. }
  79. }
  80. Vector3 position;
  81. Quaternion rotation;
  82. void FixedUpdate()
  83. {
  84. if (newRigidbody) {
  85. transform.forward = newRigidbody.velocity.normalized;
  86. }
  87. if (!isHit && flyTime >= 0) {
  88. flyTime += Time.deltaTime;
  89. this.position = this.transform.position;
  90. this.rotation = this.transform.rotation;
  91. if (flyTime > 14) {
  92. Destroy(gameObject);
  93. GameMgr.ins.gameMode.HitTarget(0);
  94. AudioMgr.ins.PlayCheer(false);
  95. nextShoot();
  96. }
  97. this.UpdateRotate();
  98. }
  99. // this.UpdateShake();
  100. }
  101. public void Hit() {
  102. gameObject.GetComponent<BoxCollider>().enabled = false;
  103. if (newRigidbody) {
  104. newRigidbody.useGravity = false;
  105. newRigidbody.velocity = Vector3.zero;
  106. Destroy(newRigidbody);
  107. newRigidbody = null;
  108. }
  109. isHit = true;
  110. // this.Shake();
  111. this.activeEffectCyclone(false);
  112. this.activeEffectBomb(true);
  113. this.activeEffectTrail(false);
  114. //最新一箭击中后会发光标记
  115. ArrowLightSick.RecoveryAll();
  116. this.GetComponentInChildren<ArrowLightSick>().Hit();
  117. }
  118. public Vector3 getHeadPosition() {
  119. return this.transform.Find("Head").position;
  120. }
  121. /**箭矢旋转 */
  122. private Vector3 rotateV3 = new Vector3(0, 0, 1400);
  123. private void UpdateRotate() {
  124. this.transform.Find("Head").Rotate(rotateV3 * Time.deltaTime, Space.Self);
  125. }
  126. /**箭矢震动 */
  127. private bool shaking = false;
  128. private float shakeTime = 0;
  129. private float shakeTimeMax = 0.5f;
  130. private float shakeRange = 5;
  131. private int shakeDirection = 1;
  132. private Vector3 shakeAngles = new Vector3();
  133. private void Shake()
  134. {
  135. this.shaking = true;
  136. this.shakeTime = 0;
  137. this.shakeDirection = 1;
  138. }
  139. private void UpdateShake()
  140. {
  141. if (!this.shaking) {
  142. return;
  143. }
  144. Transform transform = this.transform.Find("Head").transform;
  145. float shakeRangeNow = (1 - this.shakeTime / this.shakeTimeMax) * this.shakeRange;
  146. if (shakeRangeNow <= 0) {//震动结束
  147. this.shaking = false;
  148. this.shakeAngles.x = 0;
  149. } else {
  150. this.shakeAngles.x = -this.shakeDirection * shakeRangeNow;
  151. this.shakeDirection *= -1;
  152. }
  153. transform.localEulerAngles = this.shakeAngles;
  154. this.shakeTime += Time.deltaTime;
  155. }
  156. //进入下一轮射击
  157. bool hasDoneNextShoot = false;
  158. public void nextShoot() {
  159. if (hasDoneNextShoot) return;
  160. hasDoneNextShoot = true;
  161. GameMgr.ins.gameMode.ResumeTimeCounting(this);
  162. if (!GameMgr.ins.gameMode.DoNextShoot()) return;
  163. this.armBow.readyShoot();
  164. //把瞄准点画成红圈,渲染在靶子上(取消)
  165. Transform redCircle = TargetBody.ins.transform.Find("RedCircle");
  166. redCircle.gameObject.SetActive(false);
  167. //最新一箭击中后会发光标记(取消)
  168. ArrowLightSick.RecoveryAll();
  169. }
  170. void OnCollisionEnter(Collision collision) {
  171. if ((1 << collision.gameObject.layer) != LayerMask.GetMask("Target"))
  172. {
  173. this.Hit();
  174. GameMgr.ins.gameMode.HitTarget(0);
  175. AudioMgr.ins.PlayCheer(false);
  176. }
  177. this.transform.SetParent(collision.transform.parent);
  178. }
  179. public void activeEffectCyclone(bool value)
  180. {
  181. this.transform.Find("Head/EF_kuosanquan").gameObject.SetActive(value);
  182. if (!value) return;
  183. ParticleSystemRenderer ps = this.transform.Find("Head/EF_kuosanquan/kuosan").GetComponent<ParticleSystemRenderer>();
  184. ParticleSystemRenderer ps1 = this.transform.Find("Head/EF_kuosanquan/kuosan (1)").GetComponent<ParticleSystemRenderer>();
  185. DOTween.To(() => ps.minParticleSize, value => {
  186. ps.minParticleSize = value;
  187. ps.maxParticleSize = value;
  188. }, 0.4f, 0.6f);
  189. DOTween.To(() => ps1.minParticleSize, value => {
  190. ps1.minParticleSize = value;
  191. ps1.maxParticleSize = value;
  192. }, 0.8f, 0.6f);
  193. }
  194. void activeEffectBomb(bool value)
  195. {
  196. this.transform.Find("Head/EF_baodian").gameObject.SetActive(value);
  197. }
  198. void activeEffectTrail(bool value)
  199. {
  200. this.transform.Find("EF_tuowei").gameObject.SetActive(value);
  201. this.transform.Find("EF_tuowei/Trail").GetComponent<TrailRenderer>().time = 1.6f / mySpeed;
  202. }
  203. }