Arrow.cs 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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. public RaycastHit raycastHit1;
  12. private float mySpeed = 0;
  13. public bool simulatedParabola = false; //模拟抛物线
  14. public ArmBow armBow;
  15. public static float speed = GameMgr.RealSizeToGameSize(60);
  16. void Awake()
  17. {
  18. GameMgr.ins.gameMode.PauseTimeCounting(this);
  19. }
  20. void Start()
  21. {
  22. newRigidbody = this.gameObject.AddComponent<Rigidbody>();
  23. mySpeed = speed;
  24. if (GameAssistUI.ins) {
  25. mySpeed *= (1 + GameAssistUI.ins.shootScaleValue);
  26. }
  27. if (raycastHit.transform && raycastHit.transform.gameObject.name == "TargetBody") {
  28. //把瞄准点画成红圈,渲染在靶子上
  29. Transform redCircle = TargetBody.ins.transform.Find("RedCircle");
  30. redCircle.gameObject.SetActive(true);
  31. redCircle.transform.position = -redCircle.transform.forward * 0.001f + raycastHit.point;
  32. }
  33. //目标落点在靶子时,调整仰角,走Update模式
  34. if (raycastHit1.transform && raycastHit1.transform.gameObject.name == "TargetBody") {
  35. ApplyParabolaAngle(raycastHit1.point);
  36. //有几率进入抛物线观看镜头
  37. float distance = TargetBody.ins.GetDistance();
  38. if (Mathf.RoundToInt(distance) >= 50) {
  39. if (Random.value < 0.5) simulatedParabola = true;
  40. }
  41. } else {
  42. //目标落点不在靶子,瞄准点在靶子时,调整仰角,走物理模式
  43. if (raycastHit.transform && raycastHit.transform.gameObject.name == "TargetBody") {
  44. ApplyParabolaAngle(raycastHit.point);
  45. if (hasParabolaAngle) {
  46. hasParabolaAngle = false;
  47. float angleX = parabolaAngleInRadius / Mathf.PI * 180;
  48. Vector3 eulerAngle = this.transform.eulerAngles;
  49. eulerAngle.x = -angleX;
  50. this.transform.eulerAngles = eulerAngle;
  51. }
  52. }
  53. }
  54. newRigidbody.velocity = this.transform.forward * mySpeed;
  55. newRigidbody.collisionDetectionMode = CollisionDetectionMode.ContinuousDynamic;
  56. Transform cameraTF = this.transform.Find("Camera");
  57. cameraTF.gameObject.SetActive(true);
  58. cameraTF.gameObject.AddComponent<ArrowCamera>().arrow = this;
  59. this.activeEffectTrail(true);
  60. //如果有自动调整发射角,则禁用物理运动
  61. if (hasParabolaAngle) {
  62. this.GetComponent<BoxCollider>().enabled = false;
  63. Destroy(newRigidbody);
  64. }
  65. }
  66. bool hasParabolaAngle = false;
  67. float parabolaAngleInRadius = 0;
  68. void ApplyParabolaAngle(Vector3 destination)
  69. {
  70. float deltaX = Vector2.Distance(
  71. new Vector2(destination.x, destination.z),
  72. new Vector2(this.transform.position.x, this.transform.position.z)
  73. );
  74. float deltaY = destination.y - this.transform.position.y;
  75. float a = 0.5f * Physics.gravity.y * Mathf.Pow(deltaX, 2) / Mathf.Pow(this.mySpeed, 2);
  76. float b = deltaX;
  77. float c = a - deltaY;
  78. hasParabolaAngle = Mathf.Pow(b, 2) - 4 * a * c >= 0;
  79. if (hasParabolaAngle) {
  80. float res1 = (-b + Mathf.Pow(Mathf.Pow(b, 2) - 4*a*c, 0.5f)) / (2 * a);
  81. float res2 = (-b - Mathf.Pow(Mathf.Pow(b, 2) - 4*a*c, 0.5f)) / (2 * a);
  82. parabolaAngleInRadius = Mathf.Min(Mathf.Atan(res1), Mathf.Atan(res2));
  83. }
  84. }
  85. void OnDestroy() {
  86. GameMgr.ins.gameMode.ResumeTimeCounting(this);
  87. if (this.newRigidbody)
  88. {
  89. Destroy(this.newRigidbody);
  90. }
  91. }
  92. Vector3 position;
  93. Quaternion rotation;
  94. void FixedUpdate()
  95. {
  96. if (newRigidbody) {
  97. transform.forward = newRigidbody.velocity.normalized;
  98. }
  99. if (!isHit && flyTime >= 0) {
  100. flyTime += Time.deltaTime;
  101. this.position = this.transform.position;
  102. this.rotation = this.transform.rotation;
  103. if (flyTime > 14) {
  104. Destroy(gameObject);
  105. GameMgr.ins.gameMode.HitTarget(0);
  106. AudioMgr.ins.PlayCheer(false);
  107. nextShoot();
  108. }
  109. this.UpdateRotate();
  110. }
  111. // this.UpdateShake();
  112. }
  113. void Update() {
  114. UpdateFlyWithoutPhysics();
  115. }
  116. float flyTimeWithoutPhysics = 0;
  117. void UpdateFlyWithoutPhysics() {
  118. if (!hasParabolaAngle || isHit) return;
  119. flyTimeWithoutPhysics += Time.deltaTime;
  120. Vector3 destination = raycastHit1.point;
  121. float deltaX = Mathf.Sqrt(
  122. Mathf.Pow(destination.x - shootOutPosition.x, 2) +
  123. Mathf.Pow(destination.z - shootOutPosition.z, 2)
  124. );
  125. float vx = Mathf.Cos(parabolaAngleInRadius) * mySpeed;
  126. float t = deltaX / vx;
  127. if (flyTimeWithoutPhysics < t) {
  128. t = flyTimeWithoutPhysics;
  129. }
  130. float vy = Mathf.Sin(parabolaAngleInRadius) * mySpeed + Physics.gravity.y * t;
  131. float dy = Mathf.Sin(parabolaAngleInRadius) * mySpeed * t + 0.5f * Physics.gravity.y * Mathf.Pow(t, 2);
  132. float dx = vx * t;
  133. Vector3 nextPosition = new Vector3(destination.x - shootOutPosition.x, 0, destination.z - shootOutPosition.z);
  134. nextPosition = nextPosition.normalized * dx;
  135. nextPosition.y = shootOutPosition.y + dy;
  136. this.transform.position = nextPosition;
  137. Vector3 eulerAngles = this.transform.eulerAngles;
  138. float angleX = Mathf.Atan(vy / vx) / Mathf.PI * 180;
  139. eulerAngles.x = -angleX;
  140. this.transform.eulerAngles = eulerAngles;
  141. //箭头到达目标点时,触发碰撞(之所以用以下,是因为箭的锚点在尾部,而箭的长度接近1.1)
  142. if (Vector3.Distance(nextPosition, destination) < 1.1f) {
  143. TargetBody.ins.Hit(this, destination);
  144. }
  145. }
  146. public void Hit() {
  147. gameObject.GetComponent<BoxCollider>().enabled = false;
  148. if (newRigidbody) {
  149. newRigidbody.useGravity = false;
  150. newRigidbody.velocity = Vector3.zero;
  151. Destroy(newRigidbody);
  152. newRigidbody = null;
  153. }
  154. isHit = true;
  155. // this.Shake();
  156. this.activeEffectCyclone(false);
  157. this.activeEffectBomb(true);
  158. this.activeEffectTrail(false);
  159. //最新一箭击中后会发光标记
  160. ArrowLightSick.RecoveryAll();
  161. this.GetComponentInChildren<ArrowLightSick>().Hit();
  162. }
  163. public Vector3 getHeadPosition() {
  164. return this.transform.Find("Head").position;
  165. }
  166. /**箭矢旋转 */
  167. private Vector3 rotateV3 = new Vector3(0, 0, 1400);
  168. private void UpdateRotate() {
  169. this.transform.Find("Head").Rotate(rotateV3 * Time.deltaTime, Space.Self);
  170. }
  171. /**箭矢震动 */
  172. private bool shaking = false;
  173. private float shakeTime = 0;
  174. private float shakeTimeMax = 0.5f;
  175. private float shakeRange = 5;
  176. private int shakeDirection = 1;
  177. private Vector3 shakeAngles = new Vector3();
  178. private void Shake()
  179. {
  180. this.shaking = true;
  181. this.shakeTime = 0;
  182. this.shakeDirection = 1;
  183. }
  184. private void UpdateShake()
  185. {
  186. if (!this.shaking) {
  187. return;
  188. }
  189. Transform transform = this.transform.Find("Head").transform;
  190. float shakeRangeNow = (1 - this.shakeTime / this.shakeTimeMax) * this.shakeRange;
  191. if (shakeRangeNow <= 0) {//震动结束
  192. this.shaking = false;
  193. this.shakeAngles.x = 0;
  194. } else {
  195. this.shakeAngles.x = -this.shakeDirection * shakeRangeNow;
  196. this.shakeDirection *= -1;
  197. }
  198. transform.localEulerAngles = this.shakeAngles;
  199. this.shakeTime += Time.deltaTime;
  200. }
  201. //进入下一轮射击
  202. bool hasDoneNextShoot = false;
  203. public void nextShoot() {
  204. if (hasDoneNextShoot) return;
  205. hasDoneNextShoot = true;
  206. GameMgr.ins.gameMode.ResumeTimeCounting(this);
  207. if (!GameMgr.ins.gameMode.DoNextShoot()) return;
  208. this.armBow.readyShoot();
  209. //把瞄准点画成红圈,渲染在靶子上(取消)
  210. Transform redCircle = TargetBody.ins.transform.Find("RedCircle");
  211. redCircle.gameObject.SetActive(false);
  212. //最新一箭击中后会发光标记(取消)
  213. ArrowLightSick.RecoveryAll();
  214. }
  215. void OnCollisionEnter(Collision collision) {
  216. if ((1 << collision.gameObject.layer) != LayerMask.GetMask("Target"))
  217. {
  218. this.Hit();
  219. GameMgr.ins.gameMode.HitTarget(0);
  220. AudioMgr.ins.PlayCheer(false);
  221. }
  222. this.transform.SetParent(collision.transform.parent);
  223. }
  224. public void activeEffectCyclone(bool value)
  225. {
  226. this.transform.Find("Head/EF_kuosanquan").gameObject.SetActive(value);
  227. if (!value) return;
  228. ParticleSystemRenderer ps = this.transform.Find("Head/EF_kuosanquan/kuosan").GetComponent<ParticleSystemRenderer>();
  229. ParticleSystemRenderer ps1 = this.transform.Find("Head/EF_kuosanquan/kuosan (1)").GetComponent<ParticleSystemRenderer>();
  230. DOTween.To(() => ps.minParticleSize, value => {
  231. ps.minParticleSize = value;
  232. ps.maxParticleSize = value;
  233. }, 0.4f, 0.6f);
  234. DOTween.To(() => ps1.minParticleSize, value => {
  235. ps1.minParticleSize = value;
  236. ps1.maxParticleSize = value;
  237. }, 0.8f, 0.6f);
  238. }
  239. void activeEffectBomb(bool value)
  240. {
  241. this.transform.Find("Head/EF_baodian").gameObject.SetActive(value);
  242. }
  243. void activeEffectTrail(bool value)
  244. {
  245. this.transform.Find("EF_tuowei").gameObject.SetActive(value);
  246. this.transform.Find("EF_tuowei/Trail").GetComponent<TrailRenderer>().time = 1.6f / mySpeed;
  247. }
  248. }