TailingLine.cs 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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 Transform rootTran;
  15. public int count;
  16. public float maxDis;
  17. private LineRenderer _lineRenderer;
  18. public List<LinePointInfo> _linePointInfo = new List<LinePointInfo>();
  19. protected float minDis;
  20. private void Awake()
  21. {
  22. _lineRenderer = GetComponent<LineRenderer>();
  23. Init();
  24. }
  25. private void Init()
  26. {
  27. minDis = maxDis / count;
  28. // Vector3 startPos = rootTran.position;
  29. Vector3 startRot = rootTran.rotation.eulerAngles;
  30. _lineRenderer.positionCount = count;
  31. for (int i = 0; i < count; i++)
  32. {
  33. LinePointInfo linePointInfo = new LinePointInfo();
  34. Vector3 pos = rootTran.transform.TransformPoint(new Vector3(0, 0, minDis * i*-1));
  35. linePointInfo.pos = pos;
  36. linePointInfo.rotion = startRot;
  37. linePointInfo.dir = rootTran.transform.forward;
  38. _lineRenderer.SetPosition(i, linePointInfo.pos);
  39. _linePointInfo.Add(linePointInfo);
  40. }
  41. }
  42. void Start()
  43. {
  44. }
  45. // Update is called once per frame
  46. void Update()
  47. {
  48. LinePointInfo linePointInfo = _linePointInfo[0];
  49. linePointInfo.pos = rootTran.position;
  50. linePointInfo.rotion = rootTran.rotation.eulerAngles;
  51. linePointInfo.dir = rootTran.transform.forward;
  52. _lineRenderer.SetPosition(0, linePointInfo.pos);
  53. for (int i = 1; i < _linePointInfo.Count; i++)
  54. {
  55. LinePointInfo point = _linePointInfo[i];
  56. LinePointInfo lasetPoint = _linePointInfo[i - 1];
  57. Vector3 dir = lasetPoint.pos - point.pos;
  58. Vector3 newDir = Vector3.Lerp(dir.normalized, lasetPoint.dir, 0.1f).normalized;
  59. point.pos = lasetPoint.pos + (newDir * (minDis * -1));
  60. point.dir = newDir;
  61. _lineRenderer.SetPosition(i, point.pos);
  62. // Quaternion q1= Quaternion.LookRotation(dir.normalized);
  63. // Quaternion q2= Quaternion.LookRotation(lasetPoint.dir);
  64. // Quaternion q = Quaternion.Lerp(q1, q2, 0.1f);
  65. }
  66. }
  67. }