Arrow.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. using System;
  2. using UnityEngine;
  3. using DG.Tweening;
  4. /* 箭对象 */
  5. public class Arrow : MonoBehaviour
  6. {
  7. //飞行时间统计
  8. [NonSerialized] public float flyTime = 0;
  9. //标识—是否击中了什么
  10. [NonSerialized] public bool isHit = false;
  11. //箭的初速度(私用)
  12. private float mySpeed = 0;
  13. //箭的初速度(公用)
  14. public static float speed = GameMgr.RealSizeToGameSize(60);
  15. //箭射出时从弓传过来的参数
  16. #region
  17. //手臂弓
  18. [NonSerialized] public ArmBow armBow;
  19. //射出时的起始坐标
  20. [NonSerialized] public Vector3 shootOutPosition;
  21. //绝对射线
  22. [NonSerialized] public RaycastHit absoluteRay;
  23. //制作误差偏移角(强行制造误差,为了增加游戏难度,该偏移角的大小根据难度而定)
  24. [NonSerialized] public float offsetAngle;
  25. #endregion
  26. //射线击中的靶子
  27. TargetBody rayHitTargetBody;
  28. //能够完美击中射线点
  29. bool canPerfectHit;
  30. //能否使用侧面镜头
  31. [NonSerialized] public bool canUseSideCamera;
  32. [NonSerialized] public ArrowCamera arrowCameraComp;
  33. public System.Action onDoNextShoot;
  34. void Awake()
  35. {
  36. GameMgr.ins.gameMode.PauseTimeCounting(this);
  37. }
  38. void OnDestroy() {
  39. GameMgr.ins.gameMode.ResumeTimeCounting(this);
  40. }
  41. void Start()
  42. {
  43. Destroy(gameObject.GetComponent<BoxCollider>());
  44. ArrowDebugView.ins?.ClearLog();
  45. ArrowDebugView.ins?.AppendLog("p:" + shootOutPosition);
  46. ArrowDebugView.ins?.AppendLog("R:" + transform.rotation);
  47. ArrowDebugView.ins?.AppendLog("S:" + speed);
  48. if (ShootCheck.ins) {
  49. ArrowDebugView.ins?.AppendLog("bt1:" + ShootCheck.ins.byteTime1);
  50. ArrowDebugView.ins?.AppendLog("bt2:" + ShootCheck.ins.byteTime2);
  51. }
  52. mySpeed = speed;
  53. Billboard.ins?.SetArrowSpeed(speed);
  54. if (GameAssistUI.ins) {
  55. mySpeed *= GameAssistUI.ins.shootScaleValue;
  56. Billboard.ins?.SetArrowSpeedScale(GameAssistUI.ins.shootScaleValue);
  57. }
  58. Billboard.ins?.ShowSpeed();
  59. if (absoluteRay.transform) {
  60. //记录射线有没有击中靶子
  61. rayHitTargetBody = absoluteRay.transform.GetComponent<TargetBody>();
  62. //把瞄准点画成红圈,渲染在靶子上
  63. if (rayHitTargetBody) {
  64. Transform redCircle = rayHitTargetBody.transform.Find("RedCircle");
  65. redCircle.gameObject.SetActive(true);
  66. redCircle.transform.position = -redCircle.transform.forward * 0.001f + absoluteRay.point;
  67. }
  68. }
  69. SetUpBeforFly();
  70. ArrowDebugView.ins?.AppendLog("F:" + finalPoint);
  71. ArrowDebugView.ins?.AppendLog("FAR:" + parabolaAngleInRadian);
  72. Transform cameraTF = this.transform.Find("Camera");
  73. cameraTF.gameObject.SetActive(true);
  74. arrowCameraComp = cameraTF.gameObject.AddComponent<ArrowCamera>();
  75. arrowCameraComp.arrow = this;
  76. this.activeEffectTrail(true);
  77. }
  78. /**在箭飞行前初始化&如果瞄准射线的落点在靶子上时,才调用该函数 */
  79. void SetUpBeforFly()
  80. {
  81. //基础角
  82. float baseAngleX = FormatAngleX(this.transform.eulerAngles.x);
  83. //最大可调整的角度差
  84. float maxDeltaAngleX = 5;
  85. //最终角
  86. float finalAngleX = baseAngleX;
  87. if (absoluteRay.transform) {
  88. //看能否通过调整发射角让箭落在靶子的瞄准点上
  89. CalculateParabolaAngle(absoluteRay.point);
  90. //瞄准的是不是Target层
  91. bool isTargetLayer = IsTargetLayer(absoluteRay.transform.gameObject);
  92. //绝对发射角无解
  93. if (!hasParabolaAngle) {
  94. if (isTargetLayer) AimLoadChecker.ins?.ShowOutTip();
  95. } else {
  96. //来到这里,证明发射角有解,该解姑且叫它绝对角
  97. float absoluteAngleX = parabolaAngleInRadian / Mathf.PI * 180;
  98. //客户要求绝对角跟原本角度相差不能超过 maxDeltaAngleX
  99. float deltaAngleX = absoluteAngleX - baseAngleX;
  100. //如果绝对角跟原本角度相差不超过maxDeltaAngleX
  101. if (Mathf.Abs(deltaAngleX) < maxDeltaAngleX) {
  102. finalAngleX = Mathf.Clamp(absoluteAngleX + offsetAngle, -89, 89);
  103. if (Math.Abs(offsetAngle) < 0.01) {
  104. canPerfectHit = true;
  105. }
  106. if (rayHitTargetBody && Mathf.RoundToInt(rayHitTargetBody.GetDistance()) >= 50 && UnityEngine.Random.value < 0.5) {
  107. canUseSideCamera = true;
  108. }
  109. } else {
  110. finalAngleX = Mathf.Clamp(baseAngleX + maxDeltaAngleX + this.offsetAngle, -89, 89);
  111. if (isTargetLayer) AimLoadChecker.ins?.ShowOutTip();
  112. }
  113. }
  114. finalPoint = absoluteRay.point;
  115. } else {
  116. finalPoint = this.transform.position + this.transform.forward * 100;
  117. }
  118. parabolaAngleInRadian = finalAngleX / 180 * Mathf.PI;
  119. }
  120. /*
  121. 因为Unity引擎的规则,向上时359~270度,向下是0~90度
  122. 为了方便数学运算,换算成 向上时0~90度,向下是0~-90度
  123. */
  124. float FormatAngleX(float value)
  125. {
  126. if (value > 180) value = 360 - value;
  127. else value = -value;
  128. return value;
  129. }
  130. bool IsTargetLayer(GameObject gameObject) {
  131. return (1 << gameObject.layer) == LayerMask.GetMask("Target");
  132. }
  133. public Transform Head()
  134. {
  135. return this.transform.Find("Head");
  136. }
  137. /**发射角有无解 */
  138. bool hasParabolaAngle = false;
  139. /**发射角弧度解 */
  140. float parabolaAngleInRadian = 0;
  141. /**
  142. 求弓箭发射到指定坐标所需要的角度。
  143. 已知初速度大小V,重力g,起始坐标(a1,a2),目标坐标(b1,b2)。
  144. 解:
  145. 1、列出关系式
  146. Δx = b1 - a1;
  147. Δy = b2 - a2;
  148. Vx = V * cos(angle)
  149. Vy = V * sin(angle)
  150. Vy * t + 1/2 * g * t^2 = Δy
  151. Vx * t = Δx
  152. t = Δx / Vx
  153. 2、推导过程
  154. (V * sin(angle)) * Δx / (V * cos(angle)) + 1/2 * g * Δx^2 / (V^2*cos(angle)^2) = Δy
  155. tan(angle) * Δx + 1/2 * g * Δx^2 / (V^2*cos(angle)^2) = Δy
  156. tan(angle) * Δx + 1/2 * g * (Δx^2 / V^2) * (1 + tan(angle)^2) = Δy
  157. 3、根据求根公式得出结论
  158. a = 1/2 * g * Δx^2 / V^2
  159. b = Δx
  160. c = a - Δy
  161. d = tan(angle) = (-b ± (b^2 - 4*a*c)^0.5) / (2*a)
  162. angle = atan(d)
  163. 有无根的判别式,有根则b^2-4*a*c>=0
  164. */
  165. void CalculateParabolaAngle(Vector3 destination)
  166. {
  167. float deltaX = Vector2.Distance(
  168. new Vector2(destination.x, destination.z),
  169. new Vector2(this.transform.position.x, this.transform.position.z)
  170. );
  171. float deltaY = destination.y - this.transform.position.y;
  172. float a = 0.5f * Physics.gravity.y * Mathf.Pow(deltaX, 2) / Mathf.Pow(this.mySpeed, 2);
  173. float b = deltaX;
  174. float c = a - deltaY;
  175. hasParabolaAngle = Mathf.Pow(b, 2) - 4 * a * c >= 0;
  176. if (hasParabolaAngle) {
  177. float res1 = (-b + Mathf.Pow(Mathf.Pow(b, 2) - 4*a*c, 0.5f)) / (2 * a);
  178. float res2 = (-b - Mathf.Pow(Mathf.Pow(b, 2) - 4*a*c, 0.5f)) / (2 * a);
  179. parabolaAngleInRadian = Mathf.Min(Mathf.Atan(res1), Mathf.Atan(res2));
  180. }
  181. }
  182. void FixedUpdate()
  183. {
  184. if (!isHit && flyTime >= 0) {
  185. flyTime += Time.deltaTime;
  186. if (flyTime > 14) {
  187. FlyTimeOut();
  188. }
  189. this.UpdateRotate();
  190. }
  191. }
  192. void Update() {
  193. UpdateFlyLogic();
  194. }
  195. void FlyTimeOut() {
  196. Destroy(gameObject);
  197. GameMgr.ins.gameMode.HitTarget(0);
  198. AudioMgr.ins.PlayCheer(false);
  199. nextShoot();
  200. }
  201. float logicFlyTime = 0;
  202. Vector3 finalPoint;
  203. /**飞行帧逻辑(运动学) */
  204. void UpdateFlyLogic() {
  205. if (isHit) return;
  206. logicFlyTime += Time.deltaTime;
  207. Vector3 destination = finalPoint;
  208. float vx = Mathf.Cos(parabolaAngleInRadian) * mySpeed;
  209. float t = logicFlyTime;
  210. float vy = Mathf.Sin(parabolaAngleInRadian) * mySpeed + Physics.gravity.y * t;
  211. float dy = Mathf.Sin(parabolaAngleInRadian) * mySpeed * t + 0.5f * Physics.gravity.y * Mathf.Pow(t, 2);
  212. float dx = vx * t;
  213. Vector3 nextPosition = new Vector3(destination.x - shootOutPosition.x, 0, destination.z - shootOutPosition.z);
  214. nextPosition = nextPosition.normalized * dx;
  215. nextPosition.y = dy;
  216. nextPosition = shootOutPosition + nextPosition;
  217. Vector3 oldPosition = this.transform.position;
  218. this.transform.position = nextPosition;
  219. Vector3 eulerAngles = this.transform.eulerAngles;
  220. float angleX = Mathf.Atan(vy / vx) / Mathf.PI * 180;
  221. eulerAngles.x = -angleX;
  222. this.transform.eulerAngles = eulerAngles;
  223. float deltaDistance = Vector3.Distance(oldPosition, nextPosition);
  224. Ray ray = new Ray(oldPosition, nextPosition - oldPosition);
  225. RaycastHit raycastHit;
  226. bool raycastResult = Physics.Raycast(ray, out raycastHit, deltaDistance);
  227. if (raycastResult) {
  228. this.transform.position = raycastHit.point;
  229. OnHitAnyInFlyLogic(raycastHit);
  230. }
  231. }
  232. //飞行逻辑中检测到碰撞
  233. void OnHitAnyInFlyLogic(RaycastHit raycastHit) {
  234. this.Head().position = raycastHit.point;
  235. this.transform.SetParent(raycastHit.transform.parent);
  236. string targetName = raycastHit.transform.gameObject.name;
  237. if (targetName == "TargetBody") {
  238. Vector3 hitPoint = raycastHit.point;
  239. if (rayHitTargetBody && canPerfectHit) {
  240. hitPoint = absoluteRay.point;
  241. this.Head().position = hitPoint;
  242. }
  243. GetComponentInChildren<ArrowCamera>().arrowCameraTemplate?.beforeHit();
  244. raycastHit.transform.GetComponent<TargetBody>().Hit(this, hitPoint);
  245. } else if (targetName.StartsWith("TargetAnimalPart")) {
  246. Vector3 hitPoint = raycastHit.point;
  247. string partName = targetName.Split(new char[]{'_'})[1];
  248. GetComponentInChildren<ArrowCamera>().arrowCameraTemplate?.beforeHit();
  249. raycastHit.transform.GetComponentInParent<TargetAnimal>().OnHit(this, hitPoint, partName);
  250. //箭击中的音效
  251. AudioMgr.ins.PlayArrowEnter();
  252. } else if (raycastHit.transform.GetComponent<TargetOutBound>()) { //撞到空气墙当作超时处理
  253. FlyTimeOut();
  254. } else {
  255. GetComponentInChildren<ArrowCamera>().arrowCameraTemplate?.beforeHit();
  256. Hit();
  257. GameMgr.ins.gameMode.HitTarget(0);
  258. //击中其它东西时的音效
  259. if (UnityEngine.SceneManagement.SceneManager.GetActiveScene().name == "GameChallenge") {
  260. AudioMgr.ins.PlayArrowEnter();
  261. } else {
  262. AudioMgr.ins.PlayCheer(false);
  263. }
  264. }
  265. }
  266. public void Hit() {
  267. isHit = true;
  268. GameDebug.ins?.ShowRes(absoluteRay.point, this.Head().position);
  269. //控制箭的特效显示
  270. this.activeEffectCyclone(false);
  271. this.activeEffectBomb(true);
  272. this.activeEffectTrail(false);
  273. //最新一箭击中后会发光标记
  274. ArrowLightSick.RecoveryAll();
  275. this.GetComponentInChildren<ArrowLightSick>().Hit();
  276. }
  277. //进入下一轮射击
  278. public bool hasDoneNextShoot = false;
  279. public void nextShoot() {
  280. if (hasDoneNextShoot) return;
  281. hasDoneNextShoot = true;
  282. GameMgr.ins.gameMode.ResumeTimeCounting(this);
  283. onDoNextShoot?.Invoke();
  284. if (!GameMgr.ins.gameMode.DoNextShoot()) return;
  285. if (AimHandler.ins) AimHandler.ins.BanControlObjRotate(false);
  286. this.armBow.readyShoot();
  287. //把瞄准点画成红圈,渲染在靶子上(取消)
  288. if (rayHitTargetBody) {
  289. Transform redCircle = rayHitTargetBody.transform.Find("RedCircle");
  290. redCircle.gameObject.SetActive(false);
  291. }
  292. //最新一箭击中后会发光标记(取消)
  293. ArrowLightSick.RecoveryAll();
  294. }
  295. //---------箭矢旋转--------
  296. Vector3 rotateV3 = new Vector3(0, 0, 1400);
  297. void UpdateRotate() {
  298. this.Head().Rotate(rotateV3 * Time.deltaTime, Space.Self);
  299. }
  300. //---------箭矢特效---------
  301. public void activeEffectCyclone(bool value)
  302. {
  303. this.transform.Find("Head/EF_kuosanquan").gameObject.SetActive(value);
  304. if (!value) return;
  305. ParticleSystemRenderer ps = this.transform.Find("Head/EF_kuosanquan/kuosan").GetComponent<ParticleSystemRenderer>();
  306. ParticleSystemRenderer ps1 = this.transform.Find("Head/EF_kuosanquan/kuosan (1)").GetComponent<ParticleSystemRenderer>();
  307. DOTween.To(() => ps.minParticleSize, value => {
  308. ps.minParticleSize = value;
  309. ps.maxParticleSize = value;
  310. }, 0.4f, 0.6f);
  311. DOTween.To(() => ps1.minParticleSize, value => {
  312. ps1.minParticleSize = value;
  313. ps1.maxParticleSize = value;
  314. }, 0.8f, 0.6f);
  315. }
  316. void activeEffectBomb(bool value)
  317. {
  318. this.transform.Find("Head/EF_baodian").gameObject.SetActive(value);
  319. }
  320. void activeEffectTrail(bool value)
  321. {
  322. this.transform.Find("EF_tuowei").gameObject.SetActive(value);
  323. this.transform.Find("EF_tuowei/Trail").GetComponent<TrailRenderer>().time = 1.6f / mySpeed;
  324. }
  325. }