Marching-Cubes
Marching-Cubes copied to clipboard
No Collider in Terrain on non-Fixed-Map
Hey, at first: great work. problem: if i generate colliders automatically in terrain mode and use a non fixed map, the collider isn't active. if I disable and enable the chunk or the meshcollider component of the cunk in playmode, the collider works.
how to fix this?
there a no problems in fixed map mode.
Hi, do you have generate colliders turned on in the mesh generation settings, or did you implement your own? If the latter, my system might be interfering with yours.
I have yours enabled in the mesh generator script. i use your assets without any modification. if i used a fixed map size it works out of the box. i have version 2019.1.0f2
Edit: problem exists in v 2017.4... also
I have experienced this exact issue in Unity 2019.1.1f1. I believe the reason for the collider not working is that the mesh does not have its bounds calculated by the time you assign it to the mesh collider. I see where you purposefully enable and disable the mesh collider to cause it to update; however, that does not appear to work as expected. I found that if you wait until the end of the frame to re-enable the collider, then the bounds will update correctly. I used a coroutine to demonstrate my workaround, but I am positive there is a more proper fix.
meshCollider.enabled = false;
StartCoroutine(EnableCollider());
//meshCollider.enabled = true;
}
meshRenderer.material = mat;
}
private IEnumerator EnableCollider()
{
yield return new WaitForEndOfFrame();
meshCollider.enabled = true;
}
I'm not sure about that last one, since the same chunk can have its mesh changed many times in UpdateChunkMesh as the player moves, but I haven't tried it.
Instead, I added this command to Chunk.cs:
public void UpdateColliders()
{
if (generateCollider)
{
meshCollider.sharedMesh = null;
meshCollider.sharedMesh = mesh;
// force update
meshCollider.enabled = false;
meshCollider.enabled = true;
}
}
And I called it at the very bottom of the UpdateChunkMesh function in MeshGenerator.cs:
chunk.UpdateColliders();
It's working in my fork here and here. (However, I need DontGoThroughThings if I'm shooting through landscapes quickly enough.)
There's probably a better fix, but this works.
When updating a lot of mesh colliders, I found it much more efficient to do it with https://docs.unity3d.com/2019.3/Documentation/ScriptReference/Physics.BakeMesh.html
https://pastebin.com/5vtND7ap << MeshChunk
https://pastebin.com/G4qYGHcP << JobifiedBaking
When doing live edit (Updates the collider every frame) i got ~60fps (with force update ~30fps, not editing ~120fps). This uses Unity Job system, so I think it is a bit of overkill if you want a static terrain. I personally use this over "Forceupdate" in my project because of the massive FPS boost.
I had the idea of using a coroutine as well, unfortunately around 200 units from your start, you no longer update meshes. SimLeek has the most effective approach and it works well.
Also, Sebastian already baked the mesh in the Chunks file.