Drag and Drop Problem

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#.

Thank you very much in advance,

| detect what sprite was in the grid square

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

if(clickedSprite.Type = 1)
{
//do something
}
else if(clickedSprite.Type == 2)
{
//do soemthing else
}
else
{
//etc...
}