Arrow.cs 12 KB

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