Hi,
I’m creating a game where the player drags 2d sprites over a grid. However, I am struggling to make a system where when the player drags a sprite over a grid square and lets go, it snaps to the grid.
As well as this, I was wondering if there was any way to detect what sprite was in the grid square and trigger functions accordingly?
It would be very much appreciated if you could provide code, as I am new to C#.
Use a mouse ray so determine the coordinates of the mouse when clicked. If you have a map data structure set up (if you don’t, you really should) you can set up a plane collider (A plane mesh with a box collider) so the ray actually has something to hit, and you would use that hit coordinates to look up in your map,
Or, less preferred, though may be better if you have irregular shapes, you can use put a collider in each of your sprites to directly detect whether the sprite that was clicked.
Once you have that set up the behaviours are pretty straightforward