Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

No Collider in Terrain on non-Fixed-Map #3

Open
blendR8 opened this issue May 7, 2019 · 6 comments
Open

No Collider in Terrain on non-Fixed-Map #3

blendR8 opened this issue May 7, 2019 · 6 comments

Comments

@blendR8
Copy link

blendR8 commented May 7, 2019

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.

@SebLague
Copy link
Owner

SebLague commented May 7, 2019

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.

@blendR8
Copy link
Author

blendR8 commented May 7, 2019

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

@ImDanTheDev
Copy link

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;
    }

@SimLeek
Copy link

SimLeek commented Jul 3, 2019

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.

@VincentLagerros
Copy link

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.

@andwerb
Copy link

andwerb commented Aug 19, 2020

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants