Hey,
in my 2D project I am drawing lines to connect objects and use collisions. For this, whenever I draw a line, I also need to adjust the EdgeCollider2D of this LineRenderer object.
However, while I get the LineRenderer where it should be using the useWorldSpace property, I can’t figure out how to do the same with the EdgeCollider2D. Because although I give the collider the same positions for start and end point as the line itself, the collider appears slightly to the right, the position where the line would appear if useWorldSpace were disabled.
How can I get the EdgeCollider2D points to be right where the line is?
So far it looks like this:
void OnMouseDrag () {
Vector2 screenPos = new Vector2();
Camera.main.ScreenToWorldPoint (screenPos);
line.GetComponent<LineRenderer>().SetPosition (0,
new Vector3 (transform.position.x + (GetComponent<SpriteRenderer>().bounds.size.x)/2,
transform.position.y,
transform.position.z));
line.GetComponent<LineRenderer>().SetPosition (1, Camera.main.ScreenToWorldPoint(Input.mousePosition)+Vector3.forward*10);
tempEdges = line.GetComponent<EdgeCollider2D> ().points;
tempEdges [0] = new Vector2 (this.transform.position.x, this.transform.position.y);
tempEdges [1] = new Vector2 (
(Camera.main.ScreenToWorldPoint (Input.mousePosition) + Vector3.forward * 10).x,
(Camera.main.ScreenToWorldPoint (Input.mousePosition) + Vector3.forward * 10).y);
line.GetComponent<EdgeCollider2D> ().points = tempEdges;
}