TailingLine.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. public class TailingLine : MonoBehaviour
  6. {
  7. [System.Serializable]
  8. public class LinePointInfo
  9. {
  10. public Vector3 pos;
  11. public Vector3 rotion;
  12. public Vector3 dir;
  13. }
  14. public AnimationCurve offQuanZhong;
  15. public float quanZhong=5;
  16. // public float addTime;
  17. public Transform rootTran;
  18. public int count;
  19. public float maxDis;
  20. private LineRenderer _lineRenderer;
  21. public List<LinePointInfo> _linePointInfo = new List<LinePointInfo>();
  22. protected float minDis;
  23. private void Awake()
  24. {
  25. _lineRenderer = GetComponent<LineRenderer>();
  26. Init();
  27. }
  28. private void Init()
  29. {
  30. minDis = maxDis / count;
  31. // Vector3 startPos = rootTran.position;
  32. Vector3 startRot = rootTran.rotation.eulerAngles;
  33. _lineRenderer.positionCount = count;
  34. for (int i = 0; i < count; i++)
  35. {
  36. LinePointInfo linePointInfo = new LinePointInfo();
  37. Vector3 pos = rootTran.transform.TransformPoint(new Vector3(0, 0, minDis * i * -1));
  38. linePointInfo.pos = pos;
  39. linePointInfo.rotion = startRot;
  40. linePointInfo.dir = rootTran.transform.forward;
  41. _lineRenderer.SetPosition(i, linePointInfo.pos);
  42. _linePointInfo.Add(linePointInfo);
  43. }
  44. }
  45. void Start()
  46. {
  47. }
  48. // Update is called once per frame
  49. void Update()
  50. {
  51. LinePointInfo linePointInfo = _linePointInfo[0];
  52. linePointInfo.pos = rootTran.position;
  53. linePointInfo.rotion = rootTran.rotation.eulerAngles;
  54. linePointInfo.dir = rootTran.transform.forward;
  55. _lineRenderer.SetPosition(0, linePointInfo.pos);
  56. // float at = (1 - addTime) / _linePointInfo.Count;
  57. int m = _linePointInfo.Count / 2;
  58. int c = _linePointInfo.Count - m;
  59. float quanZhongWidget = 1.0f / c;
  60. for (int i = 1; i < _linePointInfo.Count; i++)
  61. {
  62. float w = i * 1.0f / _linePointInfo.Count;
  63. float at = offQuanZhong.Evaluate(w);
  64. if (at <= 0)
  65. {
  66. at = 0.01f;
  67. }
  68. LinePointInfo point = _linePointInfo[i];
  69. LinePointInfo lasetPoint = _linePointInfo[i - 1];
  70. Vector3 dir = lasetPoint.pos - point.pos;
  71. float md = dir.magnitude;
  72. if (md < minDis)
  73. {
  74. md = minDis;
  75. }
  76. else
  77. {
  78. md = Mathf.Lerp(md, minDis, at);
  79. }
  80. if (m == i)
  81. {
  82. var sin = Mathf.Sin(Time.time);
  83. var cos = Mathf.Cos(Time.time);
  84. Quaternion quaternion = Quaternion.Euler(sin * quanZhong, cos * quanZhong, 0);
  85. dir = quaternion * dir;
  86. }
  87. Vector3 newDir = Vector3.Lerp(dir.normalized, lasetPoint.dir, at).normalized;
  88. point.pos = lasetPoint.pos + (newDir * (md * -1));
  89. point.dir = newDir;
  90. _lineRenderer.SetPosition(i, point.pos);
  91. }
  92. }
  93. }