- Home /
Question by
jedaniels000 · May 11, 2020 at 08:24 AM ·
collisioncollision detectionmeshcollidermesh collider
Mesh Collider collision problem
I am generating a mesh surface for my player to walk on in chunks; for each chunk, the code below is run. My issue is that the player (also tested cube & sphere with and without rigidbody) goes right through the floor (also raycasts won't collide with the mesh). I have scoured the internet for a solution; OnTrigger is not enabled and the mesh is in the default layer. However, the part I really don't understand is if I pause the game and manually add a mesh collider to the floor, the objects will collide with the floor then. I've compared the two colliders (script generated and manually generated) and they appear identical. Any pointers would be greatly appreciated.
meshFilter = GetComponent<MeshFilter> ();
meshRenderer = GetComponent<MeshRenderer> ();
meshCollider = GetComponent<MeshCollider> ();
// if there's not a mesh filter, add one
if (meshFilter == null) {
meshFilter = gameObject.AddComponent<MeshFilter> ();
}
// if there's not a mesh renderer, add one
if (meshRenderer == null) {
meshRenderer = gameObject.AddComponent<MeshRenderer> ();
}
// if there's not a mesh collider and we want one, add one
if (meshCollider == null && colliderOn) {
meshCollider = gameObject.AddComponent<MeshCollider> ();
}
// If there's a mesh collider and we don't want one, destroy it
if (meshCollider != null && !colliderOn) {
DestroyImmediate (meshCollider);
}
// Get the mesh
mesh = GetComponent<Mesh>();
// If there's no mesh, add a mesh
if (mesh == null) {
mesh = new Mesh ();
meshFilter.sharedMesh = mesh;
}
if (colliderOn) {
if (meshCollider.sharedMesh == null) {
meshCollider.sharedMesh = mesh;
meshCollider.material = null;
}
}
meshRenderer.material = chunkMaterial;
Comment