void OnMouseDown() { beingDragged = true; // Start dragging. touchOffset = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position; //offset between the cursor and the block centre lastSnap = transform.position; // this is obviously where we snapped the last time bounds = SlidingPuzzleExample.CalculateSlidingBounds(transform.position, transform.lossyScale); // create default bounds SlidingPuzzleExample.RegisterObstacle(transform, true); // marks this space as free in the matrix (or else we won't be able to return back here) }
///<summary>This is where the dragging logic takes places.</summary> void Drag() { // the destination is where the cursor points (minus the offset) clamped by the bounds Vector3 destination = ClampPosition(Camera.main.ScreenToWorldPoint(Input.mousePosition) - touchOffset); destination.z = transform.position.z; // now use that information to get the new bounds bounds = SlidingPuzzleExample.CalculateSlidingBounds(lastSnap, transform.lossyScale); // simulate a snap to the grid so we can get potentially new bounds in the next step lastSnap = ClampPosition(SlidingPuzzleExample.mainGrid.AlignVector3(destination, transform.lossyScale)); // move to the destination! transform.position = destination; }