I’m trying to use this(http://wiki.unity3d.com/index.php/MarchingSquares) script for a procedural 2d asteroid, and for whatever reason it creates the collision mesh, but won’t form it until after I’ve moved it. Doesn’t matter whether or not I move it in script or in the editor, but the collision mesh won’t form. I’m still trying to wrap my head around building meshes procedurally, but I can’t seem to figure out what I’m missing.
// render
// for the sake of simplicity we are rendering every frame.
// obviously you should only render when the data of the cells has changed
Mesh mesh, cmesh;
MarchSquares(out mesh, out cmesh, ref cells, 0.5f);
// update the render mesh
mf = (MeshFilter) Testg.GetComponent(typeof(MeshFilter));
mf.mesh.Clear();
mf.mesh.vertices = mesh.vertices;
mf.mesh.uv = mesh.uv;
mf.mesh.triangles = mesh.triangles;
mf.mesh.normals = mesh.normals;
Destroy(mesh);
// update the collision mesh
MeshFilter cmf = (MeshFilter) TestRenderg.GetComponent(typeof(MeshFilter));
cmf.mesh.Clear();
cmf.mesh.vertices = cmesh.vertices;
cmf.mesh.uv = cmesh.uv;
cmf.mesh.triangles = cmesh.triangles;
cmf.mesh.normals = cmesh.normals;
Destroy(cmesh);
mc = (MeshCollider) Testg.GetComponent(typeof(MeshCollider));
if (mc != null)
mc.sharedMesh = mf.mesh;
Vector3 temp = Testg.transform.position;
temp.y += 1;
Testg.transform.position = temp;
}
public void FixedUpdate() {
// render
// for the sake of simplicity we are rendering every frame.
// obviously you should only render when the data of the cells has changed
Mesh mesh, cmesh;
MarchSquares(out mesh, out cmesh, ref cells, 0.5f);
// update the render mesh
MeshFilter mf = (MeshFilter) Testg.GetComponent(typeof(MeshFilter));
mf.mesh.Clear();
mf.mesh.vertices = mesh.vertices;
mf.mesh.uv = mesh.uv;
mf.mesh.triangles = mesh.triangles;
mf.mesh.normals = mesh.normals;
Destroy(mesh);
// update the collision mesh
MeshFilter cmf = (MeshFilter) TestRenderg.GetComponent(typeof(MeshFilter));
cmf.mesh.Clear();
cmf.mesh.vertices = cmesh.vertices;
cmf.mesh.uv = cmesh.uv;
cmf.mesh.triangles = cmesh.triangles;
cmf.mesh.normals = cmesh.normals;
Destroy(cmesh);
MeshCollider mc = (MeshCollider) Testg.GetComponent(typeof(MeshCollider));
if (mc != null)
mc.sharedMesh = mf.mesh;
}
I have read about (but never used) a trick where you disable and then enable the collider after the mesh has been modified.
– AlucardJaymight also work.
– firestorm713q