- Home /
How to render a LineRenderer through multiple points?
Hello i am having a little trouble trying to render Lines through multiple points but the only thing i'm getting is whats in the picture, its only the start point to 0,0,0 anyone know how to fix this?
Here is my code so you know what i have done.
using UnityEngine;
using System.Collections;
[RequireComponent(typeof(LineRenderer))]
public class Lines : MonoBehaviour {
LineRenderer lineRenderer;
public Transform[] points;
void Update () {
lineRenderer = GetComponent<LineRenderer>();
for (int i = 0; i < points.Length; i++)
lineRenderer.SetPosition(0, points[0].position);
}
}
Answer by alexander11 · Aug 07, 2016 at 02:33 AM
OK i have fixed it here is the code.
using UnityEngine;
using System.Collections;
[RequireComponent(typeof(LineRenderer))]
public class Lines : MonoBehaviour {
LineRenderer lineRenderer;
public Transform[] points;
private Vector3[] vP;
int seg = 20;
void Start()
{
lineRenderer = GetComponent<LineRenderer>();
Line();
}
void Line () {
seg = points.Length;
vP = new Vector3[points.Length];
for (int i = 0; i < points.Length; i++)
{
vP[i] = points[i].position;
}
for (int i = 0; i < seg; i++)
{
float t = i / (float)seg;
lineRenderer.SetVertexCount(seg);
lineRenderer.SetPositions(vP);
}
}
}
float t = i / (float)seg;
What's this line's purpose?
For the amount of vertex/vertices, then i put in the lineRenderer.SetVertexCount. it's practically the Segment count.
since i have achieved this for the LineRenderer, would you know how to spawn object's in between the all of the points? this would be helpful because i am trying to create nodes so i can render a road.
Answer by StormMuller · Aug 07, 2016 at 02:06 AM
You have 3 problems here.
You are not using the
i
variable made in the loop, so you are only using the first position. To fix this replacelineRenderer.SetPosition(0, points[0].position);
withlineRenderer.SetPosition(0, points[i].position);
You are using
SetPosition
instead ofSetPositions
doing this whole computation in the update function is really inefficient.
So this is what I recommend you do:
Using System.Collections.Generic;
[RequireComponent(typeof(LineRenderer))]
public class Lines : MonoBehaviour {
LineRenderer lineRenderer;
public Transform[] points;
private Vector3[] positionsOfPoints;
void Start(){
lineRenderer = GetComponent<LineRenderer>();
UpdateLine(); // this can be called in update if your positions aren't static
}
void UpdateLine()
{
List<Vector3> temp = new List<Vector3>():
foreach(Transform t in points)
{
temp.Add(t.position);
}
positionsOfPoints = temp.ToArray();
lineRenderer.SetVertexCount(positionsOfPoints.Length); // add this
lineRenderer.SetPositions(positionsOfPoints);
}
}
I haven't tested this yet but it seems correct. :)
It semi works its only rendering the line Renderer from point 1 to point 2 but not to the other points.
And i am getting an Error. LineRenderer.SetPosition index out of bounds
Updated my answer. check the second last line of code (Line 24).
Answer by Firedan1176 · Aug 07, 2016 at 01:58 AM
You're not using your iteration index in your for loop. Change points[0].position
to points[i].position
.
that still does not work its now 0,0,0 to the last point.
nope what is field anyway? i'm still a rookie in c#