LineRenderer End Width Bug

I have a script that renders a laser from the front of a game object and ‘bounces’ off of gameobjects with the tag “mirror”. You can see that in the image below:

41349-screen-01-marked.png

However something weird happens to the ending width of the laser after the first bounce. You can see in the image below, the ‘farthest’ out laser i.e. the last segment (marked as “4”) bounced ending width is fine. It is .075 in the beginning and .075 at the end. The other segments however taper down to 0 from .075, and it changes.

For example if I spawn a block in the lasers path on a different segment, that segment’s end width returns to .075. Image below:

41350-screen-02-marked.png

The segment marked “2” ending width now remains constant where segment “1” tapers. It seems the last segment to bounce retains it’s ending width, but the previously bounced segments do not.

Any Ideas?
Code Below:

void RenderLaser(){
         hitCount = 1;
         laserActive = true;
         teleActive = false;
         curPos = transform.position;
         curRot = transform.forward;
         laserLine.SetVertexCount(1);
         laserLine.SetPosition (0, transform.position);
         
         while (laserActive) {
             hitCount++;
             laserLine.SetVertexCount(hitCount);
             if (Physics.Raycast (curPos, curRot, out laserHit, range)) {
                 
                 //hitCount++
                 curPos = laserHit.point;
                 curRot = Vector3.Reflect(curRot, laserHit.normal);
                 laserLine.SetPosition(hitCount-1, laserHit.point);
                 if(laserHit.collider.tag != "Mirror"){
                     laserActive = false;
                     if(laserHit.collider.tag == "WinFace"){
                         Debug.Log("Teleport Activated.");
                         teleActive = true;
                     }
                 }
             } else {
                 //hitCount++
                 laserActive = false;
                 laserLine.SetPosition(hitCount-1, curPos+100*curRot);
             }
             if (hitCount > limit){
                 laserActive = false;
             }
             //END WHILE
         }
         
     }

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 *= original[i / 3];*

}
else if (i % 3 == 1)
{
res = Vector3.Lerp(original[(i - 1) / 3], original[(i + 2) / 3], 0.0001f);
}
else if (i % 3 == 2)
{
res = 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();
* }*

* 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 = targets[i/2].transform.position;
}
else
{
points = 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 = original[i / 3];
}
else if (i % 3 == 1)
{
res = Vector3.Lerp(original[(i - 1) / 3], original[(i + 2) / 3], 0.0001f);
}
else if (i % 3 == 2)
{
res = Vector3.Lerp(original[(i + 1) / 3], original[(i - 2) / 3], 0.0001f);
}
}
return res;
}

}

I hope it helps solving this problem in the future!

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 :slight_smile: