How to limit drag

I’m trying to drag a go but would like to limit it to x or y axis, limit distance and maybe add some easing. Essentially something like a preview for a match 3 type of game, where you can press an item and start dragging it, but you can only move it to horizontally or vertically and only a certain direction. When the range is exceeded, the item animates back.

I started by using in.cursor and was going to try an limit the position. But now I’m thinking that that’s probably not the best approach.

So instead I’m thinking of doing this…
when a draggable item is clicked, two rectangles are added (with factory). When the drag range exceeds the box, the drag is aborted. The dragged item then gets a message from the cursor based on the interaction with the cross

image

Is this a reasonable way to approach this, or is there a better way to achieve this?

I think you will get continuous updates when something is dragged and I would use that information when deciding if something has been dragged too far, at which point I’d cancel the drag operation and return the game object.

The idea with the two rectangles sound a bit more complicated to me, but if that works then great!

1 Like

The easiest way is probably to do it with math and a lil bit of code.
Making it go only vertical or only horizontal is just an if statement. Aborting could be done when the cursor is too far away from the initial position. Easing can be done by keeping an internal goal position and interpolating the sprites / gameobjects position towards that. It’s really a matter of just breaking down the problem into smaller more manageable pieces. Hope that helped :smiley:

2 Likes