Prevent object from being instantiated inside other object

Hi
Im trying to instantiate a cube (for now its a cube, later also more complex meshes) where a raycast hits an object. However, the cube should not be created half inside the object, what is the case if i instantiate it at the raycast hitpoint. Is there a way to calculate how to shift the cube to have it created outside the hit object? Another problem is, that im constantly updating the cubes position to the new raycast hitpoint as long as a key is held down, so even colliders wond do it…
god, i hope someone understands what im trying to say… and has an idea how to solve that problem.
cheers

Try modifying your cube prefab so that the pivot point ‘centre’ is actually a little outside the cube that you see. Then, when you instantiate the cube, rotate it towards the normal of the raycastHit so that it is always on the surface of the hit object.

Also, if you don’t want the raycasts to hit the instantiated cube, just use a layermask and put the cube on a layer that doesn’t get hit by the ray.