Here is what I need: Collision with constantly updating procedurally generated mesh.
Here is the scenario:
I have a few objects on the screen which are drawing procedural mesh trails using the custom script TimedTrailRenderer (so I can adjust color of the trail on the fly). When one of the objects intersect the trail of another object I would like to be able to call a series of events on that collision, change the color of the trail and perform any number of other things. I create the MeshCollider when the trail is instantiated.
My current problem is the MeshCollider.sharedMesh variable doesn’t seem to update to the newly created procedural trail mesh when i try to assign it. In fact the sharedMesh variable seems to only have the triangles that were created in the first frame the application was running.
I’ve tried writing to the deprecated MeshCollider.mesh attribute, as well as updating the MeshFilter.sharedMesh attribute. neither of these worked.
After creating the new mesh the following is called in the TimedTrialRenderer script.
Mesh mesh = (o.GetComponent(typeof(MeshFilter)) as MeshFilter).mesh;
mesh.Clear();
mesh.vertices = newVertices;
mesh.colors = newColors;
mesh.uv = newUV;
mesh.triangles = newTriangles;
(o.GetComponent(typeof(MeshCollider)) as MeshCollider).sharedMesh = mesh;
If, while the application is running, I select the Trail and select the desired procedural mesh from the mesh list, the collision will update to the new model, but obviously will quickly get out of sink as the trail continues to render while the now assigned collision mesh remains exactly on the frame it was when i selected it from the pull down. If i try to select the same mesh from the pull down a second time, it will not update to the new trail.
My current assumption is that the sharedMesh attribute is stored once and is read-only so it can’t be accessed/changed again at runtime.
Does anyone have a recommendation on how to solve this issue?