How to get a face on given direction of a cube

For me to explain my problem in a easy way to understand, I have to explain the mechanic as a whole:

So, I’m developing a game, where the player can fit pieces with holes in other pieces of diferent sizes, and I’m releasing raycasts to know where the piece should go.
With that said, Here’s the problem:

How far the upper piece will fit on the bottom piece is being controled by a raycast that travels trough the lower piece.
I’m struggling basically with Vectors and Positions, my main logic consist in getting a point in the lower piece, and creating an extended point on a given direction (on that example, the extended point will go up), and I use Bounds.ClosestPoint.
Here’s the piece of code that is responsible for doing this controll:

    public static Vector3 GetExtendedPoint(Vector3 desiredDirection, RaycastHit hit, Collider targetCollider)
    {
        Vector3 extendedPoint = hit.point + desiredDirection * 1;
        Vector3 extendedPointDirection = targetCollider.transform.forward.normalized;
        extendedPoint = extendedPoint + extendedPointDirection * extendedPoint.y;

        Vector3 desiredPosition = targetCollider.bounds.ClosestPoint(extendedPoint);

        return desiredPosition;
    }

I know that the problem is on this line:

extendedPoint = extendedPoint + extendedPointDirection * extendedPoint.y;

But I can’t figure out a way to fix this, any help will be much appreciated.
Thanks in advice! :slight_smile:

Problem might be because of rotation.
I would do triggerenter instead of raycast take part you want to place create empty gameobject make it child of the part set transfrom to zero remove the part now you can set exact location you just placed when ontriggerenter set parts transfrom to empty gameobject transfrom.

I think I understand your idea, but I do raycasts because of the drag of the object, and I use raycasts to know to which piece I should fit in, if I don’t used raycats, the game would be too hard to play.

And Something I should add as information is that I only snap the moving piece before I do all the calculations of positions.

You could create empty gameobjects to mark the snapping points of each piece, then compare their position and angles and adjust the position of the piece so they match together when near. No need of expensive raycasts.

Since you have a RaycastHit from your raycast, you can get the surface normal where the ray has hit. Whether the ray has hit where you intended to however, is another matter.

I managed to fix it!
Basically, I got the normal that my raycast was pointing to, and did a Vector3.Scale() between the collider.bounds.size and the normal, this gave me what where the size of the direction that I was hitting, and subtracting this with the previous point that I already had returned me the exact point that I needed.

public static Vector3 GetExtendedPoint(Vector3 desiredDirection, RaycastHit hit, Collider targetCollider)
    {
        Vector3 extendedPoint = hit.point + (desiredDirection * 1) - (Vector3.Scale(targetCollider.bounds.size, hit.normal) / 2);
        Vector3 desiredPosition = targetCollider.bounds.ClosestPoint(extendedPoint);

        return desiredPosition;
    }