Arrow.cs 7.6 KB

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