Arrow.cs 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665
  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. Destroy(gameObject);
  311. if (arrowCameraComp != null) {
  312. var targetLock = arrowCameraComp.arrowCameraTemplate as ArrowCameraTemplate_targetLock;
  313. if (targetLock != null && !targetLock.IsDeleting)
  314. {
  315. // 删除弓箭相机,因为Lock目标靶的情况下 会脱离弓箭
  316. Destroy(arrowCameraComp);
  317. }
  318. }
  319. GameMgr.ins.gameMode.HitTarget(0);
  320. AudioMgr.ins.PlayCheer(false);
  321. nextShoot();
  322. }
  323. float logicFlyTime = 0;
  324. Vector3 finalPoint;
  325. //飞行速度可在过程中变化,比如快到目标时突然加速
  326. public AnimationCurve speedCurve = AnimationCurve.Linear(0, 1, 1, 1.5f);
  327. /**飞行帧逻辑(运动学) */
  328. void UpdateFlyLogic()
  329. {
  330. if (isHit) return;
  331. logicFlyTime += Time.deltaTime;
  332. Vector3 destination = finalPoint;
  333. float vx = Mathf.Cos(parabolaAngleInRadian) * mySpeed;
  334. float t = logicFlyTime;
  335. float vy = Mathf.Sin(parabolaAngleInRadian) * mySpeed + Physics.gravity.y * t;
  336. float dy = Mathf.Sin(parabolaAngleInRadian) * mySpeed * t + 0.5f * Physics.gravity.y * Mathf.Pow(t, 2);
  337. float dx = vx * t;
  338. Vector3 nextPosition = new Vector3(destination.x - shootOutPosition.x, 0, destination.z - shootOutPosition.z);
  339. nextPosition = nextPosition.normalized * dx;
  340. nextPosition.y = dy;
  341. nextPosition = shootOutPosition + nextPosition;
  342. Vector3 oldPosition = this.transform.position;
  343. this.transform.position = nextPosition;
  344. Vector3 eulerAngles = this.transform.eulerAngles;
  345. float angleX = Mathf.Atan(vy / vx) / Mathf.PI * 180;
  346. eulerAngles.x = -angleX;
  347. this.transform.eulerAngles = eulerAngles;
  348. float deltaDistance = Vector3.Distance(oldPosition, nextPosition);
  349. Ray ray = new Ray(oldPosition, nextPosition - oldPosition);
  350. RaycastHit raycastHit;
  351. bool raycastResult = Physics.Raycast(ray, out raycastHit, deltaDistance);
  352. if (raycastResult)
  353. {
  354. this.transform.position = raycastHit.point;
  355. if (ArrowTraceDebug.ins) ArrowTraceDebug.ins.OnArrowUpdate(this);
  356. OnHitAnyInFlyLogic(raycastHit);
  357. }
  358. else
  359. {
  360. if (ArrowTraceDebug.ins) ArrowTraceDebug.ins.OnArrowUpdate(this);
  361. }
  362. }
  363. /** 经典射击里面的 新飞行帧逻辑(运动学) */
  364. void UpdateFlyLogic_New()
  365. {
  366. if (isHit) return;
  367. // 飞行进度 (0 = 刚射出, 1 = 到达目标)
  368. float totalDistance = Vector3.Distance(shootOutPosition, finalPoint);
  369. float currentDistance = Vector3.Distance(transform.position, shootOutPosition);
  370. float progress = Mathf.Clamp01(currentDistance / totalDistance);
  371. // 从曲线获取速度倍率
  372. float speedMultiplier = ArmBow.ins.customCurveArrow.Evaluate(progress);
  373. float detalSlowFactor = currentSlowFactor * 0.5f;
  374. float _tempSlowFactor = currentSlowFactor + ( detalSlowFactor * speedMultiplier - detalSlowFactor);
  375. //Debug.Log("TempSlowFactor:"+ _tempSlowFactor);
  376. logicFlyTime += Time.deltaTime * _tempSlowFactor;
  377. float currentSpeed = mySpeed;
  378. float t = logicFlyTime;
  379. // --- 初速度分解 ---
  380. float vx = Mathf.Cos(parabolaAngleInRadian) * currentSpeed; // 水平速度大小
  381. float vy0 = Mathf.Sin(parabolaAngleInRadian) * currentSpeed; // 初始竖直速度
  382. // XZ方向向量(水平朝向目标)
  383. Vector3 dirXZ = new Vector3(finalPoint.x - shootOutPosition.x, 0, finalPoint.z - shootOutPosition.z).normalized;
  384. // --- 位移计算 ---
  385. float dx = vx * t; // 水平位移
  386. float dy = vy0 * t + 0.5f * Physics.gravity.y * t * t; // 竖直位移
  387. Vector3 nextPosition = shootOutPosition + dirXZ * dx + Vector3.up * dy;
  388. // --- 更新位置 ---
  389. Vector3 oldPosition = this.transform.position;
  390. this.transform.position = nextPosition;
  391. // --- 箭头朝向 ---
  392. float vy = vy0 + Physics.gravity.y * t; // 当前瞬时竖直速度
  393. float angleX = Mathf.Atan2(vy, vx) * Mathf.Rad2Deg;
  394. Vector3 eulerAngles = this.transform.eulerAngles;
  395. eulerAngles.x = -angleX;
  396. this.transform.eulerAngles = eulerAngles;
  397. // --- 碰撞检测 ---
  398. float deltaDistance = Vector3.Distance(oldPosition, nextPosition);
  399. Ray ray = new Ray(oldPosition, nextPosition - oldPosition);
  400. if (Physics.Raycast(ray, out RaycastHit raycastHit, deltaDistance))
  401. {
  402. this.transform.position = raycastHit.point;
  403. if (ArrowTraceDebug.ins) ArrowTraceDebug.ins.OnArrowUpdate(this);
  404. OnHitAnyInFlyLogic(raycastHit);
  405. Debug.Log(raycastHit.collider.gameObject.name);
  406. }
  407. else
  408. {
  409. if (ArrowTraceDebug.ins) ArrowTraceDebug.ins.OnArrowUpdate(this);
  410. }
  411. }
  412. public class HitType
  413. {
  414. public static int None = 0;
  415. public static int TargetInRing = 1; //击中了靶子,且在得分环内
  416. public static int TargetOutRing = 2; //击中了靶子,但不在得分环内
  417. public static int NotTarget = 4; //击中非目标对象
  418. public static int Animal = 5; //击中动物
  419. }
  420. [NonSerialized] public int hitType = HitType.None;
  421. [NonSerialized] public Transform raycastHitTransform; //射线击中的目标变换
  422. [NonSerialized] public string[] hitTargetAnimalInfo;
  423. //飞行逻辑中检测到碰撞
  424. void OnHitAnyInFlyLogic(RaycastHit raycastHit)
  425. {
  426. this.Head().position = raycastHit.point;
  427. this.transform.SetParent(raycastHit.transform.parent);
  428. string targetName = raycastHit.transform.gameObject.name;
  429. this.raycastHitTransform = raycastHit.transform;
  430. if (targetName == "TargetBody")
  431. {
  432. Vector3 hitPoint = raycastHit.point;
  433. if (rayHitTargetBody && canPerfectHit)
  434. {
  435. hitPoint = absoluteRay.point;
  436. this.Head().position = hitPoint;
  437. }
  438. if (GlobalData.MyDeviceMode == DeviceMode.Archery)
  439. this.arrowCameraComp.arrowCameraTemplate?.beforeHit();
  440. else
  441. nextShootFromType();//子弹直接下一轮
  442. raycastHit.transform.GetComponent<TargetBody>().Hit(this, hitPoint);
  443. }
  444. else if (targetName.StartsWith("TargetAnimalPart"))
  445. {
  446. hitType = HitType.Animal;
  447. Vector3 hitPoint = raycastHit.point;
  448. string partName = targetName.Split(new char[] { '_' })[1];
  449. if (GlobalData.MyDeviceMode == DeviceMode.Archery)
  450. this.arrowCameraComp.arrowCameraTemplate?.beforeHit();
  451. else
  452. nextShootFromType();//子弹直接下一轮
  453. TargetAnimal targetAnimal = raycastHit.transform.GetComponentInParent<TargetAnimal>();
  454. targetAnimal.OnHit(this, hitPoint, partName);
  455. //箭击中的音效
  456. AudioMgr.ins.PlayArrowEnter();
  457. //记录击中的部位和动物ID
  458. hitTargetAnimalInfo = new string[] {
  459. targetAnimal.GetOnlineID().ToString(),
  460. targetAnimal.targetAnimalParts.IndexOf(raycastHitTransform).ToString(),
  461. SyncDataUtil.Vec3ToStr(transform.localPosition),
  462. SyncDataUtil.QuatToStr(transform.localRotation),
  463. SyncDataUtil.Vec3ToStr(transform.position),
  464. SyncDataUtil.QuatToStr(transform.rotation)
  465. };
  466. }
  467. else if (raycastHit.transform.GetComponent<TargetOutBound>())
  468. { //撞到空气墙当作超时处理
  469. if (GlobalData.MyDeviceMode == DeviceMode.Archery)
  470. FlyTimeOut();
  471. else
  472. StartCoroutine(delayFlyTimeOut());
  473. }
  474. else
  475. {
  476. if (GlobalData.MyDeviceMode == DeviceMode.Archery)
  477. this.arrowCameraComp.arrowCameraTemplate?.beforeHit();
  478. else
  479. nextShootFromType();//子弹直接下一轮
  480. Hit();
  481. GameMgr.ins.gameMode.HitTarget(0);
  482. //击中其它东西时的音效
  483. hitType = HitType.NotTarget;
  484. if (UnityEngine.SceneManagement.SceneManager.GetActiveScene().name == "Game")
  485. {
  486. AudioMgr.ins.PlayCheer(false);
  487. }
  488. else
  489. {
  490. if (CommonConfig.StandaloneModeOrPlatformB) {
  491. AudioMgr.ins.PlayCheer(false);
  492. } else {
  493. AudioMgr.ins.PlayArrowEnter();
  494. }
  495. }
  496. }
  497. }
  498. public void Hit()
  499. {
  500. isHit = true;
  501. if (GameDebug.ins) GameDebug.ins.ShowRes(absoluteRay.point, this.Head().position);
  502. //控制箭的特效显示
  503. this.activeEffectCyclone(false);
  504. this.activeEffectBomb(true);
  505. this.activeEffectTrail(false);
  506. //最新一箭击中后会发光标记
  507. ArrowLightSick.RecoveryAll();
  508. this.GetComponentInChildren<ArrowLightSick>().Hit();
  509. }
  510. //进入下一轮射击
  511. [NonSerialized] public bool hasDoneNextShoot = false;
  512. public void nextShootFromType() {
  513. // Debug.Log("nextShootFromType:" + GlobalDataTemp.pkMatchType);
  514. if (GlobalDataTemp.pkMatchType == PKMatchType.LocalPK || GlobalDataTemp.pkMatchType == PKMatchType.OnlinePK)
  515. {
  516. StartCoroutine(delayNectShoot());
  517. }
  518. else {
  519. nextShoot();
  520. }
  521. }
  522. IEnumerator delayNectShoot() {
  523. yield return new WaitForSeconds(1.0f);
  524. nextShoot();
  525. }
  526. public void nextShoot()
  527. {
  528. if (hasDoneNextShoot) return;
  529. hasDoneNextShoot = true;
  530. GameMgr.ins.gameMode.ResumeTimeCounting(this);
  531. onDoNextShoot?.Invoke();
  532. try
  533. {
  534. //把瞄准点画成红圈,渲染在靶子上(取消)
  535. if (rayHitTargetBody)
  536. {
  537. //string typeStr = GlobalData.MyDeviceMode == DeviceMode.Archery ? "RedCircle" : "BulletCircle";
  538. Transform redCircle = rayHitTargetBody.transform.Find("RedCircle");
  539. redCircle.gameObject.SetActive(false);
  540. }
  541. //最新一箭击中后会发光标记(取消)
  542. ArrowLightSick.RecoveryAll();
  543. }
  544. catch (System.Exception e) { Debug.LogError(e.Message + "\n" + e.StackTrace); }
  545. if (!GameMgr.ins.gameMode.DoNextShoot()) return;
  546. this.armBow.readyShoot();
  547. }
  548. //---------箭矢旋转--------
  549. Vector3 rotateV3 = new Vector3(0, 0, 1400);
  550. void UpdateRotate()
  551. {
  552. this.Head().Rotate(rotateV3 * Time.deltaTime, Space.Self);
  553. }
  554. //---------箭矢特效---------
  555. public void activeEffectCyclone(bool value)
  556. {
  557. this.transform.Find("Head/EF_kuosanquan").gameObject.SetActive(value);
  558. if (!value) return;
  559. ParticleSystemRenderer ps = this.transform.Find("Head/EF_kuosanquan/kuosan").GetComponent<ParticleSystemRenderer>();
  560. ParticleSystemRenderer ps1 = this.transform.Find("Head/EF_kuosanquan/kuosan (1)").GetComponent<ParticleSystemRenderer>();
  561. DOTween.To(() => ps.minParticleSize, value =>
  562. {
  563. ps.minParticleSize = value;
  564. ps.maxParticleSize = value;
  565. }, 0.4f, 0.6f);
  566. DOTween.To(() => ps1.minParticleSize, value =>
  567. {
  568. ps1.minParticleSize = value;
  569. ps1.maxParticleSize = value;
  570. }, 0.8f, 0.6f);
  571. }
  572. void activeEffectBomb(bool value)
  573. {
  574. this.transform.Find("Head/EF_baodian").gameObject.SetActive(value);
  575. }
  576. void activeEffectTrail(bool value)
  577. {
  578. if (GlobalData.MyDeviceMode == DeviceMode.Archery)
  579. {
  580. Transform guadiantuowei = this.transform.Find("guadiantuowei");
  581. if (guadiantuowei == null)
  582. {
  583. this.transform.Find("EF_tuowei").gameObject.SetActive(value);
  584. this.transform.Find("EF_tuowei/Trail").GetComponent<TrailRenderer>().time = 1.6f / mySpeed;
  585. }
  586. else {
  587. guadiantuowei.gameObject.SetActive(value);
  588. }
  589. }
  590. else {
  591. StartCoroutine(showTrail(value));
  592. }
  593. }
  594. IEnumerator showTrail(bool value) {
  595. this.transform.Find("EF_tuowei_bullet").gameObject.SetActive(value);
  596. //this.transform.Find("EF_tuowei_bullet/Trail").GetComponent<TrailRenderer>().time = 0;
  597. yield return new WaitForEndOfFrame();
  598. this.transform.Find("EF_tuowei_bullet/GB_flame_FX_1").gameObject.SetActive(true);
  599. //this.transform.Find("EF_tuowei_bullet/Trail").GetComponent<TrailRenderer>().time = 0.02f;
  600. //this.transform.Find("EF_tuowei_bullet/Trail").GetComponent<TrailRenderer>().time = 1.6f / mySpeed;
  601. }
  602. }