# How To Drag any Cube Using Mouse Click and Moved Only 1 Unit??

i m making 2D puzzle game (4 * 5) see the referance.

i want to drag any cube using mouse click moved left,right,up,down only 1 unit should be moved.

i m writing code for 1 unit move cube in my program when user click mouse

transform.position += Vector3.up * 0.1f;
transform.position += Vector3.down * 0.1f;
transform.position += Vector3.left * 0.1f;
transform.position += Vector3.right * 0.1f;

but i want to drag any cube for left,right,up,down moved(1 unit) .

how to drag any cube using mouse click left,right,up,down move 1 unit only.

Code:

void Update()
{
bool up;
bool down;
bool left;
bool right;
if (Input.GetMouseButtonDown(0))
{
if (up == false)
{
transform.position += Vector3.up * 0.1f;                    //only 1 unit move when drag any cube click in unity
down = true;
left = true;
right = true;
}
//else if (down == false)
//{
//    transform.position += Vector3.down * 0.1f;
//    up = true;
//    left = true;
//    right = true;
//}
//else if (left == false)
//{
//    transform.position += Vector3.left * 0.1f;
//    up = true;
//    down = true;
//    right = true;
//}
//else if (right == false)
//{
//    transform.position += Vector3.right * 0.1f;
//    up = true;
//    left = true;
//    down = true;
//}

}
}

Referance:

Best way to do this is define what a unit is, probably a float, and then when you click you do something like:

transform.position += (Vector3.up + unit);

Unity has the OnMouseDrag() MonoBehaviour method, but you will need to have a collider attached to cube GameObject for it to detect the drag event.