Hello all!
Ive spent hours trying to implement a simple 3d block placing system similar to minecraft, i know about voxel generation, and that this is an inefficient way to do this, but this is intended as a small side mechanic and im set on doing it this way, I have the following code which raycasts a preview block and moves its position accordingly, it works as intended on the ground, but when it hits another cube i just can get it to align to the normal of the hit cube, can one of you genius coders help a brother out?
if (buildModeOn)
{
RaycastHit hitInfo;
if (Physics.Raycast(shootingPoint.position, shootingPoint.forward, out hitInfo, 10, buildLayer))
{
if (hitInfo.transform.tag == "Cube")
{
canBuild = true;
Vector3 spawnPosition = new Vector3(Mathf.Round((hitInfo.point.x + hitInfo.normal.x) * 2) / 2, hitInfo.point.y + hitInfo.normal.y - 0.75f, Mathf.Round((hitInfo.point.z + hitInfo.normal.z) * 2) / 2);
currentTemplateBlock.transform.position = spawnPosition;
}
else
{
canBuild = true;
Vector3 spawnPosition = new Vector3(Mathf.Round((hitInfo.point.x + hitInfo.normal.x) * 2) / 2, hitInfo.point.y + hitInfo.normal.y - 0.75f, Mathf.Round((hitInfo.point.z + hitInfo.normal.z) * 2) / 2);
currentTemplateBlock.transform.position = spawnPosition;
}
}