Answers for "LineRenderer End Width Bug"
http://answers.unity.com/questions/909428/linerenderer-end-width-bug.html
The latest answers for the question "LineRenderer End Width Bug"Answer by Lewwwer
http://answers.unity.com/answers/1216922/view.html
Hi all!
I know it's an old post but I met with this problem in my game and based on @NoseKills 's solution I made a function adding some extra points to a Vector3 array in order to display lines correctly.
Vector3[] solution(Vector3[] original)
{
Vector3[] res = new Vector3[original.Length * 3 - 2];
for (int i = 0; i < res.Length; i++)
{
if (i % 3 == 0)
{
res[i] = original[i / 3];
}
else if (i % 3 == 1)
{
res[i] = Vector3.Lerp(original[(i - 1) / 3], original[(i + 2) / 3], 0.0001f);
}
else if (i % 3 == 2)
{
res[i] = Vector3.Lerp(original[(i + 1) / 3], original[(i - 2) / 3], 0.0001f);
}
}
return res;
}
Basically it puts extra points before and after every positions, so the lines between them will be rendered correctly.
In my game I wanted to make a star of lines centered at the gameObject and pointing to each element of an array of GameObjects. I will post it here to show how to use this function:
using UnityEngine;
using System.Collections;
[RequireComponent(typeof(LineRenderer))]
public class StarLine : MonoBehaviour {
public GameObject[] targets;
private LineRenderer lr;
private Vector3 pos;
void Start ()
{
lr = GetComponent<LineRenderer>();
}
void Update ()
{
pos = transform.position;
Vector3[] points = new Vector3[targets.Length*2];
for (int i = 0; i < points.Length; i++)
{
if (i % 2 == 0)
{
points[i] = targets[i/2].transform.position;
}
else
{
points[i] = pos;
}
}
Vector3[] good = solution(points);
lr.SetVertexCount(good.Length);
lr.SetPositions(good);
}
Vector3[] solution(Vector3[] original)
{
Vector3[] res = new Vector3[original.Length * 3 - 2];
for (int i = 0; i < res.Length; i++)
{
if (i % 3 == 0)
{
res[i] = original[i / 3];
}
else if (i % 3 == 1)
{
res[i] = Vector3.Lerp(original[(i - 1) / 3], original[(i + 2) / 3], 0.0001f);
}
else if (i % 3 == 2)
{
res[i] = Vector3.Lerp(original[(i + 1) / 3], original[(i - 2) / 3], 0.0001f);
}
}
return res;
}
}
I hope it helps solving this problem in the future!Sat, 16 Jul 2016 13:47:55 GMTLewwwerAnswer by NoseKills
http://answers.unity.com/answers/909773/view.html
The linerenderer draws the line pretty much with quads and because it needs to make the flat end of each segment match with the start of next segment, it needs to tilt the quads/triangles that make the line, thus the segments are not always facing camera.
I've been able to get satisfying results by adding 2 nodes instead of one to each spot where the line changes direction. Not sure if that always works.
P.S. nice looking game :)Wed, 25 Feb 2015 14:13:08 GMTNoseKills