EyeLaser.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class EyeLaser : MonoBehaviour
  5. {
  6. [Header("端点")]
  7. public GameObject head;
  8. public GameObject end;
  9. private GameObject end1, end2;
  10. public Transform headPos;
  11. public Transform endPos;
  12. public float deltaAngle1, deltaAngle2;
  13. public float lineLen;
  14. [Header("特效")]
  15. private bool hasPrepared;
  16. public GameObject lines;
  17. private GameObject lines1, lines2;
  18. private LineRenderer[] lineRenderers = new LineRenderer[5];
  19. private LineRenderer[] lineRenderers1 = new LineRenderer[5];
  20. private LineRenderer[] lineRenderers2 = new LineRenderer[5];
  21. public List<Material> lineMats = new List<Material>();
  22. public float minSpeed, maxSpeed;
  23. private float[] curSpeed = new float[5];
  24. [Header("时长")]
  25. public float traceTime;
  26. public float waitTime;
  27. public float continueTime;
  28. private bool isAttack;
  29. private bool isShine;
  30. private bool isTrace;
  31. private bool isWait;
  32. private float pastTime;
  33. [Header("大小")]
  34. private List<int> shaderIDs = new List<int>();
  35. private float bossScale;
  36. [Header("警示")]
  37. public GameObject tip;
  38. private GameObject tip1, tip2;
  39. private LaserHit lh, lh1, lh2;
  40. [Header("碰撞")]
  41. public Transform col;
  42. private Transform col1, col2;
  43. [Header("组件")]
  44. private YuMenGuan ymg;
  45. private void Prepare()
  46. {
  47. if (!hasPrepared)
  48. {
  49. hasPrepared = true;
  50. ymg = GetComponentInParent<YuMenGuan>();
  51. end1 = PoolManager.Instantiate(end, Vector3.zero, new Quaternion(0, 0, 0, 0), transform);
  52. end2 = PoolManager.Instantiate(end, Vector3.zero, new Quaternion(0, 0, 0, 0), transform);
  53. lines1 = PoolManager.Instantiate(lines, Vector3.zero, new Quaternion(0, 0, 0, 0), transform);
  54. lines2 = PoolManager.Instantiate(lines, Vector3.zero, new Quaternion(0, 0, 0, 0), transform);
  55. col1 = PoolManager.Instantiate(col, Vector3.zero, new Quaternion(0, 0, 0, 0), transform);
  56. col2 = PoolManager.Instantiate(col, Vector3.zero, new Quaternion(0, 0, 0, 0), transform);
  57. tip1 = lines1.transform.GetChild(4).gameObject;
  58. tip2 = lines2.transform.GetChild(4).gameObject;
  59. for (int i = 0; i < 5; i++)
  60. {
  61. lineRenderers[i] = lines.transform.GetChild(i).GetComponent<LineRenderer>();
  62. lineRenderers1[i] = lines1.transform.GetChild(i).GetComponent<LineRenderer>();
  63. lineRenderers2[i] = lines2.transform.GetChild(i).GetComponent<LineRenderer>();
  64. curSpeed[i] = Random.Range(minSpeed, maxSpeed);
  65. }
  66. foreach (Material m in lineMats)
  67. {
  68. shaderIDs.Add(Shader.PropertyToID(m.name));
  69. }
  70. lh = col.GetComponent<LaserHit>();
  71. lh1 = col1.GetComponent<LaserHit>();
  72. lh2 = col2.GetComponent<LaserHit>();
  73. }
  74. lh.enabled = false;
  75. lh1.enabled = false;
  76. lh2.enabled = false;
  77. lines.SetActive(true);
  78. lines1.SetActive(true);
  79. lines2.SetActive(true);
  80. }
  81. public void DrawLine()
  82. {
  83. Prepare();
  84. tip.SetActive(true);
  85. tip1.SetActive(true);
  86. tip2.SetActive(true);
  87. isShine = true;
  88. isTrace = true;
  89. }
  90. private void LineShine()
  91. {
  92. for(int i = 0; i < lineMats.Count; i++)
  93. {
  94. Vector2 offest = lineMats[i].mainTextureOffset;
  95. offest.x -= curSpeed[i] * Time.deltaTime;
  96. lineMats[i].SetTextureOffset("_MainTex", offest);
  97. }
  98. }
  99. private void ChangeCol()
  100. {
  101. bossScale = ymg.gameObject.transform.localScale.x;
  102. col.gameObject.SetActive(true);
  103. col1.gameObject.SetActive(true);
  104. col2.gameObject.SetActive(true);
  105. col.position = (headPos.position + TranEnd(0)) / 2;
  106. col1.position = (headPos.position + TranEnd(1)) / 2;
  107. col2.position = (headPos.position + TranEnd(2)) / 2;
  108. Vector3 ls = col.localScale;
  109. Vector3 ls1 = col1.localScale;
  110. Vector3 ls2 = col2.localScale;
  111. ls.x = Vector2.Distance(headPos.position, TranEnd(0));
  112. ls1.x = Vector2.Distance(headPos.position, TranEnd(1));
  113. ls2.x = Vector2.Distance(headPos.position, TranEnd(2));
  114. col.localScale = ls / bossScale;
  115. col1.localScale = ls1 / bossScale;
  116. col2.localScale = ls2 / bossScale;
  117. Vector3 left = headPos.position - TranEnd(0);
  118. Vector3 left1 = headPos.position - TranEnd(1);
  119. Vector3 left2 = headPos.position - TranEnd(2);
  120. left.z = 0;
  121. left1.z = 0;
  122. left2.z = 0;
  123. col.right = -left;
  124. col1.right = -left1;
  125. col2.right = -left2;
  126. }
  127. //点A绕点B转后坐标
  128. Vector2 RotatePointAroundPivot(Vector2 point, Vector2 pivot, float angleDegrees)
  129. {
  130. float angleRadians = angleDegrees * Mathf.Deg2Rad;
  131. float dx = point.x - pivot.x;
  132. float dy = point.y - pivot.y;
  133. float cosTheta = Mathf.Cos(angleRadians);
  134. float sinTheta = Mathf.Sin(angleRadians);
  135. float newX = cosTheta * dx - sinTheta * dy;
  136. float newY = sinTheta * dx + cosTheta * dy;
  137. return new Vector2(pivot.x + newX, pivot.y + newY);
  138. }
  139. private Vector3 TranEnd(int id)
  140. {
  141. Vector3 pos = (endPos.position - headPos.position).normalized * lineLen + headPos.position;
  142. pos.z = 0;
  143. switch (id)
  144. {
  145. case 1:
  146. pos = RotatePointAroundPivot(pos, headPos.position, deltaAngle1);
  147. break;
  148. case 2:
  149. pos = RotatePointAroundPivot(pos, headPos.position, deltaAngle2);
  150. break;
  151. default:
  152. break;
  153. }
  154. if (pos.y < -1)
  155. {
  156. float k = (headPos.position.y + 1) / (headPos.position.y - pos.y);
  157. pos.x = k * (pos.x - headPos.position.x) + headPos.position.x;
  158. pos.y = -1;
  159. }
  160. return pos;
  161. }
  162. private void Update()
  163. {
  164. if (isTrace)
  165. {
  166. pastTime += Time.deltaTime;
  167. if (endPos != null)
  168. {
  169. end.transform.position = TranEnd(0);
  170. end1.transform.position = TranEnd(1);
  171. end2.transform.position = TranEnd(2);
  172. Vector3[] poses = new Vector3[2];
  173. Vector3[] poses1 = new Vector3[2];
  174. Vector3[] poses2 = new Vector3[2];
  175. poses[0] = headPos.position;
  176. poses1[0] = headPos.position;
  177. poses2[0] = headPos.position;
  178. poses[1] = TranEnd(0);
  179. poses1[1] = TranEnd(1);
  180. poses2[1] = TranEnd(2);
  181. lineRenderers[4].SetPositions(poses);
  182. lineRenderers1[4].SetPositions(poses1);
  183. lineRenderers2[4].SetPositions(poses2);
  184. }
  185. if (pastTime >= traceTime)
  186. {
  187. isTrace = false;
  188. pastTime = 0;
  189. lineRenderers[4].SetPositions(new Vector3[0]);
  190. lineRenderers1[4].SetPositions(new Vector3[0]);
  191. lineRenderers2[4].SetPositions(new Vector3[0]);
  192. isWait = true;
  193. tip.SetActive(false);
  194. tip1.SetActive(false);
  195. tip2.SetActive(false);
  196. }
  197. }
  198. if (isWait)
  199. {
  200. pastTime += Time.deltaTime;
  201. if (pastTime >= waitTime)
  202. {
  203. pastTime = 0;
  204. isWait = false;
  205. Vector3[] poses = new Vector3[2];
  206. Vector3[] poses1 = new Vector3[2];
  207. Vector3[] poses2 = new Vector3[2];
  208. poses[0] = headPos.position;
  209. poses1[0] = headPos.position;
  210. poses2[0] = headPos.position;
  211. poses[1] = TranEnd(0);
  212. poses1[1] = TranEnd(1);
  213. poses2[1] = TranEnd(2);
  214. foreach (LineRenderer lr in lineRenderers)
  215. {
  216. lr.SetPositions(poses);
  217. }
  218. foreach (LineRenderer lr in lineRenderers1)
  219. {
  220. lr.SetPositions(poses1);
  221. }
  222. foreach (LineRenderer lr in lineRenderers2)
  223. {
  224. lr.SetPositions(poses2);
  225. }
  226. ChangeCol();
  227. isAttack = true;
  228. lh.enabled = true;
  229. lh1.enabled = true;
  230. lh2.enabled = true;
  231. head.transform.position = headPos.position;
  232. end.transform.position = TranEnd(0);
  233. end1.transform.position = TranEnd(1);
  234. end2.transform.position = TranEnd(2);
  235. head.SetActive(true);
  236. end.SetActive(true);
  237. end1.SetActive(true);
  238. end2.SetActive(true);
  239. }
  240. }
  241. if (isAttack)
  242. {
  243. pastTime += Time.deltaTime;
  244. if (pastTime >= continueTime)
  245. {
  246. lines.SetActive(false);
  247. lines1.SetActive(false);
  248. lines2.SetActive(false);
  249. head.SetActive(false);
  250. end.SetActive(false);
  251. end1.SetActive(false);
  252. end2.SetActive(false);
  253. foreach (LineRenderer lr in lineRenderers)
  254. {
  255. lr.SetPositions(new Vector3[2]);
  256. }
  257. foreach (LineRenderer lr in lineRenderers1)
  258. {
  259. lr.SetPositions(new Vector3[2]);
  260. }
  261. foreach (LineRenderer lr in lineRenderers2)
  262. {
  263. lr.SetPositions(new Vector3[2]);
  264. }
  265. pastTime = 0;
  266. isAttack = false;
  267. lh.enabled = false;
  268. lh1.enabled = false;
  269. lh2.enabled = false;
  270. col.gameObject.SetActive(false);
  271. col1.gameObject.SetActive(false);
  272. col2.gameObject.SetActive(false);
  273. ymg.EndCurAttackState(true);
  274. }
  275. }
  276. if (isShine)
  277. {
  278. LineShine();
  279. }
  280. }
  281. }