1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677 |
- 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 Transform rootTran;
- public int count;
- public float maxDis;
- private LineRenderer _lineRenderer;
- public List<LinePointInfo> _linePointInfo = new List<LinePointInfo>();
- protected float minDis;
- private void Awake()
- {
- _lineRenderer = GetComponent<LineRenderer>();
- 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);
- for (int i = 1; i < _linePointInfo.Count; i++)
- {
- LinePointInfo point = _linePointInfo[i];
- LinePointInfo lasetPoint = _linePointInfo[i - 1];
- Vector3 dir = lasetPoint.pos - point.pos;
- Vector3 newDir = Vector3.Lerp(dir.normalized, lasetPoint.dir, 0.1f).normalized;
- point.pos = lasetPoint.pos + (newDir * (minDis * -1));
- point.dir = newDir;
- _lineRenderer.SetPosition(i, point.pos);
- // Quaternion q1= Quaternion.LookRotation(dir.normalized);
- // Quaternion q2= Quaternion.LookRotation(lasetPoint.dir);
- // Quaternion q = Quaternion.Lerp(q1, q2, 0.1f);
- }
- }
- }
|