# Unable to make a working building system.

Hi, I’m making a 3D game with a building system, it sort of works, the only issue is that the placed objects clip through walls, floors, etc. I have 2 scripts, one for the raycast and object preview. And a second script that places a clone of the preview in the exact location. I’ve tried everything, adding value, snapping, etc. But it just makes another strange clipping bug. Can somebody please tell me how to fix this bug?

Here is the code:

``````void Update()
{
Vector3 mouse = Input.mousePosition;
Ray casepoint = Camera.main.ScreenPointToRay(mouse);
RaycastHit hit;

if (Physics.Raycast(casepoint, out hit, 10f))
{
transform.position = new Vector3(hit.point.x, hit.point.y + offset, hit.point.z);
}
if (Input.GetKeyDown(KeyCode.R))
{
transform.Rotate(0, 90, 0);
}

}
``````

I think I finally understood what you’re trying to accomplish.

First, we need to know the “direction” the clicked surface is facing. We can know that thanks to the RayHit Normal property.
This will return a Vector3 where one of its values will (usually) be 1 or -1 and the others 0.

Now that we know the “up” axis of the surface, we know the direction we should apply the offset towards, so we just have to multiply the Normal (Vector3) by the offset (= half of the object’s height).

Like so:

``````Vector3 mouse = Input.mousePosition;
Ray casepoint = Camera.main.ScreenPointToRay(mouse);

RaycastHit hit;

float offset = transform.localScale.y / 2;   // Move this one outside to calculate it just once

if (Physics.Raycast(casepoint, out hit, Mathf.Infinity))
{
Vector3 normalOffset = hit.normal * offset;
transform.position = normalOffset + new Vector3(hit.point.x, hit.point.y, hit.point.z);
}
``````

Note that the offset might not be necessary if you set the pivot at the base of the models before importing them to Unity from your 3D package BUT you’ll probably still need to orient them using the normal value.