Arrow.cs 11 KB

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