I have the following scrip allowing me to spawn blocks in my scene when I do it on a standard size cube they stack just fine but if I go to any size other size then that they tend to not stack correctly. Most of the time they stack inside the one you are trying to spawn it on top or side of.
using UnityEngine;
using System.Collections;
public class CubeBuilder : MonoBehaviour
{
float range = Mathf.Infinity;
public GameObject currentBlock;
public Transform curBlock;
RaycastHit hit;
public GameObject piece;
public GameObject piece1;
private Transform child;
Vector3 hitPoint;
private Rect checkRect = new Rect(0,0,160,160);
public Ray ray;
private GameObject[] blocks;
void Start ()
{
blocks = GameObject.FindGameObjectsWithTag("WorldBlocks");
}
void Build ()
{
ray= Camera.main.ScreenPointToRay (Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast (ray,out hit, 1000))
{hitPoint = hit.transform.position + hit.normal;
Instantiate(currentBlock, hitPoint, curBlock.rotation);
}
}
void Erase ()
{
ray= Camera.main.ScreenPointToRay (Input.mousePosition);
RaycastHit hit;
if(blocks.Length> 50)
{
if (Physics.Raycast (ray, out hit, 1000))
{
piece1 = hit.transform.gameObject;
piece = piece1.GetComponent<ParticleHolder>().ruble;
hitPoint = hit.transform.position + hit.normal;
Instantiate(piece, hitPoint,curBlock.rotation);
Destroy(hit.transform.gameObject);
}
}
}
void Update ()
{
blocks = GameObject.FindGameObjectsWithTag("WorldBlocks");
if (!checkRect.Contains(Input.mousePosition))
{
if (Input.GetMouseButtonDown(0))
Build();
if (Input.GetMouseButtonDown(1))
Erase();
}
}
}