Arrow.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. using System.Collections.Generic;
  2. using UnityEngine;
  3. using DG.Tweening;
  4. public class Arrow : MonoBehaviour
  5. {
  6. private Rigidbody newRigidbody;
  7. public float flyTime = 0;
  8. public bool isHit = false;
  9. public ArmBow armBow;
  10. public static float speed = 40;
  11. //镜头跟随飞出的箭矢
  12. public static bool followArrow = true;
  13. void Start()
  14. {
  15. // CrossHair.ins.gameObject.SetActive(false);
  16. newRigidbody = this.gameObject.AddComponent<Rigidbody>();
  17. float speedCopy = speed;
  18. if (GameAssistUI.ins) {
  19. speedCopy *= (1 + GameAssistUI.ins.shootScaleValue);
  20. }
  21. newRigidbody.velocity = this.transform.forward * speedCopy;
  22. newRigidbody.collisionDetectionMode = CollisionDetectionMode.ContinuousDynamic;
  23. this.transform.Find("Camera").gameObject.SetActive(followArrow);
  24. this.activeEffectCyclone(true);
  25. this.activeEffectTrail(true);
  26. if (!followArrow) {
  27. GameObject.FindObjectOfType<BowCamera>().onViewRecovery = nextShoot;
  28. }
  29. }
  30. void OnDestroy() {
  31. if (this.newRigidbody)
  32. {
  33. Destroy(this.newRigidbody);
  34. }
  35. }
  36. Vector3 position;
  37. Quaternion rotation;
  38. void FixedUpdate()
  39. {
  40. if (newRigidbody) {
  41. transform.forward = newRigidbody.velocity.normalized;
  42. }
  43. if (!isHit && flyTime >= 0) {
  44. flyTime += Time.deltaTime;
  45. this.position = this.transform.position;
  46. this.rotation = this.transform.rotation;
  47. if (flyTime > 4) {
  48. Destroy(gameObject);
  49. GameMgr.ins.gameMode.HitTarget(0);
  50. AudioMgr.ins.PlayCheer(false);
  51. if (followArrow) {
  52. nextShoot();
  53. }
  54. }
  55. this.UpdateRotate();
  56. }
  57. this.UpdateShake();
  58. this.updateMoveCamera();
  59. }
  60. public void Hit() {
  61. gameObject.GetComponent<BoxCollider>().enabled = false;
  62. if (newRigidbody) {
  63. newRigidbody.useGravity = false;
  64. newRigidbody.velocity = Vector3.zero;
  65. Destroy(newRigidbody);
  66. newRigidbody = null;
  67. }
  68. isHit = true;
  69. // this.Shake();
  70. this.activeEffectCyclone(false);
  71. this.activeEffectBomb(true);
  72. this.activeEffectTrail(false);
  73. }
  74. public Vector3 getHeadPosition() {
  75. return this.transform.Find("Head").position;
  76. }
  77. /**箭矢旋转 */
  78. private Vector3 rotateV3 = new Vector3(0, 0, 1400);
  79. private void UpdateRotate() {
  80. this.transform.Find("Head").Rotate(rotateV3 * Time.deltaTime, Space.Self);
  81. }
  82. /**箭矢震动 */
  83. private bool shaking = false;
  84. private float shakeTime = 0;
  85. private float shakeTimeMax = 0.5f;
  86. private float shakeRange = 5;
  87. private int shakeDirection = 1;
  88. private Vector3 shakeAngles = new Vector3();
  89. private void Shake()
  90. {
  91. this.shaking = true;
  92. this.shakeTime = 0;
  93. this.shakeDirection = 1;
  94. }
  95. private void UpdateShake()
  96. {
  97. if (!this.shaking) {
  98. return;
  99. }
  100. Transform transform = this.transform.Find("Head").transform;
  101. float shakeRangeNow = (1 - this.shakeTime / this.shakeTimeMax) * this.shakeRange;
  102. if (shakeRangeNow <= 0) {//震动结束
  103. this.shaking = false;
  104. this.shakeAngles.x = 0;
  105. } else {
  106. this.shakeAngles.x = -this.shakeDirection * shakeRangeNow;
  107. this.shakeDirection *= -1;
  108. }
  109. transform.localEulerAngles = this.shakeAngles;
  110. this.shakeTime += Time.deltaTime;
  111. }
  112. /**相机移动 */
  113. private Vector3 cameraPosition = new Vector3();
  114. private bool cameraMoveFinish = false;
  115. //相机的位置xyz变化 【变化量,最小值,最大值】
  116. private float[] cpcs = {
  117. //运动中
  118. 0, 0, 0,
  119. 1.5f, 0, 0.25f,
  120. -7, -1.5f, 0,
  121. //击中后
  122. -3.5f, -1.2f, 0,
  123. -3.5f, 0, 0.5f,
  124. 7, -1.5f, -1.5f
  125. };
  126. private void updateMoveCamera() {
  127. if (cameraMoveFinish) {
  128. return;
  129. }
  130. Transform cameraT = this.transform.Find("Camera");
  131. cameraPosition.x = cameraT.localPosition.x;
  132. cameraPosition.y = cameraT.localPosition.y;
  133. cameraPosition.z = cameraT.localPosition.z;
  134. if (this.isHit) {
  135. cameraPosition.x = Mathf.Clamp(cameraPosition.x + this.cpcs[9] * Time.deltaTime, this.cpcs[10], this.cpcs[11]);
  136. cameraPosition.y = Mathf.Clamp(cameraPosition.y + this.cpcs[12] * Time.deltaTime, this.cpcs[13], this.cpcs[14]);
  137. cameraPosition.z = Mathf.Clamp(cameraPosition.z + this.cpcs[15] * Time.deltaTime, this.cpcs[16], this.cpcs[17]);
  138. float d = Vector3.Distance(cameraPosition, new Vector3(
  139. this.cpcs[9] < 0 ? this.cpcs[10]: this.cpcs[11],
  140. this.cpcs[12] < 0 ? this.cpcs[13]: this.cpcs[14],
  141. this.cpcs[15] < 0 ? this.cpcs[16]: this.cpcs[17]
  142. ));
  143. if (d < 0.001f) {
  144. cameraMoveFinish = true;
  145. if (followArrow) {
  146. Sequence seq = DOTween.Sequence();
  147. seq.AppendInterval(1.0f);
  148. seq.AppendCallback(delegate() {
  149. nextShoot();
  150. Destroy(cameraT.gameObject);
  151. this.enabled = false;
  152. });
  153. }
  154. }
  155. } else {
  156. cameraPosition.x = Mathf.Clamp(cameraPosition.y + this.cpcs[0] * Time.deltaTime, this.cpcs[1], this.cpcs[2]);
  157. cameraPosition.y = Mathf.Clamp(cameraPosition.y + this.cpcs[3] * Time.deltaTime, this.cpcs[4], this.cpcs[5]);
  158. cameraPosition.z = Mathf.Clamp(cameraPosition.z + this.cpcs[6] * Time.deltaTime, this.cpcs[7], this.cpcs[8]);
  159. }
  160. cameraT.localPosition = cameraPosition;
  161. cameraT.LookAt(this.transform.Find("Head"));
  162. }
  163. /**延时方法 */
  164. public void nextShoot() {
  165. if (!GameMgr.ins.gameMode.DoNextShoot()) return;
  166. this.armBow.readyShoot();
  167. // CrossHair.ins.gameObject.SetActive(true);
  168. }
  169. void OnCollisionEnter(Collision collision) {
  170. if ((1 << collision.gameObject.layer) != LayerMask.GetMask("Target"))
  171. {
  172. this.Hit();
  173. GameMgr.ins.gameMode.HitTarget(0);
  174. AudioMgr.ins.PlayCheer(false);
  175. }
  176. this.transform.SetParent(collision.transform.parent);
  177. }
  178. void activeEffectCyclone(bool value)
  179. {
  180. this.transform.Find("Head/EF_kuosanquan").gameObject.SetActive(value);
  181. ParticleSystemRenderer ps = this.transform.Find("Head/EF_kuosanquan/kuosan").GetComponent<ParticleSystemRenderer>();
  182. ParticleSystemRenderer ps1 = this.transform.Find("Head/EF_kuosanquan/kuosan (1)").GetComponent<ParticleSystemRenderer>();
  183. if (!followArrow) {
  184. ps.minParticleSize = 0.3f;
  185. ps.maxParticleSize = 0.3f;
  186. ps1.minParticleSize = 0.6f;
  187. ps1.maxParticleSize = 0.6f;
  188. }
  189. DOTween.To(() => ps.minParticleSize, value => {
  190. ps.minParticleSize = value;
  191. ps.maxParticleSize = value;
  192. }, followArrow ? 0.6f : 0.06f, 0.6f);
  193. DOTween.To(() => ps1.minParticleSize, value => {
  194. ps1.minParticleSize = value;
  195. ps1.maxParticleSize = value;
  196. }, followArrow ? 1.2f : 0.12f, 0.6f);
  197. }
  198. void activeEffectBomb(bool value)
  199. {
  200. this.transform.Find("Head/EF_baodian").gameObject.SetActive(value);
  201. }
  202. void activeEffectTrail(bool value)
  203. {
  204. this.transform.Find("EF_tuowei").gameObject.SetActive(value);
  205. this.transform.Find("EF_tuowei/Trail").GetComponent<TrailRenderer>().time = 1.6f / speed;
  206. }
  207. }