EyeLaser.cs 9.9 KB

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