- Home /
Collision mesh won't form until mesh is moved?
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.
Answer by firestorm713q · Apr 19, 2013 at 12:30 AM
A dirty fix is to just move it up a unit, then back down, but I'd rather just have it make the object and go...if someone provides a better solution, I'll unmark this answer, but for now, this is my solution...
Answer by sparkzbarca · Apr 19, 2013 at 03:01 AM
how do you know the collision mesh isn't formed.
My guess is it is, most of the time though objects without rigidbodys are "asleep" that is why they don't collide right away.
If its because a collision doesnt happen at start do collider.awake() maybe or add a rigidbody.
So the quoted script is only where the meshes are created and rendered. If you look through the linked marching squares script, there is a rigidbody attached, so that's not the issue. Also, collider doesn't have an awake function that I can call, as far as I can tell.
Your answer
Follow this Question
Related Questions
Colliders not Working with Tilemap Generation! 0 Answers
Custom collider for procedurally generated tiles 1 Answer
I want this script to update my mesh collider 0 Answers
Where do I add code to do procedural generation of environments? 1 Answer
How to Clamp A RigidBody to the Ground/ Moving a RigidBody Along the Ground 0 Answers