ArrowNew2.cs 16 KB

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