Problem with instantiating inside of object.

I have a problem where I am trying to make a cube sort of building game and when I add the cube of which I am trying to add, it spawns slightly where I want it, yet is mostly inside of the previous cube.

Wondering if anyone can help with this problem.

Here is a picture of what happens:

alt text

Here is the code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MouseManager : MonoBehaviour {

//	public Camera CamObject;
	public GameObject thePrefabToSpawn;


	// Use this for initialization
	void Start () {
		
	}
	
	// Update is called once per frame
	void Update () {
	
		if (Input.GetMouseButtonDown(0)) {

			Camera theCamera = Camera.main;
			//Or Camera.main
			//can be set to variable

			Ray ray = theCamera.ScreenPointToRay (Input.mousePosition);

			RaycastHit hitInfo;

			if (Physics.Raycast (ray, out hitInfo)) {
				Debug.Log ("Found Collision: " + hitInfo.collider.gameObject.name);


	
				//Add New Object



				Vector3 spawnSpot = hitInfo.collider.transform.position + hitInfo.normal;



				Instantiate(thePrefabToSpawn, spawnSpot, Quaternion.identity);
			}

		}

	}


}

Normals aren’t super-reliable for something like this. If you’re going to be stacking objects side by side cleanly in a pseudo-grid, then even just the floating point errors will start accumulating and creating bigger and bigger differences in position over time (assuming the normals are near-perfectly accurate to begin with). I would definitely use the normal, don’t get me wrong, but use it to get a general direction and then snap the new object to an exact offset of the old one based on that direction.

The simplest way here is likely to cache the six vector directions (up, down, forward, back, left, right). Then, when raycasting, find the Vector3.Angle() between the hit normal and all six of those vector directions, and use the vector that produces the smallest absolute angle difference. Offset the new position by that instead, and it’ll be a really clean.