How to know if a point of a line touch or lie in a perimeter
Hello,
I have two lineRenderers (and therefore, two Vector3 arrays)
Is there an easy way to check if a line collides with another? (without mathematical calculation but with a Unity's method, if possible).
In other words, does a point of a line touch or lie in a perimeter?
Thank you.
Answer by xxmariofer · Dec 15, 2020 at 11:12 AM
unity has a class called edge collider, it is basically the same as the line renderer but instead of creating "sprites" it creates colliders from point A to B, you can add the line renderer points to the edge collider and detect collisions then
https://docs.unity3d.com/es/530/Manual/class-EdgeCollider2D.html
Thank you for your answer. I'm going to try this. Do you think I can create an edgeCollider2D and catch a collision like that?
//Create collider
EdgeCollider2D edgeCollider2d = new GameObject().AddComponent<EdgeCollider2D>();
Vector2[] edgePoints = new Vector2[l.positionCount];
Vector3[] linePoints = new Vector3[l.positionCount];
l.GetPositions(linePoints); //fill linePoints with the LineRenderer's points
for (int i = 0; i < l.positionCount; i++) { edgePoints[i] = (Vector2)linePoints[i]; }
edgeCollider2d.points = edgePoints;
//Test to know if a lineRenderer collides another
Collider2D[] colliders = new Collider2D[9];
ContactFilter2D contactFilter = new ContactFilter2D(); contactFilter.NoFilter();
edgeCollider2d.OverlapCollider(contactFilter, colliders);
if (colliders.Length > 0) hasCollision = false;
looks right to me, but remember to untick the UseWorldSpace from the linerenderer that comes ticked from default
Finally, to try to stick to the LineRenderer, I valued the position and the rotation of the EdgeCollider2D with those of the LineRenderer. We get the following code:
EdgeCollider2D edgeCollider2d = new GameObject("collider_" + color).AddComponent<EdgeCollider2D>();
Vector2[] edgePoints = new Vector2[l.positionCount];
Vector3[] linePoints = new Vector3[l.positionCount];
l.GetPositions(linePoints); //peupler le tableau linePoints avec les vertex du LineRenderer
for (int i = 0; i < l.positionCount; i++) { edgePoints[i] = (Vector2)linePoints[i]; }
edgeCollider2d.points = edgePoints;
edgeCollider2d.transform.position = l.transform.position;
edgeCollider2d.transform.rotation = l.transform.rotation;
Problem, the EdgeCollider2D does not follow the LineRenderer because it exists on a 2D plane and the LineRenderer on a 3D plane.
For example, the LineRenderer consisting of two points ((0,0,4), (5,0,4)) gives an EdgeCollider2D of ((0,0), (5,0)).
Do I really have to use an EdgeCollider2D?
Thought it was a 2d game, then you should use something different take a look into this suggestion
https://forum.unity.com/threads/how-to-add-collider-to-a-line-renderer.505307/#post-4417348
Thank you very much for your answer. $$anonymous$$esh collider is a good solution!
Your answer
![](https://koobas.hobune.stream/wayback/20220613021649im_/https://answers.unity.com/themes/thub/images/avi.jpg)