[SOLVED] Place object on top of surface without intersection (or gaps)

(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 :slight_smile:

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?

Hello @Dude_Loe ,

RaycastHit has two useful propreties that you can use to achieve your goal: RaycastHit.point and RaycastHit.normal. The first proprety gives you the point of impact in World Space on the mesh and the second gives a vector perpendicular to the surface hit. You can use both of these propreties the following way:

  1. You get the RaycastHit.point

  2. You add the RaycastHit.normal multiplied by some distance

Note that the distance here is the distance between the surface of the object hit and the center of the object you want to place. I think an easy way to find the distance needed to place the object on the surface would involve using Collider.ClosestPoint(Vector3 position). Here is some sloppy code that might work for you:

const float distanceFromSurface = 0.1f;
    public Vector3 GetPositionOnSurface(RaycastHit hitInfo, GameObject objectToPlace) 
    {
        Collider objectToPlaceCollider = objectToPlace.GetComponent<Collider>();

        // Distance between the center of the object you want to place and the point its surface
        float distFromCenter = Vector3.Distance(objectToPlaceCollider.ClosestPoint(hitInfo.point), objectToPlace.transform.position);

        // Total Distance between the center of the object and the surface of the object you want to place your object on
        float totalDistance = distanceFromSurface + distFromCenter; 
        return hitInfo.point + hitInfo.normal * totalDistance;
    }

I hope my explaination was clear.