Arrow.cs 10.0 KB

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