Hi, I have a project that is generating meshes dynamically from an online api providing 2d polygons (I project these into world coords)
I appear to have the mesh configured correctly (it does render correctly as shown in the image “mesh.png” below).
I create another game object as a child of the game object that has a mesh renderer on it and apply the mesh collider to it using the same mesh. Everything looks correct when I pause the editor (see collider.png below).
The issue comes with Physics.Raycast(transform.position, transform.forward, out RaycastHit hit, 10.0f), this has worked successfully for me with other objects dynamically created in the scene just not for dynamic poly’s. It never registers a “hit”, I have tried lifting the mesh collider game object above the camera too to see if it’s backfacing but no cigar. I’m at a loss where to take this from here, hoping for a helping hand. Can provide more detail but wanted to avoid a code dump here as a first pass but here is how the mesh generation is taking place (omitting vertices calculation etc. assuming this is correct as of images.)
// create a mesh from the points
var mesh = new Mesh();
mesh.vertices = vertices;
mesh.uv = uvs;
mesh.triangles = triangulator.Triangulate();
mesh.colors = colors;
mesh.RecalculateNormals();
mesh.RecalculateBounds();
mesh.Optimize();
// make a new game object, we will draw programatically
var assetGameObject = new GameObject("Asset");
// make the filter including the mesh we made
var filter = assetGameObject.AddComponent<MeshFilter>();
filter.mesh = mesh;
// the way we render the poly
var renderer = assetGameObject.AddComponent<MeshRenderer>();
renderer.material = _roadMaterial;
renderer.material.color = colour;
// then use the above mesh for the mesh collider so we can interact with it e.g. look at
var colliderGameObject = new GameObject("AssetCollider");
colliderGameObject.transform.parent = assetGameObject.transform;
// use the above mesh for the mesh collider so we can interact with it e.g. look at
var collider = colliderGameObject.AddComponent<MeshCollider>();
collider.sharedMesh = mesh;
collider.convex = false;
collider.enabled = true;
collider.isTrigger = false;
assetGameObject.transform.parent = _stage; // add to stage