Help - Line Renderer Broken bug.
Hello every one, i’m using Unity 's Line Renderer to draw a line. But it have some bug, when i try to move cursor near last point of line, it will start broken.
If anyone have exp with this please help me to fix this bug.
Below is my video for this bug:
And full project in here: GitHub - Brackeys/Line-Rider-Replica: This is the source code for a Line Rider Replica created during a Twitch Livestream.
Thanks you!
You need to check for sharp turn, here’s my solution
on your Line.cs
//Checking for sharp angle
public bool IsSharpAngle(Vector2 mousePos)
{
if (points!=null&&points.Count >= 2)
{
Vector2 lastDelta = points.Last() - points[points.Count - 1];
Vector2 newDelta = mousePos-points.Last();
return Vector2.Dot(newDelta, lastDelta) <= 0;
}
return false;
}
//return last point
public Vector2 LastPoint()
{
return points.Last();
}
Then finally in your LineCreator.cs
if (activeLine != null)
{
//check sharp angle
Vector2 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
if(activeLine.IsSharpAngle(mousePos))
{
Line prevLine = activeLine;
GameObject lineGO = Instantiate(linePrefab);
activeLine = lineGO.GetComponent<Line>();
activeLine.UpdateLine(prevLine.LastPoint());
}
activeLine.UpdateLine(mousePos);
}
2 Likes
yeah that’s pretty much it! good luck and happy coding!