Arrow.cs 11 KB

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