Minecraft like building?

I have a building mechanism that allows you to place blocks, it’s all free and custom, though i want it to be in a grid, so when you place it, it’ll be exactly the same to the grid.

i didn’t try anything yet, but this is the script i’ve been using to place blocks:

using UnityEngine;

using System.Collections;

public class PlaceBlock : MonoBehaviour
{

    Ray ray;
    RaycastHit hit;
    public GameObject prefab;
    public GameObject prefab2;
    // Use this for initialization
    void Start()
    {




    }

    // Update is called once per frame
    void Update()
    {

        ray = Camera.main.ScreenPointToRay(Input.mousePosition);

        if (Physics.Raycast(ray, out hit))
        {
            if (Input.GetKeyDown(KeyCode.X))
            {
                GameObject obj = Instantiate(prefab, new Vector3(hit.point.x, hit.point.y + 0.5f, hit.point.z), Quaternion.identity) as GameObject;
            }
            if (Input.GetKeyDown(KeyCode.Z))
            {
                GameObject obj = Instantiate(prefab2, new Vector3(hit.point.x, hit.point.y + 0.5f, hit.point.z), Quaternion.identity) as GameObject;
            }
        }
    }

}

i have no idea how to acomplish it, i need help

this is how it looks so far

Hi,
You must round your x, y and z values for example:
GameObject obj = Instantiate(prefab2, new Vector3(Mathf.Round(hit.point.x - 0.5f) + 0.5f, Mathf.Round(hit.point.y - 0.5f) + 0.5f, Mathf.Round(hit.point.z - 0.5f) + 0.5f);

@MT369MT is right, you need to mathf.clamp where the raycasting is hitting the ground. The reason the blocks don’t stack is you need a “top collider” trigger on the top of the bricks to identify for the raycast that it is hitting the top of the brick. The game I am currently working on is similar to yours, I have got it working so that the bricks are stacked on top of each other and got the player able to rotate the bricks too.