TailingLine.cs 3.1 KB

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