- Home /
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:
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:
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
}
}
does this also happen if you just use your linerenderer to draw a line where you just set the locations manually and nothing happens to it code-wise?
No if I draw the points manually the laser widths are fine and remain constant, but drawing them manually defeats the purpose behind the mechanics I intend for this game. :/
Answer by Lewwwer · Jul 16, 2016 at 01:47 PM
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!
Answer by NoseKills · Feb 25, 2015 at 02:13 PM
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 :)
Interesting. So every time the laser "bounces" you add a very small new segment by adding a new node very close to the raycast hit.point? And thank you :)
Yeah that's it. I can't remember my findings exactly anymore, but maybe even just adding 2 nodes at the same spot might do it.
I was doing this for a 2D match 3 game a la Jelly Splash so my case was a bit simpler, but I'd believe the same solution should work
Your answer
Follow this Question
Related Questions
Laser Render Reflect Angle incorrect 1 Answer
Create a 2D line reflection (laser trace) 1 Answer
LineRenderer (Laser Beam) is not following the ray it's going on the wrong direction when reflecting 1 Answer
Laser LineRenderer Tapers after first Reflect 0 Answers
How to make a mirror in 3.x? 0 Answers