Arrow.cs 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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.gameObject.name == "TargetBody") {
  28. // Vector3 targetPosition = raycastHit.transform.position;
  29. // Vector3 tailPosition = this.transform.position;
  30. // float moveDistance = Vector3.Distance(targetPosition, tailPosition);
  31. // float canFlyTime = Mathf.Sqrt(tailPosition.y * 2 / 9.81f);
  32. // float estimateTime = moveDistance / mySpeed;
  33. // // if (estimateTime < canFlyTime) {
  34. // // float t = 1.5f;
  35. // // float g = 9.81f;
  36. // // float vy = g * t / 2;
  37. // // float vx = moveDistance / t;
  38. // // newRigidbody.velocity = new Vector3(0, vy, vx);
  39. // // mySpeed = newRigidbody.velocity.magnitude;
  40. // // }
  41. ApplyParabolaAngle(raycastHit.point);
  42. float distance = TargetBody.ins.GetDistance();
  43. if (Mathf.RoundToInt(distance) >= 50) {
  44. if (Random.value < 0.5) simulatedParabola = true;
  45. }
  46. }
  47. newRigidbody.velocity = this.transform.forward * mySpeed;
  48. newRigidbody.collisionDetectionMode = CollisionDetectionMode.ContinuousDynamic;
  49. Transform cameraTF = this.transform.Find("Camera");
  50. cameraTF.gameObject.SetActive(true);
  51. cameraTF.gameObject.AddComponent<ArrowCamera>().arrow = this;
  52. this.activeEffectTrail(true);
  53. }
  54. void ApplyParabolaAngle(Vector3 destination)
  55. {
  56. float angleX = 0;
  57. float distX = Vector2.Distance(
  58. new Vector2(destination.x, destination.z),
  59. new Vector2(this.transform.position.x, this.transform.position.z)
  60. );
  61. float distY = destination.y - this.transform.position.y;
  62. float posBase = (Physics.gravity.y * Mathf.Pow(distX, 2.0f)) / (2.0f * Mathf.Pow(mySpeed, 2.0f));
  63. float posX = distX / posBase;
  64. float posY = (Mathf.Pow(posX, 2.0f) / 4.0f) - ((posBase - distY) / posBase);
  65. bool paoshe = false;
  66. if (posY >= 0.0f)
  67. {
  68. if (paoshe)
  69. angleX = Mathf.Rad2Deg * Mathf.Atan(-posX / 2.0f + Mathf.Pow(posY, 0.5f));
  70. else
  71. angleX = Mathf.Rad2Deg * Mathf.Atan(-posX / 2.0f - Mathf.Pow(posY, 0.5f));
  72. }
  73. else
  74. {
  75. angleX = 45.0f;
  76. }
  77. Debug.LogWarning(angleX);
  78. // bool hasBestAngle = false;
  79. // for (float angle = -5.0f; angle < 20.0f; angle += 0.3f) {
  80. // float vx = mySpeed * Mathf.Cos(angle / Mathf.Rad2Deg);
  81. // float vy = mySpeed * Mathf.Sin(angle / Mathf.Rad2Deg);
  82. // // vx * t = distX;
  83. // float t = distX / vx;
  84. // // vy * t + 1/2 * Physics.gravity.y * Mathf.Pow(t, 2) = distY;
  85. // float realY = vy * t + 1/2 * Physics.gravity.y * Mathf.Pow(t, 2);
  86. // if (Mathf.Abs(realY - distY) < 0.3f) {
  87. // angleX = angle;
  88. // hasBestAngle = true;
  89. // // Debug.Log("disY" + Mathf.Abs(realY - distY) + " angleX:" + angleX + " realY:" + realY + " distY:" + distY);
  90. // Debug.Log()
  91. // break;
  92. // }
  93. // }
  94. // if (hasBestAngle) {
  95. // // Debug.Log(angleX);
  96. // } else {
  97. // angleX = 20;
  98. // Debug.Log("AAA");
  99. // }
  100. Vector3 finalAngle = this.transform.eulerAngles;
  101. finalAngle = new Vector3(-angleX, finalAngle.y, finalAngle.z);
  102. this.transform.localRotation = Quaternion.Euler(finalAngle);
  103. }
  104. void OnDestroy() {
  105. GameMgr.ins.gameMode.ResumeTimeCounting(this);
  106. if (this.newRigidbody)
  107. {
  108. Destroy(this.newRigidbody);
  109. }
  110. }
  111. Vector3 position;
  112. Quaternion rotation;
  113. void FixedUpdate()
  114. {
  115. if (newRigidbody) {
  116. transform.forward = newRigidbody.velocity.normalized;
  117. }
  118. if (!isHit && flyTime >= 0) {
  119. flyTime += Time.deltaTime;
  120. this.position = this.transform.position;
  121. this.rotation = this.transform.rotation;
  122. if (flyTime > 14) {
  123. Destroy(gameObject);
  124. GameMgr.ins.gameMode.HitTarget(0);
  125. AudioMgr.ins.PlayCheer(false);
  126. nextShoot();
  127. }
  128. this.UpdateRotate();
  129. }
  130. // this.UpdateShake();
  131. }
  132. public void Hit() {
  133. gameObject.GetComponent<BoxCollider>().enabled = false;
  134. if (newRigidbody) {
  135. newRigidbody.useGravity = false;
  136. newRigidbody.velocity = Vector3.zero;
  137. Destroy(newRigidbody);
  138. newRigidbody = null;
  139. }
  140. isHit = true;
  141. // this.Shake();
  142. this.activeEffectCyclone(false);
  143. this.activeEffectBomb(true);
  144. this.activeEffectTrail(false);
  145. }
  146. public Vector3 getHeadPosition() {
  147. return this.transform.Find("Head").position;
  148. }
  149. /**箭矢旋转 */
  150. private Vector3 rotateV3 = new Vector3(0, 0, 1400);
  151. private void UpdateRotate() {
  152. this.transform.Find("Head").Rotate(rotateV3 * Time.deltaTime, Space.Self);
  153. }
  154. /**箭矢震动 */
  155. private bool shaking = false;
  156. private float shakeTime = 0;
  157. private float shakeTimeMax = 0.5f;
  158. private float shakeRange = 5;
  159. private int shakeDirection = 1;
  160. private Vector3 shakeAngles = new Vector3();
  161. private void Shake()
  162. {
  163. this.shaking = true;
  164. this.shakeTime = 0;
  165. this.shakeDirection = 1;
  166. }
  167. private void UpdateShake()
  168. {
  169. if (!this.shaking) {
  170. return;
  171. }
  172. Transform transform = this.transform.Find("Head").transform;
  173. float shakeRangeNow = (1 - this.shakeTime / this.shakeTimeMax) * this.shakeRange;
  174. if (shakeRangeNow <= 0) {//震动结束
  175. this.shaking = false;
  176. this.shakeAngles.x = 0;
  177. } else {
  178. this.shakeAngles.x = -this.shakeDirection * shakeRangeNow;
  179. this.shakeDirection *= -1;
  180. }
  181. transform.localEulerAngles = this.shakeAngles;
  182. this.shakeTime += Time.deltaTime;
  183. }
  184. //进入下一轮射击
  185. bool hasDoneNextShoot = false;
  186. public void nextShoot() {
  187. if (hasDoneNextShoot) return;
  188. hasDoneNextShoot = true;
  189. GameMgr.ins.gameMode.ResumeTimeCounting(this);
  190. if (!GameMgr.ins.gameMode.DoNextShoot()) return;
  191. this.armBow.readyShoot();
  192. }
  193. void OnCollisionEnter(Collision collision) {
  194. if ((1 << collision.gameObject.layer) != LayerMask.GetMask("Target"))
  195. {
  196. this.Hit();
  197. GameMgr.ins.gameMode.HitTarget(0);
  198. AudioMgr.ins.PlayCheer(false);
  199. }
  200. this.transform.SetParent(collision.transform.parent);
  201. }
  202. public void activeEffectCyclone(bool value)
  203. {
  204. this.transform.Find("Head/EF_kuosanquan").gameObject.SetActive(value);
  205. if (!value) return;
  206. ParticleSystemRenderer ps = this.transform.Find("Head/EF_kuosanquan/kuosan").GetComponent<ParticleSystemRenderer>();
  207. ParticleSystemRenderer ps1 = this.transform.Find("Head/EF_kuosanquan/kuosan (1)").GetComponent<ParticleSystemRenderer>();
  208. DOTween.To(() => ps.minParticleSize, value => {
  209. ps.minParticleSize = value;
  210. ps.maxParticleSize = value;
  211. }, 0.4f, 0.6f);
  212. DOTween.To(() => ps1.minParticleSize, value => {
  213. ps1.minParticleSize = value;
  214. ps1.maxParticleSize = value;
  215. }, 0.8f, 0.6f);
  216. }
  217. void activeEffectBomb(bool value)
  218. {
  219. this.transform.Find("Head/EF_baodian").gameObject.SetActive(value);
  220. }
  221. void activeEffectTrail(bool value)
  222. {
  223. this.transform.Find("EF_tuowei").gameObject.SetActive(value);
  224. this.transform.Find("EF_tuowei/Trail").GetComponent<TrailRenderer>().time = 1.6f / mySpeed;
  225. }
  226. }