Arrow.cs 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using DG.Tweening;
  6. using UnityEngine.SceneManagement;
  7. /* 箭对象 */
  8. public class Arrow : MonoBehaviour
  9. {
  10. //飞行时间统计
  11. [NonSerialized] public float flyTime = 0;
  12. //标识—是否击中了什么
  13. [NonSerialized] public bool isHit = false;
  14. //箭的初速度(私用)
  15. [NonSerialized] public float mySpeed = 0;
  16. //箭的初速度(公用)
  17. public static float speed = GameMgr.RealSizeToGameSize(60);
  18. //箭射出时从弓传过来的参数
  19. #region
  20. //手臂弓
  21. [NonSerialized] public ArmBow armBow;
  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. #endregion
  31. //射线击中的靶子
  32. TargetBody rayHitTargetBody;
  33. //能够完美击中射线点
  34. bool canPerfectHit;
  35. //能否使用侧面镜头
  36. [NonSerialized] public bool canUseSideCamera;
  37. [NonSerialized] public bool canUserLockCamera;
  38. [NonSerialized] public ArrowCamera arrowCameraComp;
  39. public System.Action onDoNextShoot;
  40. public static HashSet<Arrow> arrowSet = new HashSet<Arrow>();
  41. private bool isInGameScene = false;
  42. private float currentSlowFactor = 1;
  43. void Awake()
  44. {
  45. arrowSet.Add(this);
  46. GameMgr.ins.gameMode.PauseTimeCounting(this);
  47. //箭模型平时属于ArmBow层,因为ArmBow层在飞行镜头中不被渲染,所以箭出去后要切换layer
  48. this.transform.Find("Head/_hunse_jian").gameObject.layer = 0;
  49. if (SceneManager.GetActiveScene().name == "Game" && GlobalData.MyDeviceMode == DeviceMode.Archery)
  50. {
  51. //经典射箭
  52. isInGameScene = true;
  53. currentSlowFactor = ArmBow.ins.slowFactor;
  54. }
  55. else {
  56. isInGameScene = false;
  57. }
  58. }
  59. void OnDestroy()
  60. {
  61. arrowSet.Remove(this);
  62. if (GameMgr.ins) GameMgr.ins.gameMode.ResumeTimeCounting(this);
  63. }
  64. void Start()
  65. {
  66. mySpeed = speed;
  67. Billboard.ins?.SetArrowSpeed(speed);
  68. if (GameAssistUI.ins && GlobalData.MyDeviceMode == DeviceMode.Archery)
  69. {
  70. mySpeed *= GameAssistUI.ins.shootScaleValue;
  71. Billboard.ins?.SetArrowSpeedScale(GameAssistUI.ins.shootScaleValue);
  72. }
  73. Debug.Log($"Speed:{speed},mySpeed:{mySpeed},shootScaleValue :{GameAssistUI.ins.shootScaleValue}");
  74. Billboard.ins?.ShowSpeed();
  75. if (GlobalData.pkMatchType == PKMatchType.OnlinePK)
  76. {
  77. if (GameMgr.gameType == 9)
  78. {
  79. ((PKGameMode_OnlinePK)GameMgr.ins.gameMode).shootSpeedWillSend = Billboard.ins.GetShootSpeedText();
  80. }
  81. }
  82. if (absoluteRay.transform)
  83. {
  84. //记录射线有没有击中靶子
  85. rayHitTargetBody = absoluteRay.transform.GetComponent<TargetBody>();
  86. //把瞄准点画成红圈,渲染在靶子上
  87. if (rayHitTargetBody)
  88. {
  89. //如果是目标是靶子,就是启动靶子摄像头
  90. canUserLockCamera = true;
  91. //string typeStr = GlobalData.MyDeviceMode == DeviceMode.Archery? "RedCircle":"BulletCircle";
  92. Transform redCircle = rayHitTargetBody.transform.Find("RedCircle");
  93. redCircle.gameObject.SetActive(true);
  94. redCircle.transform.position = -redCircle.transform.forward * 0.001f + absoluteRay.point;
  95. //渲染弹坑
  96. if (GlobalData.MyDeviceMode == DeviceMode.Gun)
  97. {
  98. Transform crater = transform.Find("Crater");
  99. crater.rotation = redCircle.rotation;
  100. }
  101. }
  102. }
  103. SetUpBeforFly();
  104. if (GlobalData.MyDeviceMode == DeviceMode.Archery)
  105. {
  106. Transform cameraTF = this.transform.Find("Camera");
  107. cameraTF.gameObject.SetActive(true);
  108. arrowCameraComp = cameraTF.gameObject.AddComponent<ArrowCamera>();
  109. arrowCameraComp.arrow = this;
  110. }
  111. this.activeEffectTrail(true);
  112. }
  113. /**在箭飞行前初始化&如果瞄准射线的落点在靶子上时,才调用该函数 */
  114. void SetUpBeforFly()
  115. {
  116. //基础角
  117. float baseAngleX = FormatAngleX(this.transform.eulerAngles.x);
  118. //最大可调整的角度差
  119. float maxDeltaAngleX = 5;
  120. //最终角
  121. float finalAngleX = baseAngleX;
  122. //额外偏移误差角
  123. float offsetAngleX = FormatAngleX(this.finalAngleAfterOffset.x) - baseAngleX;
  124. float offsetAngleY = FormatAngleY(this.finalAngleAfterOffset.y - this.transform.eulerAngles.y);
  125. if (DebugArrowOffsetAngle.ins)
  126. {
  127. offsetAngleX *= DebugArrowOffsetAngle.ins.GetOffsetScaleValue();
  128. offsetAngleY *= DebugArrowOffsetAngle.ins.GetOffsetScaleValue();
  129. }
  130. else
  131. {
  132. offsetAngleX *= this.armBow.shootOffsetAngleScale;
  133. offsetAngleY *= this.armBow.shootOffsetAngleScale;
  134. }
  135. DebugArrowOffsetAngle2.ins?.SetOffsetAngles(offsetAngleX, offsetAngleY);
  136. if (absoluteRay.transform)
  137. {
  138. bool plusOffsetAngleY = false;
  139. Debug.Log($"射出位置:{shootOutPosition} 目标位置:{absoluteRay.point} 名字: {absoluteRay.collider.name}");
  140. //看能否通过调整发射角让箭落在靶子的瞄准点上
  141. CalculateParabolaAngle(absoluteRay.point);
  142. //瞄准的是不是Target层
  143. bool isTargetLayer = IsTargetLayer(absoluteRay.transform.gameObject);
  144. //绝对发射角无解
  145. if (!hasParabolaAngle)
  146. {
  147. if (isTargetLayer) AimLoadChecker.ins?.ShowOutTip();
  148. }
  149. else
  150. {
  151. //来到这里,证明发射角有解,该解姑且叫它绝对角
  152. float absoluteAngleX = parabolaAngleInRadian / Mathf.PI * 180;
  153. //客户要求绝对角跟原本角度相差不能超过 maxDeltaAngleX
  154. float deltaAngleX = absoluteAngleX - baseAngleX;
  155. //如果绝对角跟原本角度相差不超过maxDeltaAngleX
  156. if (Mathf.Abs(deltaAngleX) < maxDeltaAngleX)
  157. {
  158. finalAngleX = Mathf.Clamp(absoluteAngleX + offsetAngleX, -89, 89);
  159. plusOffsetAngleY = true;
  160. if (Math.Abs(offsetAngle) < 0.001)
  161. {
  162. canPerfectHit = true;
  163. }
  164. if (rayHitTargetBody && Mathf.RoundToInt(rayHitTargetBody.GetDistance()) >= 50 && UnityEngine.Random.value < 0.5)
  165. {
  166. canUseSideCamera = true;
  167. }
  168. }
  169. else
  170. {
  171. finalAngleX = Mathf.Clamp(baseAngleX + maxDeltaAngleX, -89, 89);
  172. if (isTargetLayer) AimLoadChecker.ins?.ShowOutTip();
  173. }
  174. }
  175. finalPoint = absoluteRay.point;
  176. if (plusOffsetAngleY)
  177. {
  178. Vector3 myPos = this.transform.position;
  179. Vector3 pointer = finalPoint - myPos;
  180. pointer = Quaternion.AngleAxis(offsetAngleY, Vector3.up) * pointer;
  181. finalPoint = myPos + pointer;
  182. this.transform.LookAt(finalPoint);
  183. }
  184. }
  185. else
  186. {
  187. finalPoint = this.transform.position + this.transform.forward * 100;
  188. }
  189. parabolaAngleInRadian = finalAngleX / 180 * Mathf.PI;
  190. }
  191. /*
  192. 因为Unity引擎的规则,向上时359~270度,向下是0~90度
  193. 为了方便数学运算,换算成 向上时0~90度,向下是0~-90度
  194. */
  195. float FormatAngleX(float value)
  196. {
  197. if (value > 180) value = 360 - value;
  198. else value = -value;
  199. return value;
  200. }
  201. /**UnityY轴旋转只有0°~360°,两个Y轴旋转相减求得的夹角时会出现>180的情况,该函数就是为了解决这问题 */
  202. float FormatAngleY(float value)
  203. {
  204. if (Mathf.Abs(value) > 180)
  205. {
  206. if (value < 0)
  207. {
  208. return 360f + value;
  209. }
  210. if (value > 0)
  211. {
  212. return value - 360f;
  213. }
  214. }
  215. return value;
  216. }
  217. bool IsTargetLayer(GameObject gameObject)
  218. {
  219. return (1 << gameObject.layer) == LayerMask.GetMask("Target");
  220. }
  221. public Transform Head()
  222. {
  223. return this.transform.Find("Head");
  224. }
  225. /**发射角有无解 */
  226. bool hasParabolaAngle = false;
  227. /**发射角弧度解 */
  228. float parabolaAngleInRadian = 0;
  229. /**
  230. 求弓箭发射到指定坐标所需要的角度。
  231. 已知初速度大小V,重力g,起始坐标(a1,a2),目标坐标(b1,b2)。
  232. 解:
  233. 1、列出关系式
  234. Δx = b1 - a1;
  235. Δy = b2 - a2;
  236. Vx = V * cos(angle)
  237. Vy = V * sin(angle)
  238. Vy * t + 1/2 * g * t^2 = Δy
  239. Vx * t = Δx
  240. t = Δx / Vx
  241. 2、推导过程
  242. (V * sin(angle)) * Δx / (V * cos(angle)) + 1/2 * g * Δx^2 / (V^2*cos(angle)^2) = Δy
  243. tan(angle) * Δx + 1/2 * g * Δx^2 / (V^2*cos(angle)^2) = Δy
  244. tan(angle) * Δx + 1/2 * g * (Δx^2 / V^2) * (1 + tan(angle)^2) = Δy
  245. 3、根据求根公式得出结论
  246. a = 1/2 * g * Δx^2 / V^2
  247. b = Δx
  248. c = a - Δy
  249. d = tan(angle) = (-b ± (b^2 - 4*a*c)^0.5) / (2*a)
  250. angle = atan(d)
  251. 有无根的判别式,有根则b^2-4*a*c>=0
  252. */
  253. void CalculateParabolaAngle(Vector3 destination)
  254. {
  255. float deltaX = Vector2.Distance(
  256. new Vector2(destination.x, destination.z),
  257. new Vector2(this.transform.position.x, this.transform.position.z)
  258. );
  259. float deltaY = destination.y - this.transform.position.y;
  260. float a = 0.5f * Physics.gravity.y * Mathf.Pow(deltaX, 2) / Mathf.Pow(this.mySpeed, 2);
  261. float b = deltaX;
  262. float c = a - deltaY;
  263. hasParabolaAngle = Mathf.Pow(b, 2) - 4 * a * c >= 0;
  264. Debug.Log("是否有弧度解:" + hasParabolaAngle);
  265. if (hasParabolaAngle)
  266. {
  267. float res1 = (-b + Mathf.Pow(Mathf.Pow(b, 2) - 4 * a * c, 0.5f)) / (2 * a);
  268. float res2 = (-b - Mathf.Pow(Mathf.Pow(b, 2) - 4 * a * c, 0.5f)) / (2 * a);
  269. //parabolaAngleInRadian = Mathf.Min(Mathf.Atan(res1), Mathf.Atan(res2));
  270. // 两个解分别代表低抛角和高抛角
  271. float angle1 = Mathf.Atan(res1); // 弧度
  272. float angle2 = Mathf.Atan(res2);
  273. // ✅ 这里决定是取低角度还是高角度
  274. parabolaAngleInRadian = Mathf.Min(angle1, angle2);
  275. //parabolaAngleInRadian = Mathf.Max(angle1, angle2); // 高抛解
  276. }
  277. }
  278. void FixedUpdate()
  279. {
  280. if (!isHit && flyTime >= 0)
  281. {
  282. flyTime += Time.deltaTime * currentSlowFactor;
  283. if (flyTime > 14)
  284. {
  285. Debug.Log("flyTime:"+ flyTime);
  286. FlyTimeOut();
  287. }
  288. // this.UpdateRotate();
  289. }
  290. }
  291. public ArrowSync.SyncData outputSyncData;
  292. void Update()
  293. {
  294. if (isInGameScene)
  295. UpdateFlyLogic_New();
  296. else
  297. UpdateFlyLogic();
  298. if (GlobalData.pkMatchType == PKMatchType.OnlinePK)
  299. {
  300. if (outputSyncData == null) outputSyncData = new ArrowSync.SyncData();
  301. outputSyncData.SetData(this);
  302. }
  303. }
  304. IEnumerator delayFlyTimeOut() {
  305. yield return new WaitForSeconds(0.8f);//gun_shoot 音频长度
  306. FlyTimeOut();
  307. }
  308. void FlyTimeOut()
  309. {
  310. if (arrowCameraComp != null)
  311. {
  312. var targetLock = arrowCameraComp.arrowCameraTemplate as ArrowCameraTemplate_targetLock;
  313. if (targetLock != null && !targetLock.IsDeleting)
  314. {
  315. // 删除弓箭相机,因为Lock目标靶的情况下 会脱离弓箭
  316. Destroy(arrowCameraComp.gameObject);
  317. }
  318. }
  319. Destroy(gameObject);
  320. GameMgr.ins.gameMode.HitTarget(0);
  321. AudioMgr.ins.PlayCheer(false);
  322. nextShoot();
  323. }
  324. float logicFlyTime = 0;
  325. Vector3 finalPoint;
  326. //飞行速度可在过程中变化,比如快到目标时突然加速
  327. public AnimationCurve speedCurve = AnimationCurve.Linear(0, 1, 1, 1.5f);
  328. /**飞行帧逻辑(运动学) */
  329. void UpdateFlyLogic()
  330. {
  331. if (isHit) return;
  332. logicFlyTime += Time.deltaTime;
  333. Vector3 destination = finalPoint;
  334. float vx = Mathf.Cos(parabolaAngleInRadian) * mySpeed;
  335. float t = logicFlyTime;
  336. float vy = Mathf.Sin(parabolaAngleInRadian) * mySpeed + Physics.gravity.y * t;
  337. float dy = Mathf.Sin(parabolaAngleInRadian) * mySpeed * t + 0.5f * Physics.gravity.y * Mathf.Pow(t, 2);
  338. float dx = vx * t;
  339. Vector3 nextPosition = new Vector3(destination.x - shootOutPosition.x, 0, destination.z - shootOutPosition.z);
  340. nextPosition = nextPosition.normalized * dx;
  341. nextPosition.y = dy;
  342. nextPosition = shootOutPosition + nextPosition;
  343. Vector3 oldPosition = this.transform.position;
  344. this.transform.position = nextPosition;
  345. Vector3 eulerAngles = this.transform.eulerAngles;
  346. float angleX = Mathf.Atan(vy / vx) / Mathf.PI * 180;
  347. eulerAngles.x = -angleX;
  348. this.transform.eulerAngles = eulerAngles;
  349. float deltaDistance = Vector3.Distance(oldPosition, nextPosition);
  350. Ray ray = new Ray(oldPosition, nextPosition - oldPosition);
  351. RaycastHit raycastHit;
  352. bool raycastResult = Physics.Raycast(ray, out raycastHit, deltaDistance);
  353. if (raycastResult)
  354. {
  355. this.transform.position = raycastHit.point;
  356. if (ArrowTraceDebug.ins) ArrowTraceDebug.ins.OnArrowUpdate(this);
  357. OnHitAnyInFlyLogic(raycastHit);
  358. }
  359. else
  360. {
  361. if (ArrowTraceDebug.ins) ArrowTraceDebug.ins.OnArrowUpdate(this);
  362. }
  363. }
  364. /** 经典射击里面的 新飞行帧逻辑(运动学) */
  365. void UpdateFlyLogic_New()
  366. {
  367. if (isHit) return;
  368. // 飞行进度 (0 = 刚射出, 1 = 到达目标)
  369. float totalDistance = Vector3.Distance(shootOutPosition, finalPoint);
  370. float currentDistance = Vector3.Distance(transform.position, shootOutPosition);
  371. float progress = Mathf.Clamp01(currentDistance / totalDistance);
  372. // 从曲线获取速度倍率
  373. float speedMultiplier = ArmBow.ins.customCurveArrow.Evaluate(progress);
  374. float detalSlowFactor = currentSlowFactor * 0.5f;
  375. float _tempSlowFactor = currentSlowFactor + ( detalSlowFactor * speedMultiplier - detalSlowFactor);
  376. //Debug.Log("TempSlowFactor:"+ _tempSlowFactor);
  377. logicFlyTime += Time.deltaTime * _tempSlowFactor;
  378. float currentSpeed = mySpeed;
  379. float t = logicFlyTime;
  380. // --- 初速度分解 ---
  381. float vx = Mathf.Cos(parabolaAngleInRadian) * currentSpeed; // 水平速度大小
  382. float vy0 = Mathf.Sin(parabolaAngleInRadian) * currentSpeed; // 初始竖直速度
  383. // XZ方向向量(水平朝向目标)
  384. Vector3 dirXZ = new Vector3(finalPoint.x - shootOutPosition.x, 0, finalPoint.z - shootOutPosition.z).normalized;
  385. // --- 位移计算 ---
  386. float dx = vx * t; // 水平位移
  387. float dy = vy0 * t + 0.5f * Physics.gravity.y * t * t; // 竖直位移
  388. Vector3 nextPosition = shootOutPosition + dirXZ * dx + Vector3.up * dy;
  389. // --- 更新位置 ---
  390. Vector3 oldPosition = this.transform.position;
  391. this.transform.position = nextPosition;
  392. // --- 箭头朝向 ---
  393. float vy = vy0 + Physics.gravity.y * t; // 当前瞬时竖直速度
  394. float angleX = Mathf.Atan2(vy, vx) * Mathf.Rad2Deg;
  395. Vector3 eulerAngles = this.transform.eulerAngles;
  396. eulerAngles.x = -angleX;
  397. this.transform.eulerAngles = eulerAngles;
  398. // --- 碰撞检测 ---
  399. float deltaDistance = Vector3.Distance(oldPosition, nextPosition);
  400. Ray ray = new Ray(oldPosition, nextPosition - oldPosition);
  401. if (Physics.Raycast(ray, out RaycastHit raycastHit, deltaDistance))
  402. {
  403. this.transform.position = raycastHit.point;
  404. if (ArrowTraceDebug.ins) ArrowTraceDebug.ins.OnArrowUpdate(this);
  405. OnHitAnyInFlyLogic(raycastHit);
  406. Debug.Log(raycastHit.collider.gameObject.name);
  407. }
  408. else
  409. {
  410. if (ArrowTraceDebug.ins) ArrowTraceDebug.ins.OnArrowUpdate(this);
  411. }
  412. }
  413. public class HitType
  414. {
  415. public static int None = 0;
  416. public static int TargetInRing = 1; //击中了靶子,且在得分环内
  417. public static int TargetOutRing = 2; //击中了靶子,但不在得分环内
  418. public static int NotTarget = 4; //击中非目标对象
  419. public static int Animal = 5; //击中动物
  420. }
  421. [NonSerialized] public int hitType = HitType.None;
  422. [NonSerialized] public Transform raycastHitTransform; //射线击中的目标变换
  423. [NonSerialized] public string[] hitTargetAnimalInfo;
  424. //飞行逻辑中检测到碰撞
  425. void OnHitAnyInFlyLogic(RaycastHit raycastHit)
  426. {
  427. this.Head().position = raycastHit.point;
  428. this.transform.SetParent(raycastHit.transform.parent);
  429. string targetName = raycastHit.transform.gameObject.name;
  430. this.raycastHitTransform = raycastHit.transform;
  431. if (targetName == "TargetBody")
  432. {
  433. Vector3 hitPoint = raycastHit.point;
  434. if (rayHitTargetBody && canPerfectHit)
  435. {
  436. hitPoint = absoluteRay.point;
  437. this.Head().position = hitPoint;
  438. }
  439. if (GlobalData.MyDeviceMode == DeviceMode.Archery)
  440. this.arrowCameraComp.arrowCameraTemplate?.beforeHit();
  441. else
  442. nextShootFromType();//子弹直接下一轮
  443. raycastHit.transform.GetComponent<TargetBody>().Hit(this, hitPoint);
  444. }
  445. else if (targetName.StartsWith("TargetAnimalPart"))
  446. {
  447. hitType = HitType.Animal;
  448. Vector3 hitPoint = raycastHit.point;
  449. string partName = targetName.Split(new char[] { '_' })[1];
  450. if (GlobalData.MyDeviceMode == DeviceMode.Archery)
  451. this.arrowCameraComp.arrowCameraTemplate?.beforeHit();
  452. else
  453. nextShootFromType();//子弹直接下一轮
  454. TargetAnimal targetAnimal = raycastHit.transform.GetComponentInParent<TargetAnimal>();
  455. targetAnimal.OnHit(this, hitPoint, partName);
  456. //箭击中的音效
  457. AudioMgr.ins.PlayArrowEnter();
  458. //记录击中的部位和动物ID
  459. hitTargetAnimalInfo = new string[] {
  460. targetAnimal.GetOnlineID().ToString(),
  461. targetAnimal.targetAnimalParts.IndexOf(raycastHitTransform).ToString(),
  462. SyncDataUtil.Vec3ToStr(transform.localPosition),
  463. SyncDataUtil.QuatToStr(transform.localRotation),
  464. SyncDataUtil.Vec3ToStr(transform.position),
  465. SyncDataUtil.QuatToStr(transform.rotation)
  466. };
  467. }
  468. else if (raycastHit.transform.GetComponent<TargetOutBound>())
  469. { //撞到空气墙当作超时处理
  470. if (GlobalData.MyDeviceMode == DeviceMode.Archery)
  471. FlyTimeOut();
  472. else
  473. StartCoroutine(delayFlyTimeOut());
  474. }
  475. else
  476. {
  477. if (GlobalData.MyDeviceMode == DeviceMode.Archery)
  478. this.arrowCameraComp.arrowCameraTemplate?.beforeHit();
  479. else
  480. nextShootFromType();//子弹直接下一轮
  481. Hit();
  482. GameMgr.ins.gameMode.HitTarget(0);
  483. //击中其它东西时的音效
  484. hitType = HitType.NotTarget;
  485. if (UnityEngine.SceneManagement.SceneManager.GetActiveScene().name == "Game")
  486. {
  487. AudioMgr.ins.PlayCheer(false);
  488. }
  489. else
  490. {
  491. if (CommonConfig.StandaloneModeOrPlatformB) {
  492. AudioMgr.ins.PlayCheer(false);
  493. } else {
  494. AudioMgr.ins.PlayArrowEnter();
  495. }
  496. }
  497. }
  498. }
  499. public void Hit()
  500. {
  501. isHit = true;
  502. if (GameDebug.ins) GameDebug.ins.ShowRes(absoluteRay.point, this.Head().position);
  503. //控制箭的特效显示
  504. this.activeEffectCyclone(false);
  505. this.activeEffectBomb(true);
  506. this.activeEffectTrail(false);
  507. //最新一箭击中后会发光标记
  508. ArrowLightSick.RecoveryAll();
  509. this.GetComponentInChildren<ArrowLightSick>().Hit();
  510. }
  511. //进入下一轮射击
  512. [NonSerialized] public bool hasDoneNextShoot = false;
  513. public void nextShootFromType() {
  514. // Debug.Log("nextShootFromType:" + GlobalDataTemp.pkMatchType);
  515. if (GlobalDataTemp.pkMatchType == PKMatchType.LocalPK || GlobalDataTemp.pkMatchType == PKMatchType.OnlinePK)
  516. {
  517. StartCoroutine(delayNectShoot());
  518. }
  519. else {
  520. nextShoot();
  521. }
  522. }
  523. IEnumerator delayNectShoot() {
  524. yield return new WaitForSeconds(1.0f);
  525. nextShoot();
  526. }
  527. public void nextShoot()
  528. {
  529. if (hasDoneNextShoot) return;
  530. hasDoneNextShoot = true;
  531. GameMgr.ins.gameMode.ResumeTimeCounting(this);
  532. onDoNextShoot?.Invoke();
  533. try
  534. {
  535. //把瞄准点画成红圈,渲染在靶子上(取消)
  536. if (rayHitTargetBody)
  537. {
  538. //string typeStr = GlobalData.MyDeviceMode == DeviceMode.Archery ? "RedCircle" : "BulletCircle";
  539. Transform redCircle = rayHitTargetBody.transform.Find("RedCircle");
  540. redCircle.gameObject.SetActive(false);
  541. }
  542. //最新一箭击中后会发光标记(取消)
  543. ArrowLightSick.RecoveryAll();
  544. }
  545. catch (System.Exception e) { Debug.LogError(e.Message + "\n" + e.StackTrace); }
  546. if (!GameMgr.ins.gameMode.DoNextShoot()) return;
  547. this.armBow.readyShoot();
  548. }
  549. //---------箭矢旋转--------
  550. Vector3 rotateV3 = new Vector3(0, 0, 1400);
  551. void UpdateRotate()
  552. {
  553. this.Head().Rotate(rotateV3 * Time.deltaTime, Space.Self);
  554. }
  555. //---------箭矢特效---------
  556. public void activeEffectCyclone(bool value)
  557. {
  558. this.transform.Find("Head/EF_kuosanquan").gameObject.SetActive(value);
  559. if (!value) return;
  560. ParticleSystemRenderer ps = this.transform.Find("Head/EF_kuosanquan/kuosan").GetComponent<ParticleSystemRenderer>();
  561. ParticleSystemRenderer ps1 = this.transform.Find("Head/EF_kuosanquan/kuosan (1)").GetComponent<ParticleSystemRenderer>();
  562. DOTween.To(() => ps.minParticleSize, value =>
  563. {
  564. ps.minParticleSize = value;
  565. ps.maxParticleSize = value;
  566. }, 0.4f, 0.6f);
  567. DOTween.To(() => ps1.minParticleSize, value =>
  568. {
  569. ps1.minParticleSize = value;
  570. ps1.maxParticleSize = value;
  571. }, 0.8f, 0.6f);
  572. }
  573. void activeEffectBomb(bool value)
  574. {
  575. this.transform.Find("Head/EF_baodian").gameObject.SetActive(value);
  576. }
  577. void activeEffectTrail(bool value)
  578. {
  579. if (GlobalData.MyDeviceMode == DeviceMode.Archery)
  580. {
  581. Transform guadiantuowei = this.transform.Find("guadiantuowei");
  582. if (guadiantuowei == null)
  583. {
  584. this.transform.Find("EF_tuowei").gameObject.SetActive(value);
  585. this.transform.Find("EF_tuowei/Trail").GetComponent<TrailRenderer>().time = 1.6f / mySpeed;
  586. }
  587. else {
  588. guadiantuowei.gameObject.SetActive(value);
  589. }
  590. }
  591. else {
  592. StartCoroutine(showTrail(value));
  593. }
  594. }
  595. IEnumerator showTrail(bool value) {
  596. this.transform.Find("EF_tuowei_bullet").gameObject.SetActive(value);
  597. //this.transform.Find("EF_tuowei_bullet/Trail").GetComponent<TrailRenderer>().time = 0;
  598. yield return new WaitForEndOfFrame();
  599. this.transform.Find("EF_tuowei_bullet/GB_flame_FX_1").gameObject.SetActive(true);
  600. //this.transform.Find("EF_tuowei_bullet/Trail").GetComponent<TrailRenderer>().time = 0.02f;
  601. //this.transform.Find("EF_tuowei_bullet/Trail").GetComponent<TrailRenderer>().time = 1.6f / mySpeed;
  602. }
  603. }