ArrowNew.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using DG.Tweening;
  6. /* 箭对象 */
  7. public class ArrowNew : MonoBehaviour
  8. {
  9. //飞行时间统计
  10. [NonSerialized] public float flyTime = 0;
  11. //标识—是否击中了什么
  12. [NonSerialized] public bool isHit = false;
  13. //箭的初速度(私用)
  14. [NonSerialized] public float mySpeed = 0;
  15. //箭的初速度(公用)
  16. public static float speed = GameMgr.RealSizeToGameSize(60);
  17. //箭射出时从弓传过来的参数
  18. #region
  19. //手臂弓
  20. //[NonSerialized] public ArmBow armBow;
  21. public float shootOffsetAngleScale;
  22. //射出时的起始坐标
  23. [NonSerialized] public Vector3 shootOutPosition;
  24. //绝对射线
  25. [NonSerialized] public RaycastHit absoluteRay;
  26. //制作误差偏移角(强行制造误差,为了增加游戏难度,该偏移角的大小根据难度而定)
  27. [NonSerialized] public float offsetAngle;
  28. //误差偏移后的欧拉角
  29. [NonSerialized] public Vector3 finalAngleAfterOffset;
  30. public int playerIndex;
  31. #endregion
  32. //射线击中的靶子
  33. TargetBody rayHitTargetBody;
  34. //能够完美击中射线点
  35. bool canPerfectHit;
  36. //能否使用侧面镜头
  37. [NonSerialized] public bool canUseSideCamera;
  38. [NonSerialized] public ArrowCamera arrowCameraComp;
  39. public System.Action onDoNextShoot;
  40. public static HashSet<ArrowNew> arrowSet = new HashSet<ArrowNew>();
  41. void Awake()
  42. {
  43. arrowSet.Add(this);
  44. //GameMgr.ins.gameMode.PauseTimeCounting(this);
  45. //箭模型平时属于ArmBow层,因为ArmBow层在飞行镜头中不被渲染,所以箭出去后要切换layer
  46. //this.transform.Find("Head/_hunse_jian").gameObject.layer = 0;
  47. }
  48. void OnDestroy()
  49. {
  50. arrowSet.Remove(this);
  51. //if (GameMgr.ins) GameMgr.ins.gameMode.ResumeTimeCounting(this);
  52. }
  53. void Start()
  54. {
  55. int _scale = 5;
  56. mySpeed = speed;
  57. mySpeed *= _scale; //默认用5倍
  58. if (playerIndex == 0)
  59. {
  60. Billboard.ins?.SetArrowSpeed(speed);
  61. Billboard.ins?.SetArrowSpeedScale(_scale);
  62. Billboard.ins?.ShowSpeed();
  63. }
  64. else if (playerIndex == 1)
  65. {
  66. Billboard.ins?.Second_SetArrowSpeed(speed);
  67. Billboard.ins?.Second_SetArrowSpeedScale(_scale);
  68. Billboard.ins?.Second_ShowSpeed();
  69. }
  70. //if (GameAssistUI.ins)
  71. //{
  72. // mySpeed *= GameAssistUI.ins.shootScaleValue;
  73. // Billboard.ins?.SetArrowSpeedScale(GameAssistUI.ins.shootScaleValue);
  74. //}
  75. //Billboard.ins?.ShowSpeed();
  76. //if (absoluteRay.transform)
  77. //{
  78. // //记录射线有没有击中靶子
  79. // rayHitTargetBody = absoluteRay.transform.GetComponent<TargetBody>();
  80. // //把瞄准点画成红圈,渲染在靶子上
  81. // if (rayHitTargetBody)
  82. // {
  83. // Transform redCircle = rayHitTargetBody.transform.Find("RedCircle");
  84. // redCircle.gameObject.SetActive(true);
  85. // redCircle.transform.position = -redCircle.transform.forward * 0.001f + absoluteRay.point;
  86. // }
  87. //}
  88. SetUpBeforFly();
  89. //Transform cameraTF = this.transform.Find("Camera");
  90. //cameraTF.gameObject.SetActive(true);
  91. //arrowCameraComp = cameraTF.gameObject.AddComponent<ArrowCamera>();
  92. //arrowCameraComp.arrow = this;
  93. this.activeEffectTrail(true);
  94. }
  95. /**在箭飞行前初始化&如果瞄准射线的落点在靶子上时,才调用该函数 */
  96. void SetUpBeforFly()
  97. {
  98. //基础角
  99. float baseAngleX = FormatAngleX(this.transform.eulerAngles.x);
  100. //最大可调整的角度差
  101. float maxDeltaAngleX = 5;
  102. //最终角
  103. float finalAngleX = baseAngleX;
  104. //额外偏移误差角
  105. float offsetAngleX = FormatAngleX(this.finalAngleAfterOffset.x) - baseAngleX;
  106. float offsetAngleY = FormatAngleY(this.finalAngleAfterOffset.y - this.transform.eulerAngles.y);
  107. if (DebugArrowOffsetAngle.ins)
  108. {
  109. offsetAngleX *= DebugArrowOffsetAngle.ins.GetOffsetScaleValue();
  110. offsetAngleY *= DebugArrowOffsetAngle.ins.GetOffsetScaleValue();
  111. }
  112. else
  113. {
  114. offsetAngleX *= shootOffsetAngleScale;
  115. offsetAngleY *= shootOffsetAngleScale;
  116. }
  117. DebugArrowOffsetAngle2.ins?.SetOffsetAngles(offsetAngleX, offsetAngleY);
  118. if (absoluteRay.transform)
  119. {
  120. bool plusOffsetAngleY = false;
  121. //看能否通过调整发射角让箭落在靶子的瞄准点上
  122. CalculateParabolaAngle(absoluteRay.point);
  123. //瞄准的是不是Target层
  124. bool isTargetLayer = IsTargetLayer(absoluteRay.transform.gameObject);
  125. //Debug.Log("isTargetLayer:" + isTargetLayer);
  126. //绝对发射角无解
  127. if (!hasParabolaAngle)
  128. {
  129. if (isTargetLayer) AimLoadChecker.ins?.ShowOutTip();
  130. }
  131. else
  132. {
  133. //来到这里,证明发射角有解,该解姑且叫它绝对角
  134. float absoluteAngleX = parabolaAngleInRadian / Mathf.PI * 180;
  135. //客户要求绝对角跟原本角度相差不能超过 maxDeltaAngleX
  136. float deltaAngleX = absoluteAngleX - baseAngleX;
  137. //如果绝对角跟原本角度相差不超过maxDeltaAngleX
  138. if (Mathf.Abs(deltaAngleX) < maxDeltaAngleX)
  139. {
  140. finalAngleX = Mathf.Clamp(absoluteAngleX + offsetAngleX, -89, 89);
  141. plusOffsetAngleY = true;
  142. if (Math.Abs(offsetAngle) < 0.001)
  143. {
  144. canPerfectHit = true;
  145. }
  146. if (rayHitTargetBody && Mathf.RoundToInt(rayHitTargetBody.GetDistance()) >= 50 && UnityEngine.Random.value < 0.5)
  147. {
  148. canUseSideCamera = true;
  149. }
  150. }
  151. else
  152. {
  153. finalAngleX = Mathf.Clamp(baseAngleX + maxDeltaAngleX, -89, 89);
  154. if (isTargetLayer) AimLoadChecker.ins?.ShowOutTip();
  155. }
  156. }
  157. finalPoint = absoluteRay.point;
  158. if (plusOffsetAngleY)
  159. {
  160. Vector3 myPos = this.transform.position;
  161. Vector3 pointer = finalPoint - myPos;
  162. pointer = Quaternion.AngleAxis(offsetAngleY, Vector3.up) * pointer;
  163. finalPoint = myPos + pointer;
  164. this.transform.LookAt(finalPoint);
  165. }
  166. }
  167. else
  168. {
  169. finalPoint = this.transform.position + this.transform.forward * 100;
  170. }
  171. parabolaAngleInRadian = finalAngleX / 180 * Mathf.PI;
  172. }
  173. /*
  174. 因为Unity引擎的规则,向上时359~270度,向下是0~90度
  175. 为了方便数学运算,换算成 向上时0~90度,向下是0~-90度
  176. */
  177. float FormatAngleX(float value)
  178. {
  179. if (value > 180) value = 360 - value;
  180. else value = -value;
  181. return value;
  182. }
  183. /**UnityY轴旋转只有0°~360°,两个Y轴旋转相减求得的夹角时会出现>180的情况,该函数就是为了解决这问题 */
  184. float FormatAngleY(float value)
  185. {
  186. if (Mathf.Abs(value) > 180)
  187. {
  188. if (value < 0)
  189. {
  190. return 360f + value;
  191. }
  192. if (value > 0)
  193. {
  194. return value - 360f;
  195. }
  196. }
  197. return value;
  198. }
  199. bool IsTargetLayer(GameObject gameObject)
  200. {
  201. if (playerIndex == 0)
  202. {
  203. return (1 << gameObject.layer) == LayerMask.GetMask("Target1");
  204. }
  205. else if (playerIndex == 1)
  206. {
  207. return (1 << gameObject.layer) == LayerMask.GetMask("Target2");
  208. }
  209. else
  210. {
  211. return (1 << gameObject.layer) == LayerMask.GetMask("Target");
  212. }
  213. }
  214. public Transform Head()
  215. {
  216. return this.transform.Find("Head");
  217. }
  218. /**发射角有无解 */
  219. bool hasParabolaAngle = false;
  220. /**发射角弧度解 */
  221. float parabolaAngleInRadian = 0;
  222. /**
  223. 求弓箭发射到指定坐标所需要的角度。
  224. 已知初速度大小V,重力g,起始坐标(a1,a2),目标坐标(b1,b2)。
  225. 解:
  226. 1、列出关系式
  227. Δx = b1 - a1;
  228. Δy = b2 - a2;
  229. Vx = V * cos(angle)
  230. Vy = V * sin(angle)
  231. Vy * t + 1/2 * g * t^2 = Δy
  232. Vx * t = Δx
  233. t = Δx / Vx
  234. 2、推导过程
  235. (V * sin(angle)) * Δx / (V * cos(angle)) + 1/2 * g * Δx^2 / (V^2*cos(angle)^2) = Δy
  236. tan(angle) * Δx + 1/2 * g * Δx^2 / (V^2*cos(angle)^2) = Δy
  237. tan(angle) * Δx + 1/2 * g * (Δx^2 / V^2) * (1 + tan(angle)^2) = Δy
  238. 3、根据求根公式得出结论
  239. a = 1/2 * g * Δx^2 / V^2
  240. b = Δx
  241. c = a - Δy
  242. d = tan(angle) = (-b ± (b^2 - 4*a*c)^0.5) / (2*a)
  243. angle = atan(d)
  244. 有无根的判别式,有根则b^2-4*a*c>=0
  245. */
  246. void CalculateParabolaAngle(Vector3 destination)
  247. {
  248. float deltaX = Vector2.Distance(
  249. new Vector2(destination.x, destination.z),
  250. new Vector2(this.transform.position.x, this.transform.position.z)
  251. );
  252. float deltaY = destination.y - this.transform.position.y;
  253. float a = 0.5f * Physics.gravity.y * Mathf.Pow(deltaX, 2) / Mathf.Pow(this.mySpeed, 2);
  254. float b = deltaX;
  255. float c = a - deltaY;
  256. hasParabolaAngle = Mathf.Pow(b, 2) - 4 * a * c >= 0;
  257. if (hasParabolaAngle)
  258. {
  259. float res1 = (-b + Mathf.Pow(Mathf.Pow(b, 2) - 4 * a * c, 0.5f)) / (2 * a);
  260. float res2 = (-b - Mathf.Pow(Mathf.Pow(b, 2) - 4 * a * c, 0.5f)) / (2 * a);
  261. parabolaAngleInRadian = Mathf.Min(Mathf.Atan(res1), Mathf.Atan(res2));
  262. }
  263. }
  264. void FixedUpdate()
  265. {
  266. if (!isHit && flyTime >= 0)
  267. {
  268. flyTime += Time.deltaTime;
  269. if (flyTime > 14)
  270. {
  271. FlyTimeOut();
  272. }
  273. }
  274. }
  275. void Update()
  276. {
  277. UpdateFlyLogic();
  278. }
  279. void FlyTimeOut()
  280. {
  281. Destroy(gameObject);
  282. GameController.ins.HitTarget(0);
  283. nextShoot();
  284. }
  285. float logicFlyTime = 0;
  286. Vector3 finalPoint;
  287. /**飞行帧逻辑(运动学) */
  288. void UpdateFlyLogic()
  289. {
  290. if (isHit) return;
  291. logicFlyTime += Time.deltaTime;
  292. Vector3 destination = finalPoint;
  293. float vx = Mathf.Cos(parabolaAngleInRadian) * mySpeed;
  294. float t = logicFlyTime;
  295. float vy = Mathf.Sin(parabolaAngleInRadian) * mySpeed + Physics.gravity.y * t;
  296. float dy = Mathf.Sin(parabolaAngleInRadian) * mySpeed * t + 0.5f * Physics.gravity.y * Mathf.Pow(t, 2);
  297. float dx = vx * t;
  298. Vector3 nextPosition = new Vector3(destination.x - shootOutPosition.x, 0, destination.z - shootOutPosition.z);
  299. nextPosition = nextPosition.normalized * dx;
  300. nextPosition.y = dy;
  301. nextPosition = shootOutPosition + nextPosition;
  302. Vector3 oldPosition = this.transform.position;
  303. this.transform.position = nextPosition;
  304. Vector3 eulerAngles = this.transform.eulerAngles;
  305. float angleX = Mathf.Atan(vy / vx) / Mathf.PI * 180;
  306. eulerAngles.x = -angleX;
  307. this.transform.eulerAngles = eulerAngles;
  308. float deltaDistance = Vector3.Distance(oldPosition, nextPosition);
  309. Ray ray = new Ray(oldPosition, nextPosition - oldPosition);
  310. RaycastHit raycastHit;
  311. //忽略和区分对应的target1和target2层级
  312. int _ignoreMask = playerIndex == 0 ? (1 << 6):(1<<7);
  313. bool raycastResult = Physics.Raycast(ray, out raycastHit, deltaDistance, _ignoreMask);
  314. if (raycastResult)
  315. {
  316. this.transform.position = raycastHit.point;
  317. OnHitAnyInFlyLogic(raycastHit);
  318. }
  319. }
  320. public class HitType
  321. {
  322. public static int None = 0;
  323. public static int TargetInRing = 1; //击中了靶子,且在得分环内
  324. public static int TargetOutRing = 2; //击中了靶子,但不在得分环内
  325. public static int NotTarget = 4; //击中非目标对象
  326. public static int Animal = 5; //击中动物
  327. }
  328. [NonSerialized] public int hitType = HitType.None;
  329. [NonSerialized] public Transform raycastHitTransform; //射线击中的目标变换
  330. [NonSerialized] public string[] hitTargetAnimalInfo;
  331. //飞行逻辑中检测到碰撞
  332. void OnHitAnyInFlyLogic(RaycastHit raycastHit)
  333. {
  334. Head().position = raycastHit.point;
  335. transform.SetParent(raycastHit.transform.parent);
  336. string targetName = raycastHit.transform.gameObject.name;
  337. raycastHitTransform = raycastHit.transform;
  338. if (targetName == "TargetBody")
  339. {
  340. Vector3 hitPoint = raycastHit.point;
  341. if (rayHitTargetBody && canPerfectHit)
  342. {
  343. hitPoint = absoluteRay.point;
  344. Head().position = hitPoint;
  345. }
  346. raycastHit.transform.GetComponent<TargetBodyNew>().Hit(this, hitPoint, playerIndex);
  347. }
  348. else if (raycastHit.transform.GetComponent<TargetOutBound>())
  349. { //撞到空气墙当作超时处理
  350. FlyTimeOut();
  351. }
  352. else
  353. {
  354. Hit();
  355. GameController.ins.HitTarget(0);
  356. }
  357. }
  358. public void Hit()
  359. {
  360. isHit = true;
  361. if (GameDebug.ins) GameDebug.ins.ShowRes(absoluteRay.point, this.Head().position);
  362. //控制箭的特效显示
  363. this.activeEffectCyclone(false);
  364. this.activeEffectBomb(true);
  365. this.activeEffectTrail(false);
  366. //最新一箭击中后会发光标记
  367. //ArrowLightSick.RecoveryAll();
  368. //this.GetComponentInChildren<ArrowLightSick>().Hit();
  369. }
  370. //进入下一轮射击
  371. [NonSerialized] public bool hasDoneNextShoot = false;
  372. public void nextShoot()
  373. {
  374. if (hasDoneNextShoot) return;
  375. hasDoneNextShoot = true;
  376. //try
  377. //{
  378. // //if (AimHandler.ins) AimHandler.ins.Ban9AxisCalculate(false);
  379. // //把瞄准点画成红圈,渲染在靶子上(取消)
  380. // if (rayHitTargetBody)
  381. // {
  382. // Transform redCircle = rayHitTargetBody.transform.Find("RedCircle");
  383. // redCircle.gameObject.SetActive(false);
  384. // }
  385. // //最新一箭击中后会发光标记(取消)
  386. // ArrowLightSick.RecoveryAll();
  387. //}
  388. //catch (System.Exception e) { Debug.LogError(e.Message + "\n" + e.StackTrace); }
  389. //if (!GameMgr.ins.gameMode.DoNextShoot()) return;
  390. //this.armBow.readyShoot();
  391. }
  392. #region 箭的特效
  393. void activeEffectCyclone(bool value)
  394. {
  395. transform.Find("Head/EF_kuosanquan").gameObject.SetActive(value);
  396. if (!value) return;
  397. ParticleSystemRenderer ps = transform.Find("Head/EF_kuosanquan/kuosan").GetComponent<ParticleSystemRenderer>();
  398. ParticleSystemRenderer ps1 = transform.Find("Head/EF_kuosanquan/kuosan (1)").GetComponent<ParticleSystemRenderer>();
  399. DOTween.To(() => ps.minParticleSize, value =>
  400. {
  401. ps.minParticleSize = value;
  402. ps.maxParticleSize = value;
  403. }, 0.4f, 0.6f);
  404. DOTween.To(() => ps1.minParticleSize, value =>
  405. {
  406. ps1.minParticleSize = value;
  407. ps1.maxParticleSize = value;
  408. }, 0.8f, 0.6f);
  409. }
  410. void activeEffectBomb(bool value)
  411. {
  412. transform.Find("Head/EF_baodian").gameObject.SetActive(value);
  413. }
  414. void activeEffectTrail(bool value)
  415. {
  416. transform.Find("EF_tuowei").gameObject.SetActive(value);
  417. transform.Find("EF_tuowei/Trail").GetComponent<TrailRenderer>().time = 1.6f / mySpeed;
  418. }
  419. #endregion
  420. }