Arrow.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. using System.Collections.Generic;
  2. using UnityEngine;
  3. using DG.Tweening;
  4. /* 箭对象 */
  5. public class Arrow : MonoBehaviour
  6. {
  7. private Rigidbody newRigidbody;
  8. public Vector3 shootOutPosition;
  9. public float flyTime = 0;
  10. public bool isHit = false;
  11. public RaycastHit absoluteRay;
  12. public float 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 *= 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. SetUpBeforFly();
  36. }
  37. newRigidbody.velocity = this.transform.forward * mySpeed;
  38. newRigidbody.collisionDetectionMode = CollisionDetectionMode.ContinuousDynamic;
  39. newRigidbody.drag = 0;
  40. newRigidbody.angularDrag = 0;
  41. Transform cameraTF = this.transform.Find("Camera");
  42. cameraTF.gameObject.SetActive(true);
  43. cameraTF.gameObject.AddComponent<ArrowCamera>().arrow = this;
  44. this.activeEffectTrail(true);
  45. //是update运动,则禁用物理运动
  46. if (useUpdatePhysics) {
  47. this.GetComponent<BoxCollider>().enabled = false;
  48. Destroy(newRigidbody);
  49. }
  50. }
  51. /**在箭飞行前初始化&如果瞄准射线的落点在靶子上时,才调用该函数 */
  52. void SetUpBeforFly()
  53. {
  54. //看能否通过调整发射角让箭落在靶子的瞄准点上
  55. CalculateParabolaAngle(absoluteRay.point);
  56. if (!hasParabolaAngle) { //发射角无解,就提示用户
  57. if (AimLoadChecker.ins) AimLoadChecker.ins.ShowOutTip();
  58. return;
  59. }
  60. //来到这里,证明发射角有解,该解姑且叫它绝对角
  61. //因为Unity引擎的规则,向上时359~270度,向下是0~90度
  62. //为了方便数学运算,换算成 向上时0~90度,向下是0~-90度
  63. float baseAngleX = this.transform.eulerAngles.x;
  64. if (baseAngleX > 180) baseAngleX = baseAngleX - 360;
  65. baseAngleX *= -1;
  66. //绝对角
  67. float absoluteAngleX = parabolaAngleInRadian / Mathf.PI * 180;
  68. //客户要求绝对角跟原本角度相差不能超过 maxDeltaAngleX
  69. float deltaAngleX = absoluteAngleX - baseAngleX;
  70. float maxDeltaAngleX = 5;
  71. //最终的角还要加上偏移角度,偏移角也是客户要求加,单纯是为了增加误差,简单模式偏移角度为0
  72. float finalAngleX = this.offsetAngle;
  73. //如果绝对角跟原本角度相差超过 maxDeltaAngleX,就不能用绝对角了,直接在原本角度上加上最大差角
  74. if (Mathf.Abs(deltaAngleX) > maxDeltaAngleX) {
  75. finalAngleX += baseAngleX;
  76. finalAngleX += deltaAngleX > 0 ? maxDeltaAngleX : -maxDeltaAngleX;
  77. if (AimLoadChecker.ins) AimLoadChecker.ins.ShowOutTip();
  78. } else {//不超过就最好,直接用绝对角
  79. finalAngleX += absoluteAngleX;
  80. }
  81. //拆分两轴的速度
  82. float vy = Mathf.Sin(finalAngleX / 180 * Mathf.PI) * this.mySpeed;
  83. float vx = Mathf.Cos(finalAngleX / 180 * Mathf.PI) * this.mySpeed;
  84. //x轴距离
  85. float deltaX = Mathf.Sqrt(
  86. Mathf.Pow(absoluteRay.point.x - this.shootOutPosition.x, 2) +
  87. Mathf.Pow(absoluteRay.point.z - this.shootOutPosition.z, 2)
  88. );
  89. //x方向的耗时
  90. float tx = deltaX / vx;
  91. //最终y轴的位移
  92. float dy = vy * tx + 0.5f * Physics.gravity.y * tx * tx;
  93. float tbRadius = 0.62f;//靶子半径
  94. //之所以这样比较,是因为发射位置跟靶子中心是等高的,靶子半径是已知的
  95. if (Mathf.Abs(dy) < tbRadius) { //在靶子范围内,则用update运算物理
  96. finalPoint = absoluteRay.point;
  97. finalPoint.y = absoluteRay.transform.parent.position.y + dy;
  98. useUpdatePhysics = true;
  99. parabolaAngleInRadian = finalAngleX / 180 * Mathf.PI;
  100. RandomUseSideCamera();
  101. } else { //否则,调下角度就好,到时候用unity物理运算
  102. Vector3 eulerAngle = this.transform.eulerAngles;
  103. eulerAngle.x = -finalAngleX;
  104. this.transform.eulerAngles = eulerAngle;
  105. }
  106. }
  107. void RandomUseSideCamera() {
  108. //有几率进入侧面镜头观看
  109. float distance = TargetBody.ins.GetDistance();
  110. if (Mathf.RoundToInt(distance) >= 50) {
  111. if (Random.value < 0.5) simulatedParabola = true;
  112. }
  113. }
  114. /**发射角有无解 */
  115. bool hasParabolaAngle = false;
  116. /**发射角弧度解 */
  117. float parabolaAngleInRadian = 0;
  118. /**
  119. 求弓箭发射到指定坐标所需要的角度。
  120. 已知初速度大小V,重力g,起始坐标(a1,a2),目标坐标(b1,b2)。
  121. 解:
  122. 1、列出关系式
  123. Δx = b1 - a1;
  124. Δy = b2 - a2;
  125. Vx = V * cos(angle)
  126. Vy = V * sin(angle)
  127. Vy * t + 1/2 * g * t^2 = Δy
  128. Vx * t = Δx
  129. t = Δx / Vx
  130. 2、推导过程
  131. (V * sin(angle)) * Δx / (V * cos(angle)) + 1/2 * g * Δx^2 / (V^2*cos(angle)^2) = Δy
  132. tan(angle) * Δx + 1/2 * g * Δx^2 / (V^2*cos(angle)^2) = Δy
  133. tan(angle) * Δx + 1/2 * g * (Δx^2 / V^2) * (1 + tan(angle)^2) = Δy
  134. 3、根据求根公式得出结论
  135. a = 1/2 * g * Δx^2 / V^2
  136. b = Δx
  137. c = a - Δy
  138. d = tan(angle) = (-b ± (b^2 - 4*a*c)^0.5) / (2*a)
  139. angle = atan(d)
  140. 有无根的判别式,有根则b^2-4*a*c>=0
  141. */
  142. void CalculateParabolaAngle(Vector3 destination)
  143. {
  144. float deltaX = Vector2.Distance(
  145. new Vector2(destination.x, destination.z),
  146. new Vector2(this.transform.position.x, this.transform.position.z)
  147. );
  148. float deltaY = destination.y - this.transform.position.y;
  149. float a = 0.5f * Physics.gravity.y * Mathf.Pow(deltaX, 2) / Mathf.Pow(this.mySpeed, 2);
  150. float b = deltaX;
  151. float c = a - deltaY;
  152. hasParabolaAngle = Mathf.Pow(b, 2) - 4 * a * c >= 0;
  153. if (hasParabolaAngle) {
  154. float res1 = (-b + Mathf.Pow(Mathf.Pow(b, 2) - 4*a*c, 0.5f)) / (2 * a);
  155. float res2 = (-b - Mathf.Pow(Mathf.Pow(b, 2) - 4*a*c, 0.5f)) / (2 * a);
  156. parabolaAngleInRadian = Mathf.Min(Mathf.Atan(res1), Mathf.Atan(res2));
  157. }
  158. }
  159. void OnDestroy() {
  160. GameMgr.ins.gameMode.ResumeTimeCounting(this);
  161. if (this.newRigidbody)
  162. {
  163. Destroy(this.newRigidbody);
  164. }
  165. }
  166. void FixedUpdate()
  167. {
  168. if (newRigidbody) {
  169. transform.forward = newRigidbody.velocity.normalized;
  170. }
  171. if (!isHit && flyTime >= 0) {
  172. flyTime += Time.deltaTime;
  173. if (flyTime > 14) {
  174. Destroy(gameObject);
  175. GameMgr.ins.gameMode.HitTarget(0);
  176. AudioMgr.ins.PlayCheer(false);
  177. nextShoot();
  178. }
  179. this.UpdateRotate();
  180. }
  181. // this.UpdateShake();
  182. }
  183. void Update() {
  184. UpdateFlyWithoutPhysics();
  185. }
  186. bool useUpdatePhysics = false;
  187. float flyTimeWithoutPhysics = 0;
  188. Vector3 finalPoint = default;
  189. /**更新箭的飞行轨迹(非Unity物理运算)*/
  190. void UpdateFlyWithoutPhysics() {
  191. if (!useUpdatePhysics || isHit) return;
  192. flyTimeWithoutPhysics += Time.deltaTime;
  193. Vector3 destination = finalPoint;
  194. float deltaX = Mathf.Sqrt(
  195. Mathf.Pow(destination.x - shootOutPosition.x, 2) +
  196. Mathf.Pow(destination.z - shootOutPosition.z, 2)
  197. );
  198. float vx = Mathf.Cos(parabolaAngleInRadian) * mySpeed;
  199. float t = deltaX / vx;
  200. if (flyTimeWithoutPhysics < t) {
  201. t = flyTimeWithoutPhysics;
  202. }
  203. float vy = Mathf.Sin(parabolaAngleInRadian) * mySpeed + Physics.gravity.y * t;
  204. float dy = Mathf.Sin(parabolaAngleInRadian) * mySpeed * t + 0.5f * Physics.gravity.y * Mathf.Pow(t, 2);
  205. float dx = vx * t;
  206. Vector3 nextPosition = new Vector3(destination.x - shootOutPosition.x, 0, destination.z - shootOutPosition.z);
  207. nextPosition = nextPosition.normalized * dx;
  208. nextPosition.y = shootOutPosition.y + dy;
  209. this.transform.position = nextPosition;
  210. Vector3 eulerAngles = this.transform.eulerAngles;
  211. float angleX = Mathf.Atan(vy / vx) / Mathf.PI * 180;
  212. eulerAngles.x = -angleX;
  213. this.transform.eulerAngles = eulerAngles;
  214. //箭头到达目标点时,触发碰撞(之所以用以下,是因为箭的锚点在尾部,而箭的长度接近1.1)
  215. if (Vector3.Distance(nextPosition, destination) < 1.1f) {
  216. this.transform.SetParent(TargetBody.ins.transform);
  217. TargetBody.ins.Hit(this, destination);
  218. }
  219. }
  220. public void Hit() {
  221. if (GameDebug.ins) GameDebug.ins.ShowRes(absoluteRay.point, this.getHeadPosition());
  222. gameObject.GetComponent<BoxCollider>().enabled = false;
  223. if (newRigidbody) {
  224. newRigidbody.useGravity = false;
  225. newRigidbody.velocity = Vector3.zero;
  226. Destroy(newRigidbody);
  227. newRigidbody = null;
  228. }
  229. isHit = true;
  230. // this.Shake();
  231. this.activeEffectCyclone(false);
  232. this.activeEffectBomb(true);
  233. this.activeEffectTrail(false);
  234. //最新一箭击中后会发光标记
  235. ArrowLightSick.RecoveryAll();
  236. this.GetComponentInChildren<ArrowLightSick>().Hit();
  237. }
  238. public Vector3 getHeadPosition() {
  239. return this.transform.Find("Head").position;
  240. }
  241. /**箭矢旋转 */
  242. private Vector3 rotateV3 = new Vector3(0, 0, 1400);
  243. private void UpdateRotate() {
  244. this.transform.Find("Head").Rotate(rotateV3 * Time.deltaTime, Space.Self);
  245. }
  246. /**箭矢震动 */
  247. private bool shaking = false;
  248. private float shakeTime = 0;
  249. private float shakeTimeMax = 0.5f;
  250. private float shakeRange = 5;
  251. private int shakeDirection = 1;
  252. private Vector3 shakeAngles = new Vector3();
  253. private void Shake()
  254. {
  255. this.shaking = true;
  256. this.shakeTime = 0;
  257. this.shakeDirection = 1;
  258. }
  259. private void UpdateShake()
  260. {
  261. if (!this.shaking) {
  262. return;
  263. }
  264. Transform transform = this.transform.Find("Head").transform;
  265. float shakeRangeNow = (1 - this.shakeTime / this.shakeTimeMax) * this.shakeRange;
  266. if (shakeRangeNow <= 0) {//震动结束
  267. this.shaking = false;
  268. this.shakeAngles.x = 0;
  269. } else {
  270. this.shakeAngles.x = -this.shakeDirection * shakeRangeNow;
  271. this.shakeDirection *= -1;
  272. }
  273. transform.localEulerAngles = this.shakeAngles;
  274. this.shakeTime += Time.deltaTime;
  275. }
  276. //进入下一轮射击
  277. public bool hasDoneNextShoot = false;
  278. public void nextShoot() {
  279. if (hasDoneNextShoot) return;
  280. hasDoneNextShoot = true;
  281. GameMgr.ins.gameMode.ResumeTimeCounting(this);
  282. if (!GameMgr.ins.gameMode.DoNextShoot()) return;
  283. if (AimHandler.ins) AimHandler.ins.BanControlObjRotate(false);
  284. this.armBow.readyShoot();
  285. //把瞄准点画成红圈,渲染在靶子上(取消)
  286. Transform redCircle = TargetBody.ins.transform.Find("RedCircle");
  287. redCircle.gameObject.SetActive(false);
  288. //最新一箭击中后会发光标记(取消)
  289. ArrowLightSick.RecoveryAll();
  290. }
  291. void OnCollisionEnter(Collision collision) {
  292. if ((1 << collision.gameObject.layer) != LayerMask.GetMask("Target"))
  293. {
  294. this.Hit();
  295. GameMgr.ins.gameMode.HitTarget(0);
  296. AudioMgr.ins.PlayCheer(false);
  297. }
  298. this.transform.SetParent(collision.transform.parent);
  299. }
  300. public void activeEffectCyclone(bool value)
  301. {
  302. this.transform.Find("Head/EF_kuosanquan").gameObject.SetActive(value);
  303. if (!value) return;
  304. ParticleSystemRenderer ps = this.transform.Find("Head/EF_kuosanquan/kuosan").GetComponent<ParticleSystemRenderer>();
  305. ParticleSystemRenderer ps1 = this.transform.Find("Head/EF_kuosanquan/kuosan (1)").GetComponent<ParticleSystemRenderer>();
  306. DOTween.To(() => ps.minParticleSize, value => {
  307. ps.minParticleSize = value;
  308. ps.maxParticleSize = value;
  309. }, 0.4f, 0.6f);
  310. DOTween.To(() => ps1.minParticleSize, value => {
  311. ps1.minParticleSize = value;
  312. ps1.maxParticleSize = value;
  313. }, 0.8f, 0.6f);
  314. }
  315. void activeEffectBomb(bool value)
  316. {
  317. this.transform.Find("Head/EF_baodian").gameObject.SetActive(value);
  318. }
  319. void activeEffectTrail(bool value)
  320. {
  321. this.transform.Find("EF_tuowei").gameObject.SetActive(value);
  322. this.transform.Find("EF_tuowei/Trail").GetComponent<TrailRenderer>().time = 1.6f / mySpeed;
  323. }
  324. }