Display face Object

Hello everyone,
I have a problem displaying the faces of a stone block. I want to set up a script so that every time I click on a face of my stone block, that face should appear on my RawImage, but I want the chosen face to appear only on my RawImage, not the other faces and objects around it.

using UnityEngine;

namespace MyNamespace
{
    public class CreateCanvasAndCameraOnSelectedFace : MonoBehaviour
    {
        public GameObject canvasPrefab1;
        public GameObject cameraPrefab1;
        public GameObject objectToDetect1;

        public GameObject canvasPrefab2;
        public GameObject cameraPrefab2;
        public GameObject objectToDetect2;

        public float distanceFromFace = 1f;

        GameObject cameraObject1; // D├ęclarer cameraObject1 en dehors des blocs if

        void Update()
        {
            if (Input.GetMouseButtonDown(0))
            {
                Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
                RaycastHit hitInfo;

                if (Physics.Raycast(ray, out hitInfo))
                {
                    GameObject objectHit = hitInfo.collider.gameObject;

                    if (objectHit == objectToDetect1)
                    {
                        GameObject canvasObject1 = Instantiate(canvasPrefab1, hitInfo.point, Quaternion.identity);
                        Vector3 cameraPosition1 = hitInfo.point + hitInfo.normal * distanceFromFace;
                        cameraObject1 = Instantiate(cameraPrefab1, cameraPosition1, Quaternion.identity); // Affecter cameraObject1
                        cameraObject1.transform.LookAt(hitInfo.point + hitInfo.normal);
                        cameraObject1.GetComponent<Camera>().cullingMask = 1 << objectToDetect1.layer; // Utiliser le masque de calque pour masquer les autres objets
                    }
                    else if (objectHit == objectToDetect2)
                    {
                        GameObject canvasObject2 = Instantiate(canvasPrefab2, hitInfo.point, Quaternion.identity);
                        Vector3 cameraPosition2 = hitInfo.point + hitInfo.normal * distanceFromFace;
                        GameObject cameraObject2 = Instantiate(cameraPrefab2, cameraPosition2, Quaternion.identity);
                        cameraObject2.transform.LookAt(hitInfo.point + hitInfo.normal);
                        cameraObject2.GetComponent<Camera>().cullingMask = 1 << objectToDetect2.layer; // Utiliser le masque de calque pour masquer les autres objets
                    }
                }
            }
        }
    }
}

I assume you mean when the camera is looking at the cube, you want the cube to fit within the area of the RawImage on the GUI?

Maybe you can determine some point away the camera should be placed using the scale of the cube

Vector3 cameraPosition1 = hitInfo.point + (hitInfo.normal * objectHit.transform.lossyScale.x* someFactor);

This assumes a uniform scale of the cube. You have to play around with it to figure out what someFactor is.


Hello,
Thank you for your responses. I want the camera to look at the face of the cube. I want the camera's rendering to be the face of my cube, and no other objects in my scene should appear in the rendering.

What I want to do is to display the two faces of my two objects and then retrieve all the vertices of triangles of my face and finally compare if the two faces overlap or not.