Hello,
I am creating an application that has to do with digital twin inspection. I am working with a ZED camera to get spatial mapping data saved to an .obj file. I am then instantiating a GameObject at runtime using this file in a new scene. I want the user to be able to click on an area of interest on the GameObject and get precise feedback on where the mesh was clicked (in order to add labeling and other features down the line). I have been trying to add this via a MeshCollider on the GameObject and adding raycasting from the mouse but have not been able to get it working properly.
I have the following attached to an empty game object in my scene
public class DigitalTwinSceneManager : MonoBehaviour
{
public GameObject _scan;
void Start()
{
_scan = (GameObject)Instantiate(Resources.Load("ZEDMesh"));
MeshCollider mc = _scan.AddComponent<MeshCollider>();
MeshFilter mf = _scan.AddComponent<MeshFilter>();
_scan.AddComponent<MeshRenderer>();
mc.convex = true;
mc.sharedMesh = mf.mesh;
}
}
and this is attached to my Main Camera in scene
public class RayCast : MonoBehaviour
{
Camera camera;
void Start()
{
camera = Camera.main;
}
void Update()
{
//Draw Ray
Vector3 mousePos = Input.mousePosition;
mousePos.z = 100f;
mousePos = camera.ScreenToWorldPoint(mousePos);
Debug.DrawRay(transform.position, mousePos - transform.position, Color.blue);
if (Input.GetMouseButtonDown(0))
{
Ray ray = camera.ScreenPointToRay(mousePos);
RaycastHit hit;
if (Physics.Raycast(ray, out hit))
{
GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
cube.transform.position = hit.point;
Debug.Log("Something has been hit!");
Debug.Log(hit.transform.name);
}
}
}
}
I added a Plane to my scene for testing and this works for detecting hits on the plane, however it does not detect the hits on my instantiated object. I have tried to make the components added to my object exactly the same as the plane but am still unsuccessful.
If anyone could provide any advice or direction for accomplishing this goal it would be greatly appreciated. If there’s any extra info that may be needed let me know!
Thanks!


