(SOLUTION(Using answer from @MerryLeo )): The problem was all along me and my stupid way of writing code. It was all executed in the wrong order and I completely forgot that the closest point on the collider would change as I moved it to the position I wanted. This inevitably created an infinite loop of moving it to the desired place, the distance between the objects changing (moving it away again) and doing the same over and over.
What I did to solve this was simply putting a child object inside the object I wanted to place down, keeping the collider of the parent object, but removing the visual mesh. Then I simply used the closest point on the parent object collider, but only moved the child object (with the visual mesh) to the desired location.
This is probably not a very good solution but it worked for me
I’ve been struggling with this problem for quite some time and with my lack of searching skills I haven’t been able to find anything suitable for my case.
What I am trying to achieve is a script that lets me place objects just outside the surface of the object I am pointing the camera at.
The problem right now is that when I do this the object I am placing down is always placed inside the object I am looking at.
This obviously happens because when I set the position of the object I want to place down to the position of the RaycastHit, it uses the position of the origin. The origin of the object I want to place down is always in the middle of it, so it’s always inside the object I am looking at with about half of its mass.
How could I make it so no matter size, rotation and shape, the object I want to place down is just outside the surface of the object I am looking at?