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]