Method for checking colliding lines isn't working as intended

Hi everyone, i’m very new to unity and trying to implement the pen and paper game sprouts for a project. So far, I have managed to draw lines with the mouse and connect dots, I added an edge collider to each of the lines but have found out that edge colliders cannot collide with eachother. So in order to circumvent this, I tried to make my own method, each line is a List(vector2) type called fingerPositions, every time I successfully draw a valid line, I add a new instance to my ListOfLists, which is a list(list(vector2)) type.
During the update method, I call my method which goes through each of the lines and each coordinate and compares it to each coordinate in the newly drawn line, if the coordinates are the same it means the lines are overlapping, well, in theory anyway. It seems like it occasionally works and detects the collision as intended, but the majority of the time it simply lets the lines overlap, i’m wondering if update() is potentially happening too fast and its skipping over the check. Here is my code below :

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class DrawLine : MonoBehaviour
{
public GameObject linePrefab;
public GameObject currentLine;

public PolygonCollider2D PolyCollider;
public LineRenderer lineRenderer;
public EdgeCollider2D edgeCollider;
public Rigidbody2D RB;
public List<Vector2> fingerPositions;
public bool DrawStatus;
public bool startedDrawing;
public bool EndedOnCircle;

public List<List<Vector2>> ListOfLists = new List<List<Vector2>>();
public List<Vector2> TempCloneList = new List<Vector2>();

// Start is called before the first frame update
void Start()
{
    

}

// Update is called once per frame
void Update()
{
    if (Input.GetMouseButtonDown(0) && DrawStatus == true)
    {
        CreateLine();
    }
    if(Input.GetMouseButtonUp(0))
    {
        

        if (EndedOnCircle == false && DrawStatus == true)
        {
            Debug.Log("Invalid Line");
            fingerPositions.Clear();
            Destroy(currentLine);
           
        }
        else if (EndedOnCircle == true && DrawStatus == true)
        {
            //foreach (Vector2 item in fingerPositions) { print(fingerPositions[item]); }
            //fingerPositions.ForEach();

            //for loop is to print out all the vector2 positions of the current line WORKING
            
           
               TempCloneList.Clear();
               

               for (int i = 0; i < fingerPositions.Count; i++) // Loop through List with for
               {
                   TempCloneList.Add(fingerPositions*);*

//print(fingerPositions*);*
}
/*
//if statement below calls the function to check if a line is colliding with another and destroys
//it if it is, otherwise it is valid and adds it the list of lists
*/
if (OwnLineCollisionDetector() == true)
{
Debug.Log(“Current line is colliding with another”);
Destroy(currentLine);
}
else {ListOfLists.Add(new List(TempCloneList)); }

fingerPositions.Clear();

/*
for (int w = 0; w<ListOfLists.Count; w++)
{
Debug.Log(ListOfLists[w][25]);
}
*/
}
DrawStatus = false;

}
if(Input.GetMouseButton(0) && DrawStatus == true)
{
Vector2 tempFingerPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
if (Vector2.Distance(tempFingerPos, fingerPositions[fingerPositions.Count - 1]) > .1f);
{
UpdateLine(tempFingerPos);

}

}

}
//we only want to call createlIne when user has touched the screen
void CreateLine() //function to create the line when the user touches the screen
{
currentLine = Instantiate(linePrefab, Vector3.zero, Quaternion.identity);
lineRenderer = currentLine.GetComponent();
edgeCollider = currentLine.GetComponent();
PolyCollider = currentLine.GetComponent();
RB = currentLine.GetComponent();
fingerPositions.Clear();
fingerPositions.Add(Camera.main.ScreenToWorldPoint(Input.mousePosition));
fingerPositions.Add(Camera.main.ScreenToWorldPoint(Input.mousePosition));
lineRenderer.SetPosition(0, fingerPositions[0]);
lineRenderer.SetPosition(1, fingerPositions[1]);
edgeCollider.points = fingerPositions.ToArray();

}
void UpdateLine(Vector2 newFingerPos)
{
fingerPositions.Add(newFingerPos);
lineRenderer.positionCount++;
lineRenderer.SetPosition(lineRenderer.positionCount - 1, newFingerPos);
edgeCollider.points = fingerPositions.ToArray();
}
bool OwnLineCollisionDetector()
{
if (ListOfLists != null)
{
if (ListOfLists.Count > 0)
{
for (int i = 0; i < ListOfLists.Count; i++) // loop through list of lists
{
for (int x = 0; x < ListOfLists*.Count; x++) // loop through each vector2 point in a list*
{
for (int y = 0; y < fingerPositions.Count; y++) // loop through each vector2 point in current fingerpositions list and compare them
{
if (fingerPositions[y] == ListOfLists*[x])*
{
Debug.Log(fingerPositions[y]);
Debug.Log(ListOfLists*[x]);*
return true;
}
//else { Debug.Log(fingerPositions[y] + " " + ListOfLists*[x]); }*
}
}
}
return false;
}
else { return false; }
}
return false;
}
void OnTriggerEnter(Collider other)
{
Debug.Log(“here enter”);
if (other.gameObject.name == “GameController”)
Destroy(currentLine);
}
void OnTriggerEnter2D(Collider2D other)
{
Debug.Log(“here enter”);
if (other.gameObject.name == “GameController”)
Destroy(currentLine);
}
void OnMouseOver()
{
Debug.Log(“here over”);
if (DrawStatus == true)
{
Debug.Log(“collision between lines”);
Destroy(currentLine);
}
}
}
This is attatched to a line game object, hope this is enough, thanks
Update: I realized that replacing the update method with repeat invoke method ended up making it more successful at detecting overlaps between the lines, I believe this is because each time update is called it takes my mouse position and gets its coordinate then uses the those to draw the line, the less coordinates there are the more likely lines will overlap, is there a better way of doing this though
[154046-untitled.png|154046]

Hey there,
first thanks for the picure, that helps to clarify things.

In general what you do looks ok and should work except for the “collision detection” which you do by comparing floating point values. Which is not good. There is a really high probability of having 2 points close to each other and there would be no collision detected. So this here if (fingerPositions[y] == ListOfLists*[x]) has to change.*
So what can you do?
First up: Cheap and dirty detection
Instead of comparing the positions, check if the distance is below a certain threshold you set. ((Point A - Point B).Magnitude < Threshold) => collision
Second: Actual collision detection of lines
For this you should look into 2d linear algebra. You can define 2 lines in space and calculate their exact collision point. ([this link here might help you.][1] - try to not just copy the code but understand the math behind it) Then instead of iterating over all points you iterate over all pairs of points, so e.g. one line would be the one connecting ListOfLists_[x] and ListOfLists*[x+1] as well as fingerPositions[y]and fingerPositions[y+1]
whats better? The first one might serve the purpose, though might still leave some space for not detected behaviour. (escpecially for low framerates when the distance between points gets larger - which might be an issue since i guess you want this to work on mobile) Second needs more performance though.
Hope this helps you find a way, let me know if something was unclear.
[1]: mathematics - How to detect 2D line on line collision? - Game Development Stack Exchange