Skip to content

Instantly share code, notes, and snippets.

@insthync
Last active July 30, 2019 16:50
Show Gist options
  • Save insthync/b725fc12e0c80f87f0f467f662210203 to your computer and use it in GitHub Desktop.
Save insthync/b725fc12e0c80f87f0f467f662210203 to your computer and use it in GitHub Desktop.
Line-link puzzle example
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TestCollider : MonoBehaviour
{
static bool isDragging;
static TestCollider lastDragNode;
static List<TestCollider> draggedNodes = new List<TestCollider>();
public byte nodeType = 0;
private void OnMouseDrag()
{
if (lastDragNode == null)
{
draggedNodes.Add(this);
lastDragNode = this;
}
}
private void OnMouseOver()
{
if (lastDragNode == null || lastDragNode == this)
return;
var tempLastDragNodeLayer = lastDragNode.gameObject.layer;
lastDragNode.gameObject.layer = 2; // Set layer to ignore raycast
var result = Physics2D.Linecast(lastDragNode.transform.position, transform.position);
if (lastDragNode.nodeType == nodeType &&
result.collider.gameObject == gameObject /* Check current node is not obstructed by other node types */)
{
lastDragNode.gameObject.layer = tempLastDragNodeLayer;
draggedNodes.Add(this);
lastDragNode = this;
}
else
{
lastDragNode.gameObject.layer = tempLastDragNodeLayer;
}
}
private void OnMouseUp()
{
if (draggedNodes.Count > 1)
{
// Collect score
int score = draggedNodes.Count * 10;
Debug.Log("Score += " + score + " nodeType = " + lastDragNode.nodeType);
// Clear all dragged nodes
for (int i = draggedNodes.Count - 1; i >= 0; --i)
{
Destroy(draggedNodes[i].gameObject);
}
}
draggedNodes.Clear();
lastDragNode = null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment