Drawing a WireFrame Cube Where your mouse is pointing.

(I will admit that is likely not the best title…)

My question is, what is the simplest way to draw a WireFrame cube, 1x1x1, where the player’s mouse is pointing, at whole number increments, preferably with a range limit?

I am currently building a project that uses 1x blocks to build floors and ceilings, walls are usually Objects. I have been using RayCastHits to determine where to place the blocks, but have encountered the fact that sometimes the block goes elsewhere, due to some oddities with Raycasts and Terrain… So, I though of using a Wireframe Block to determine where the block will actually go.

.:EDIT:.
Because I was asked, here is my current code for dealing with the blocks:

`
public class BlockMech : MonoBehaviour {

LayerMask blockLayer = 1;
float range = 20;
RaycastHit hit;
public int selectedBlockID = 0;
public GameObject BlockPre;
GameObject block;
public Texture2D crossHair;
public BuildMode Mode = BuildMode.Block;
public GameObject DeskObject;
GameObject placeObject;

public enum BuildMode
{
    Block
}

void Start() {
    BlockPre.name = "BlockPre";
    BlockPre.SetActive(false);
    Globals.InitBlocks();
    
}
// Update is called once per frame
void Update () {
    if (Input.GetMouseButtonDown(0))
        Build();
    if (Input.GetMouseButtonDown(1))
        Erase();
    
    
   
}

private void Erase()
{

    if (HitBlock())
    {
        Debug.DrawLine(transform.position, hit.point, Color.red, 10000);
        if (Mode == BuildMode.Block)
        {
            if (hit.transform.gameObject.tag == "Block")
            {
                Destroy(hit.transform.gameObject);
            }
        }
        
    }
}

private bool HitBlock()
{
    bool cast = Physics.Raycast(transform.position, transform.forward, out hit, range, 1);
    
    if (hit.point != null)
    {
        Debug.Log(hit.transform.gameObject.name); //What did it hit...
    } 
    return cast;
}

private void Build()
{
    
    if (HitBlock())
    {
        
        Debug.DrawLine(transform.position, hit.point, Color.cyan, 10000);
        
        if (Mode == BuildMode.Block)
        {
            if (hit.transform.gameObject.name.Contains("Block") || hit.transform.gameObject.name.Contains("Terrain"))
            {

               
                Vector3 positionRaw = hit.transform.position + hit.normal;
                if (hit.transform.gameObject.name.Contains("Terrain"))
                {
                    positionRaw = hit.point;// +hit.normal;
                }
                Vector3 intPosition = new Vector3(Mathf.Floor(positionRaw.x), Mathf.Floor(positionRaw.y), Mathf.Floor(positionRaw.z));
                if (intPosition.x == 0 && intPosition.z == 0)
                {
                    return;
                }
                GameObject[] blocksinScene = (GameObject[])GameObject.FindGameObjectsWithTag("Block");
                foreach (GameObject obj in blocksinScene)
                {
                    if (obj.transform.position == intPosition)
                    {
                        return;
                    }
                }
                if (intPosition == transform.position)
                {
                    return;
                }
                Block useBlock = Globals.BlockMasterList[selectedBlockID];
                block = (GameObject)GameObject.Instantiate(BlockPre);
                block.SetActive(true);
                block.name = useBlock._blockId.ToString() + " - Block";
                MeshRenderer mr = block.GetComponent<MeshRenderer>();
                mr.material = useBlock._blockMaterials;
                if (useBlock._blockMaterials == null)
                { Debug.Log("Nothing Assigned..."); }
                block.tag = "Block";
                CubeAtlasUVManager cubeman = block.GetComponent<CubeAtlasUVManager>();
                cubeman.rowCount = useBlock.RowCount;
                cubeman.columnCount = useBlock.ColumnCount;
                cubeman.front = useBlock.UVPositions[0];
                cubeman.right = useBlock.UVPositions[1];
                cubeman.left = useBlock.UVPositions[2];
                cubeman.back = useBlock.UVPositions[3];
                cubeman.top = useBlock.UVPositions[4];
                cubeman.bottom = useBlock.UVPositions[5];
                cubeman.updateMesh();

                block.transform.position = intPosition;
            }
        }

    }
}

}

`

Anything else Needed?

.:Edit 2:.

An example of what I mean would be the WireFrame Block in StarMade’s Build Mode.

15845-blockexample.png

Take the cast vector from the raycast and instantiate your test prefab at that point.

Try something like this:

public GameObject cubeClone;

void SomeFunction(){
    Instantiate(cubeClone, cast, transform.rotation);
}