Mouse click ON cube to attach another cube to it

I don’t know how to do this…
I want a cube where I can click on one side of it and attach a cube to that one side.

I have rigidbody, I have colliders… I even put colliders on every single side of the cube (they are contained within an empty game object so that I can reference each by name).

Please help …

Hi, @maw0041 !

This script should work for you, there is couple of implementations - currently it’s finding center of cube and take it’s rotation to attach accordingly, if you don’t need it then remove function FindSideCenter() with assigning of transform.rotation and uncomment t_SelectedObject.position = hit.point + hit.normal.normalized * f_CubeSize;

public Camera cam;
public LayerMask layer;

Transform t_SelectedObject;
Transform t_AttachToObject;
float f_CubeSize = 1f;

// Update is called once per frame
void Update()
{
    /* If we press right mouse button  */
    if (Input.GetMouseButtonDown(1))
    {
        RaycastHit hit;

        /* Draw ray from mouse position to check if we hit anything with certain layer */
        if(Physics.Raycast(cam.ScreenPointToRay(Input.mousePosition), out hit, layer))
        {
            /* If we hit the same object then we de-select it */
            if (t_SelectedObject != null && t_SelectedObject == hit.transform)
                t_SelectedObject = null;
            else
                t_SelectedObject = hit.transform;
        }
    }

    /* If we press left mouse button */
    if (Input.GetMouseButtonDown(0))
    {
        RaycastHit hit;

        /* Draw ray from mouse position to check if we hit anything with certain layer */
        if (Physics.Raycast(cam.ScreenPointToRay(Input.mousePosition), out hit, layer))
        {
            t_AttachToObject = hit.transform;

            if(t_SelectedObject != null)
            {
                /* Check if we do not hit the same object that we have selected */
                if(t_SelectedObject != hit.transform)
                {
                    /* Assign position with offset so the cube center won't be at click point
                     * Keep in mind that if you change your cube size you should change f_CubeSize as well */
                    //t_SelectedObject.position = hit.point + hit.normal.normalized * f_CubeSize;

                    /* If you need to Rotate it accordingly to object that you attach */
                    t_SelectedObject.rotation = hit.transform.rotation;

                    /* Function if you want attach cube to center of hitted object side */
                    t_SelectedObject.position = FindSideCenter(hit.point);

                }
            }
        }
    }
}

/* Function to find right side center and return it position to attach
 * If you don't need to attach it to center, then just remove this function */
Vector3 FindSideCenter(Vector3 hitPosition)
{
    Vector3[] sides = { t_AttachToObject.right, -t_AttachToObject.right, t_AttachToObject.forward, -t_AttachToObject.forward, t_AttachToObject.up, -t_AttachToObject.up };

    float minDistance = Vector3.Distance(t_AttachToObject.position + sides[0] * f_CubeSize, hitPosition);
    int sideIndex = 0;

    for(int i = 1; i < 6; i++)
    {
        float curDistance = Vector3.Distance(t_AttachToObject.position + sides <em>* f_CubeSize, hitPosition);</em>

if (curDistance < minDistance)
{
minDistance = curDistance;
sideIndex = i;
}
}

return t_AttachToObject.position + sides[sideIndex] * f_CubeSize;
}
Put this script on any object, assign camera and layer that you want to interact with. The script works this way - by pressing your right mouse you select cube that you will move. By pressing left mouse we select cube to which we will attach. If you double click right mouse on the same cube it will de-select it. It has no visualization, so you can easily add color changin and other effects. If anything unclear - ask.