我正在尝试根据存储在数组中的一些游戏对象的位置在Unity中制作图形。到目前为止这是我的代码。什么是在2个连续点之间移动线条渲染器,直到它到达最后一个点。我想要做的是保留上一行,创建新行并将其移动到下一个点。知道怎么做到这一点?非常感谢。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DrawLine : MonoBehaviour {
private LineRenderer lineRenderer;
private LineRenderer[] lineRenderers;
private float counter;
private float dist;
private int i = 0;
public Transform origin;
public Transform destination;
public float lineDrawSpeed;
public GameObject[] Points;
void Start ()
{
lineRenderer = GetComponent<LineRenderer> ();
lineRenderers = new LineRenderer[40];
lineRenderer.startWidth = 0.45f;
lineRenderer.endWidth = 0.45f;
}
void Update()
{
if (StockManager.isReady)
{
Points = GameObject.FindGameObjectsWithTag ("Point");
StartCoroutine (DrawTheLine ());
StockManager.isReady = false;
}
}
IEnumerator DrawTheLine()
{
//lineRenderer.SetPosition (0, Points [0].transform.position);
dist = Vector3.Distance (Points [0].transform.position, Points [1].transform.position);
while (counter < dist && i < Points.Length - 1)
{
lineRenderer.SetPosition (0, Points [i].transform.position);
counter = Time.deltaTime * lineDrawSpeed;
dist = Vector3.Distance (Points [i].transform.position, Points [++i].transform.position);
float x = Mathf.Lerp (0, dist, counter);
Vector3 pointA = Points [i].transform.position;
Vector3 pointB = Points [i++].transform.position;
Vector3 pointAlongLine = x * Vector3.Normalize (pointB - pointA) + pointA;
lineRenderer.SetPosition (1, pointAlongLine);
i--;
yield return new WaitForSecondsRealtime (0.2f);
}
yield return null;
}
}
答案 0 :(得分:0)
对不起,我在评论中误导了你。 LineRenderer是一个组件,因此只需执行new LineRenderer()
即可。预计会将其添加到GameObject
。摆脱阵列,你将不需要它。你的while循环的第一行可以LineRenderer lineRenderer = gameObject.AddComponent<LineRenderer>();
,这应该可以解决你的问题。您的游戏对象将添加约40个不同的LineRenderer组件。但是,如果您计划访问LR并操纵它们,那么保持阵列将来可能会有所帮助。