summaryrefslogtreecommitdiff
path: root/Pathfinding Visualizer/Assets/Scripts
diff options
context:
space:
mode:
authordereelatwit <54997325+dereelatwit@users.noreply.github.com>2023-07-24 21:33:01 -0400
committerdereelatwit <54997325+dereelatwit@users.noreply.github.com>2023-07-24 21:33:01 -0400
commite7b56ebf9d25978b3aac4a0631d4083fdb2441d1 (patch)
treea3954af1ba37f3cbf190b07cbb45fdb571a90985 /Pathfinding Visualizer/Assets/Scripts
parent09852a99914c76d885de5ee209ba7aa06f6ffb15 (diff)
Implemented mouse to line drawing
Diffstat (limited to 'Pathfinding Visualizer/Assets/Scripts')
-rw-r--r--Pathfinding Visualizer/Assets/Scripts/DrawLine.cs61
-rw-r--r--Pathfinding Visualizer/Assets/Scripts/DrawLine.cs.meta11
2 files changed, 72 insertions, 0 deletions
diff --git a/Pathfinding Visualizer/Assets/Scripts/DrawLine.cs b/Pathfinding Visualizer/Assets/Scripts/DrawLine.cs
new file mode 100644
index 0000000..e3b2e47
--- /dev/null
+++ b/Pathfinding Visualizer/Assets/Scripts/DrawLine.cs
@@ -0,0 +1,61 @@
+using System.Collections;
+using System.Collections.Generic;
+using UnityEngine;
+
+public class DrawLine : MonoBehaviour
+{
+
+ public GameObject linePrefab;
+ public GameObject currentLine;
+
+ public LineRenderer lineRenderer;
+ public EdgeCollider2D edgeCollider;
+ public List<Vector2> fingerPositions;
+
+
+
+ // Start is called before the first frame update
+ void Start()
+ {
+
+ }
+
+ // Update is called once per frame
+ void Update()
+ {
+ if (Input.GetMouseButtonDown(0))
+ {
+ CreateLine();
+ }
+
+ if (Input.GetMouseButton(0))
+ {
+ Vector2 tempFingerPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
+ if (Vector2.Distance(tempFingerPos, fingerPositions[fingerPositions.Count - 1]) > .1f)
+ {
+ UpdateLine(tempFingerPos);
+ }
+ }
+ }
+
+ void CreateLine()
+ {
+ currentLine = Instantiate(linePrefab, Vector3.zero, Quaternion.identity);
+ lineRenderer = currentLine.GetComponent<LineRenderer>();
+ edgeCollider = currentLine.GetComponent<EdgeCollider2D>();
+ 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();
+ }
+}
diff --git a/Pathfinding Visualizer/Assets/Scripts/DrawLine.cs.meta b/Pathfinding Visualizer/Assets/Scripts/DrawLine.cs.meta
new file mode 100644
index 0000000..7a8befc
--- /dev/null
+++ b/Pathfinding Visualizer/Assets/Scripts/DrawLine.cs.meta
@@ -0,0 +1,11 @@
+fileFormatVersion: 2
+guid: c528828d28cb9a640b6504b8a7d14007
+MonoImporter:
+ externalObjects: {}
+ serializedVersion: 2
+ defaultReferences: []
+ executionOrder: 0
+ icon: {instanceID: 0}
+ userData:
+ assetBundleName:
+ assetBundleVariant: