Arrow.cs 11 KB

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