Snap to Grid on Checkboard (2D)

Imagine there’s a checkboard (2D).

And I am dragging an object (from outside the checkboard), to drop it on the checkboard; I need to create a script that makes the object snaps on the grid properly.

For example, if the object (Sprite) looks like “T”, the T should be snapped exactly on the squares even if the player was dropping it between squares.

I am trying different methods, but nothing is working fine, this my the last attempt:

 using UnityEngine;
using System.Collections;



publicclassDrag:MonoBehaviour{
privateVector3screenPoint;
privateVector3offset;
GameObjectfollower;
GameObjectdraggedBlock;
GameObjectFilled;
GameObjectCenterPoint;
publicstringcenterpoint;
publicGameObjectoriginalpos;

voidStart(){
CenterPoint=GameObject.Find(centerpoint);
}

voidOnMouseDown(){

offset=gameObject.transform.position-Camera.main.ScreenToWorldPoint(newVector3(Input.mousePosition.x,Input.mousePosition.y,screenPoint.z));
CenterPoint.transform.position=originalpos.transform.position;
}

voidOnMouseDrag()
{
Vector3curScreenPoint=newVector3(Input.mousePosition.x,Input.mousePosition.y,screenPoint.z);
Vector3curPosition=Camera.main.ScreenToWorldPoint(curScreenPoint)+offset;
transform.position=curPosition;
}



voidOnMouseUp(){

if(GameObject.FindGameObjectsWithTag("filled").Length==4){

Filled=GameObject.FindGameObjectWithTag("filled");

CenterPoint.transform.position=newVector3(Filled.transform.position.x,Filled.transform.position.y,-4);

gameObject.transform.position=CenterPoint.transform.position;

Debug.Log("4filled");

}else{

gameObject.transform.position=originalpos.transform.position;
}

if(gameObject.transform.position.x>0 || gameObject.transform.position.x<0){gameObject.transform.position=newVector3(0,CenterPoint.transform.position.y,-4);
}}

}

It’s working fine if the object is “I” (3 squares) or 1-square, but if it’s something like L or T, it’s not being snapped right :(.

and any alternative solution is welcomed

is my request that unclear or it is very hard?

Do you need screenshots?

Your request is very unclear. You have not mentioned what your game is about or what you are trying to do in your game, so there is no context for what “i” and “T” shapes really mean or what their significance is. I can hazard a guess that blocks make up the shape you are trying to describe but this was after reading your question for the third time.

Your script is also tough to read with missing spaces and indentation.

We also have no idea of the scale you are working in. It would be very simple if your grid space was 1 unit of Unity’s coordinate system, that way setting your object to a “grid” is simply rounding off the position values of your squares.

If you’re trying to do a board game, rather than moving the pieces to fixed intervals, it may be better to create an empty object on the tiles that is the position on the piece.

Have the piece grab that empty’s location and move it to it.

Maybe throw in some random values to make the pieces move just off center and look more natural while you’re at it.

I have removed the extra spacing.

Animated Gif here, wait for few seconds to load:

You see there that there L piece is not getting snapped properly.

My “grid” is actually made of gameobjects, square sprites places next to each other.

Oh, ha!

I thought you were LITERALLY doing a checkerboard.

I can’t see. Do you use uGUI for those shapes? I think the issue is that shapes are aligned according to the pivot point. It means that the pivot point IS in the centre of the cell, but the pivot point itself is not aligned in centre of the shape’s cell. Hope you got my point.

The solution would be to set up the pivot point manually for each shape. Your shape consists of the cells. For example, L: 3 horizontal, 1 one up in the corner. So, you move the pivot point in the centre of any of these abstract cells. I am not sure if it will work, but you should try.

This is the gif link:

http://i.giphy.com/l4JyNiqH4Y67PYpYA.gif

can you see it?

Yes, I am trying to play with the pivot point in the Sprite editor.

And no, it’s not uGUI.

It is visibly a checkboard, but it’s of separate squares aligned next to each other. - what I mean that it’s not one big square (gameobject) with a grid texture applied on it.