Question by
unity_H99B3PONnyb9Rg · Sep 11, 2018 at 07:54 AM ·
rigidbody2dcollider2dlinerenderer
Collider is not working fine on Line Renderer...
Hi there, I am working on Brain Dots/Brain Blusters/ Brain it On like game... I am stuck with the collider of line created by Line Renderer. If I make Egde Collider then it goes crazy. It starts slipping through each surface and have some force that makes it move anywhere and collisions are also not as expected... And if I make Polygon collider then it works fine according to its physics. But polygon's behaviour is not required. The problem with polygon is shown in the picture...
Here are my scripts:
LineCreater.CS
public class LineCreator : MonoBehaviour {
public GameObject linePrefab;
public Vector3 startPos;
public Vector3 endPos;
public float lineWidth;
Line activeLine;
void Update()
{
if (Input.GetMouseButtonDown(0))
{
GameObject lineGO = Instantiate(linePrefab);
activeLine = lineGO.GetComponent<Line>();
}
if (Input.GetMouseButtonUp(0))
{
activeLine.addColliderToLine();
activeLine.gameObject.AddComponent<Rigidbody2D>();
activeLine = null;
}
if (activeLine != null)
{
Vector2 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
activeLine.UpdateLine(mousePos);
}
}
}
Line.cs
public class Line : MonoBehaviour {
public LineRenderer lineRenderer;
public PolygonCollider2D polycol;
public float lineWidth = 0.08f;
List<Vector2> points;
public void UpdateLine (Vector2 mousePos)
{
lineRenderer.startWidth = lineWidth;
lineRenderer.endWidth = lineWidth;
if (points == null)
{
points = new List<Vector2>();
SetPoint(mousePos);
return;
}
if (Vector2.Distance(points.Last(), mousePos) > .1f)
SetPoint(mousePos);
}
void SetPoint(Vector2 point)
{
points.Add(point);
lineRenderer.positionCount = points.Count;
lineRenderer.SetPosition(points.Count - 1, point);
}
public void addColliderToLine()
{
if (points.Count > 1)
polycol.points = points.ToArray();
this.gameObject.AddComponent<Rigidbody2D>();
}
}
untitled.png
(6.8 kB)
Comment