// Update is called once per frame void Update() { if (Input.GetMouseButtonDown(0)) { draggableObject = DetectDraggables(); objectDraggedFrom = DetectRecievables(); if (draggableObject != null) { draggableObject.Select(); } dragged = false; } else if (Input.GetMouseButton(0)) { if (draggableObject != null) { mousePosition = Input.mousePosition; if (dragged) { draggedItemPreviewImage.transform.position = mousePosition; //recievableObject = DetectRecievables(); //if (recievableObject != null) //{ // recievableObject.OnObjectHoveringOver(selectedItem); //} } else if (Vector2.Distance(mouseDownPosition, mousePosition) > dragThreshold) // TODO: Optimize. { if (draggableMask == (draggableMask | (1 << selectedItem.gameObject.layer))) { dragged = true; draggedItemPreviewImage.sprite = draggableObject.OnStartDrag(); draggedItemPreviewImage.transform.position = mousePosition; draggedItemPreviewImage.gameObject.SetActive(true); } } } } else if (Input.GetMouseButtonUp(0)) { if (draggableObject != null) //something is selected. { draggedItemPreviewImage.gameObject.SetActive(false); recievableObject = DetectRecievables(); if (!dragged) { return; } if (recievableObject != null && objectDraggedFrom != null && recievableObject != objectDraggedFrom) //object is over something other than where it started from. could use better logic.. { draggableObject.OnDragDone(); recievableObject.OnObjectAdded(selectedItem); } else { draggableObject.OnCancelDrag(); } } } }