weird problem with hit/ raycast.

using UnityEngine;
using System.Collections;

public class ConstructionSpacestation : MonoBehaviour
{
    public GameObject Builder;
    private Transform actualBlock;
    
    void Update()
    {
        
        if (Input.GetMouseButtonDown(0))
        {
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            RaycastHit hit;
            if (Physics.Raycast(ray, out hit, 2000) )
            {
                
                Transform actualBlock = hit.transform;
                Debug.Log(actualBlock);

                actualBlock.position = hit.point + hit.normal * 0.5f;
                actualBlock.transform.position = new Vector3(
                    Mathf.Round(actualBlock.position.x),
                    Mathf.Round(actualBlock.position.y),
                    Mathf.Round(actualBlock.position.z)
                );

                GameObject Placement = Instantiate(Builder, actualBlock.position, Quaternion.identity) as GameObject;
                                   
            }
            else
            {
                Debug.Log("nothing");
            }
        }
    }
}

I hacked together this script to instantiate a cube right next to another. But it is spitting them out at odd places, and also deleating cubes from behind.
Can you explain what Is going on? is it the hit.point that is screwing up?!

When you say

Transform actualBlock = hit.transform;

You am making the block that you are pointing at the actualBlock.

Then you say

actualBlock.position = hit.point + hit.normal * 0.5f;
                actualBlock.transform.position = new Vector3(
                    Mathf.Round(actualBlock.position.x),
                    Mathf.Round(actualBlock.position.y),
                    Mathf.Round(actualBlock.position.z)
                );

Which moves the original block you are pointing at before you ever Instantiated.

I think what you want is to create a new Vectore based on the blocks position. like

Vector3 newBlockPos = hit.point…bla bla bla.

yea you don’t want to use hit.point because im guessing you want a kind of minecraft thing.

the thing is hit.point is actually where on the side you hit. assuming your using the default cube

also alot of what your using is implicit in the code. like you dont need a distance for the raycast or a quanternion.identity. you also dont need to round really.

if(physics.Raycast(ray,out hit)
{
   instantiate(Builder,hit.transform.position + hit.normal)
}