LineRenderer画运动轨迹
网上关于LineRenderer的资料比较少,最后参考了这篇,应用到自己的场景中。
首先定义空物体,并转为预制体;默认创建了两个点,并定义线的宽度,最后给线上材质:
然后创建运动轨迹脚本并关联到运动的物体上:
脚本如下,供参考:
代码语言:javascript复制using System.Collections;
using System.Collections.Generic;
using UnityEngine;
// 创建LineRenderer画出运动轨迹
public class MotorLine : MonoBehaviour
{
public GameObject lineprefab;
public GameObject currentline;
public GameObject emptyPrefab;
public GameObject lineObject;
public LineRenderer line;
private Vector3[] path;
private List<Vector3> pos = new List<Vector3>();
private float timer;
private void Start()
{
lineObject = Instantiate(emptyPrefab, transform.position, Quaternion.identity, gameObject.transform);
}
// Update is called once per frame
private void FixedUpdate()
{
//每过5s消除之前轨迹
if (Time.time % 5 == 0 && Time.time >= 5)
{
pos.Clear();
path = pos.ToArray();
Destroy(lineObject);
lineObject = Instantiate(emptyPrefab, transform.position, Quaternion.identity, gameObject.transform);
}
//每过0.1s画一次
if (timer <= 0)
{
currentline = Instantiate(lineprefab, transform.position, Quaternion.identity, lineObject.transform);
line = currentline.GetComponent<LineRenderer>();
pos.Add(transform.position);
path = pos.ToArray();
timer = 0.1f;
}
timer -= Time.deltaTime;
if (path.Length != 0)
{
line.positionCount = path.Length;
line.SetPositions(path);
}
}
}
最后结果如下(蓝线):
以上。