using System; using System.Collections; using System.Collections.Generic; using UnityEngine; public class TailingLine : MonoBehaviour { [System.Serializable] public class LinePointInfo { public Vector3 pos; public Vector3 rotion; public Vector3 dir; } public AnimationCurve offQuanZhong; public float quanZhong=5; public float disStrength=0.5f; // public float addTime; public Transform rootTran; public int count; public float maxDis; private LineRenderer _lineRenderer; public List _linePointInfo = new List(); protected float minDis; private void Awake() { _linePointInfo.Clear(); _lineRenderer = GetComponent(); Init(); } private void Init() { minDis = maxDis / count; // Vector3 startPos = rootTran.position; Vector3 startRot = rootTran.rotation.eulerAngles; _lineRenderer.positionCount = count; for (int i = 0; i < count; i++) { LinePointInfo linePointInfo = new LinePointInfo(); Vector3 pos = rootTran.transform.TransformPoint(new Vector3(0, 0, minDis * i * -1)); linePointInfo.pos = pos; linePointInfo.rotion = startRot; linePointInfo.dir = rootTran.transform.forward; _lineRenderer.SetPosition(i, linePointInfo.pos); _linePointInfo.Add(linePointInfo); } } void Start() { } // Update is called once per frame void Update() { LinePointInfo linePointInfo = _linePointInfo[0]; linePointInfo.pos = rootTran.position; linePointInfo.rotion = rootTran.rotation.eulerAngles; linePointInfo.dir = rootTran.transform.forward; _lineRenderer.SetPosition(0, linePointInfo.pos); // float at = (1 - addTime) / _linePointInfo.Count; int m = 2; int c = _linePointInfo.Count - m; float quanZhongWidget = 1.0f / c; for (int i = 1; i < _linePointInfo.Count; i++) { float w = i * 1.0f / _linePointInfo.Count; float at = offQuanZhong.Evaluate(w); if (at <= 0) { at = 0.01f; } LinePointInfo point = _linePointInfo[i]; LinePointInfo lasetPoint = _linePointInfo[i - 1]; Vector3 dir = lasetPoint.pos - point.pos; float md = dir.magnitude; if (md < minDis) { md = minDis; } else { md = Mathf.Lerp(md, minDis, at*disStrength); } if (m == i) { var sin = Mathf.Sin(Time.time); var cos = Mathf.Cos(Time.time); Quaternion quaternion = Quaternion.Euler(sin * quanZhong, cos * quanZhong, 0); dir = quaternion * dir; } Vector3 newDir = Vector3.Lerp(dir.normalized, lasetPoint.dir, at).normalized; point.pos = lasetPoint.pos + (newDir * (md * -1)); point.dir = newDir; _lineRenderer.SetPosition(i, point.pos); } } }