| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class EyeLaser : MonoBehaviour
- {
- [Header("端点")]
- public GameObject head;
- public GameObject end;
- private GameObject end1, end2;
- public Transform headPos;
- public Transform endPos;
- public float deltaAngle1, deltaAngle2;
- public float lineLen;
- [Header("特效")]
- private bool hasPrepared;
- public GameObject lines;
- private GameObject lines1, lines2;
- private LineRenderer[] lineRenderers = new LineRenderer[5];
- private LineRenderer[] lineRenderers1 = new LineRenderer[5];
- private LineRenderer[] lineRenderers2 = new LineRenderer[5];
- public List<Material> lineMats = new List<Material>();
- public float minSpeed, maxSpeed;
- private float[] curSpeed = new float[5];
- [Header("时长")]
- public float traceTime;
- public float waitTime;
- public float continueTime;
- private bool isAttack;
- private bool isShine;
- private bool isTrace;
- private bool isWait;
- private float pastTime;
- [Header("大小")]
- private List<int> shaderIDs = new List<int>();
- private float bossScale;
- [Header("警示")]
- public GameObject tip;
- private GameObject tip1, tip2;
- private LaserHit lh, lh1, lh2;
- [Header("碰撞")]
- public Transform col;
- private Transform col1, col2;
- [Header("组件")]
- private YuMenGuan ymg;
- private void Prepare()
- {
- if (!hasPrepared)
- {
- hasPrepared = true;
- ymg = GetComponentInParent<YuMenGuan>();
- end1 = PoolManager.Instantiate(end, Vector3.zero, new Quaternion(0, 0, 0, 0), transform);
- end2 = PoolManager.Instantiate(end, Vector3.zero, new Quaternion(0, 0, 0, 0), transform);
- lines1 = PoolManager.Instantiate(lines, Vector3.zero, new Quaternion(0, 0, 0, 0), transform);
- lines2 = PoolManager.Instantiate(lines, Vector3.zero, new Quaternion(0, 0, 0, 0), transform);
- col1 = PoolManager.Instantiate(col, Vector3.zero, new Quaternion(0, 0, 0, 0), transform);
- col2 = PoolManager.Instantiate(col, Vector3.zero, new Quaternion(0, 0, 0, 0), transform);
- tip1 = lines1.transform.GetChild(4).gameObject;
- tip2 = lines2.transform.GetChild(4).gameObject;
- for (int i = 0; i < 5; i++)
- {
- lineRenderers[i] = lines.transform.GetChild(i).GetComponent<LineRenderer>();
- lineRenderers1[i] = lines1.transform.GetChild(i).GetComponent<LineRenderer>();
- lineRenderers2[i] = lines2.transform.GetChild(i).GetComponent<LineRenderer>();
- curSpeed[i] = Random.Range(minSpeed, maxSpeed);
- }
- foreach (Material m in lineMats)
- {
- shaderIDs.Add(Shader.PropertyToID(m.name));
- }
- lh = col.GetComponent<LaserHit>();
- lh1 = col1.GetComponent<LaserHit>();
- lh2 = col2.GetComponent<LaserHit>();
- }
- lh.enabled = false;
- lh1.enabled = false;
- lh2.enabled = false;
- lines.SetActive(true);
- lines1.SetActive(true);
- lines2.SetActive(true);
- }
- public void DrawLine()
- {
- Prepare();
- tip.SetActive(true);
- tip1.SetActive(true);
- tip2.SetActive(true);
- isShine = true;
- isTrace = true;
- }
- private void LineShine()
- {
- for(int i = 0; i < lineMats.Count; i++)
- {
- Vector2 offest = lineMats[i].mainTextureOffset;
- offest.x -= curSpeed[i] * Time.deltaTime;
- lineMats[i].SetTextureOffset("_MainTex", offest);
- }
- }
- private void ChangeCol()
- {
- bossScale = ymg.gameObject.transform.localScale.x;
- col.gameObject.SetActive(true);
- col1.gameObject.SetActive(true);
- col2.gameObject.SetActive(true);
- col.position = (headPos.position + TranEnd(0)) / 2;
- col1.position = (headPos.position + TranEnd(1)) / 2;
- col2.position = (headPos.position + TranEnd(2)) / 2;
- Vector3 ls = col.localScale;
- Vector3 ls1 = col1.localScale;
- Vector3 ls2 = col2.localScale;
- ls.x = Vector2.Distance(headPos.position, TranEnd(0));
- ls1.x = Vector2.Distance(headPos.position, TranEnd(1));
- ls2.x = Vector2.Distance(headPos.position, TranEnd(2));
- col.localScale = ls / bossScale;
- col1.localScale = ls1 / bossScale;
- col2.localScale = ls2 / bossScale;
- Vector3 left = headPos.position - TranEnd(0);
- Vector3 left1 = headPos.position - TranEnd(1);
- Vector3 left2 = headPos.position - TranEnd(2);
- left.z = 0;
- left1.z = 0;
- left2.z = 0;
- col.right = -left;
- col1.right = -left1;
- col2.right = -left2;
- }
- //点A绕点B转后坐标
- Vector2 RotatePointAroundPivot(Vector2 point, Vector2 pivot, float angleDegrees)
- {
- float angleRadians = angleDegrees * Mathf.Deg2Rad;
- float dx = point.x - pivot.x;
- float dy = point.y - pivot.y;
- float cosTheta = Mathf.Cos(angleRadians);
- float sinTheta = Mathf.Sin(angleRadians);
- float newX = cosTheta * dx - sinTheta * dy;
- float newY = sinTheta * dx + cosTheta * dy;
- return new Vector2(pivot.x + newX, pivot.y + newY);
- }
- private Vector3 TranEnd(int id)
- {
- Vector3 pos = (endPos.position - headPos.position).normalized * lineLen + headPos.position;
- pos.z = 0;
- switch (id)
- {
- case 1:
- pos = RotatePointAroundPivot(pos, headPos.position, deltaAngle1);
- break;
- case 2:
- pos = RotatePointAroundPivot(pos, headPos.position, deltaAngle2);
- break;
- default:
- break;
- }
- if (pos.y < -1)
- {
- float k = (headPos.position.y + 1) / (headPos.position.y - pos.y);
- pos.x = k * (pos.x - headPos.position.x) + headPos.position.x;
- pos.y = -1;
- }
- return pos;
- }
- private void Update()
- {
- if (isTrace)
- {
- pastTime += Time.deltaTime;
- if (endPos != null)
- {
- end.transform.position = TranEnd(0);
- end1.transform.position = TranEnd(1);
- end2.transform.position = TranEnd(2);
- Vector3[] poses = new Vector3[2];
- Vector3[] poses1 = new Vector3[2];
- Vector3[] poses2 = new Vector3[2];
- poses[0] = headPos.position;
- poses1[0] = headPos.position;
- poses2[0] = headPos.position;
- poses[1] = TranEnd(0);
- poses1[1] = TranEnd(1);
- poses2[1] = TranEnd(2);
- lineRenderers[4].SetPositions(poses);
- lineRenderers1[4].SetPositions(poses1);
- lineRenderers2[4].SetPositions(poses2);
- }
- if (pastTime >= traceTime)
- {
- isTrace = false;
- pastTime = 0;
- lineRenderers[4].SetPositions(new Vector3[0]);
- lineRenderers1[4].SetPositions(new Vector3[0]);
- lineRenderers2[4].SetPositions(new Vector3[0]);
- isWait = true;
- tip.SetActive(false);
- tip1.SetActive(false);
- tip2.SetActive(false);
- }
- }
- if (isWait)
- {
- pastTime += Time.deltaTime;
- if (pastTime >= waitTime)
- {
- pastTime = 0;
- isWait = false;
- Vector3[] poses = new Vector3[2];
- Vector3[] poses1 = new Vector3[2];
- Vector3[] poses2 = new Vector3[2];
- poses[0] = headPos.position;
- poses1[0] = headPos.position;
- poses2[0] = headPos.position;
- poses[1] = TranEnd(0);
- poses1[1] = TranEnd(1);
- poses2[1] = TranEnd(2);
- foreach (LineRenderer lr in lineRenderers)
- {
- lr.SetPositions(poses);
- }
- foreach (LineRenderer lr in lineRenderers1)
- {
- lr.SetPositions(poses1);
- }
- foreach (LineRenderer lr in lineRenderers2)
- {
- lr.SetPositions(poses2);
- }
- ChangeCol();
- isAttack = true;
- lh.enabled = true;
- lh1.enabled = true;
- lh2.enabled = true;
- head.transform.position = headPos.position;
- end.transform.position = TranEnd(0);
- end1.transform.position = TranEnd(1);
- end2.transform.position = TranEnd(2);
- head.SetActive(true);
- end.SetActive(true);
- end1.SetActive(true);
- end2.SetActive(true);
- }
- }
- if (isAttack)
- {
- pastTime += Time.deltaTime;
- if (pastTime >= continueTime)
- {
- lines.SetActive(false);
- lines1.SetActive(false);
- lines2.SetActive(false);
- head.SetActive(false);
- end.SetActive(false);
- end1.SetActive(false);
- end2.SetActive(false);
- foreach (LineRenderer lr in lineRenderers)
- {
- lr.SetPositions(new Vector3[2]);
- }
- foreach (LineRenderer lr in lineRenderers1)
- {
- lr.SetPositions(new Vector3[2]);
- }
- foreach (LineRenderer lr in lineRenderers2)
- {
- lr.SetPositions(new Vector3[2]);
- }
- pastTime = 0;
- isAttack = false;
- lh.enabled = false;
- lh1.enabled = false;
- lh2.enabled = false;
- col.gameObject.SetActive(false);
- col1.gameObject.SetActive(false);
- col2.gameObject.SetActive(false);
- ymg.EndCurAttackState(true);
- }
- }
- if (isShine)
- {
- LineShine();
- }
- }
- }
|