Adjusting EdgeCollider2D for LineRenderer

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;
       }

I’m having the exact same problem. Does someone have an idea to solve this?

To clarify something first; all vertex in any Collider2D are in the local-space of the Rigidbody2D they are attached to, not world-space.

For example, given a Rigidbody2D with no rotation and positioned at (10,20) with an attached EdgeCollider2D, a vertex on the EdgeCollider2D set to (0,0) will be at world-space (10,20). Likewise a vertex set to (50,50) will be at world-space (50+10,50+20).

If you don’t add a Rigidbody2D then the Collider2D is added to the hidden static body that has no rotation and is positioned at (0,0). This means that its local-space and world-space are identical.

Your above example would only work is the EdgeCollider2D is not attached to a Rigidbody2D or if the Rigidbody2D it was attached to had no rotation and was positioned at (0,0).

Assuming the above is all good, check that the Camera “Projection” property is “Orthographic” otherwise this might be perspective rendering throwing the alignment off. If you are using “Perspective” then you need to be aware that Z will affect the results, particularly when using “Camera.ScreenToWorldPoint”. You can set the Z to zero or fixed at the Collider/LineRenderer depth.

Minor note: I would store the camera you want to use and do not call “Camera.main” as it is expensive, especially when you call it lots of times. Also, you seem to have some redundant code at the start of your example related to “screenPos)”.