Entity Mesh Colliders

Hi everyone,

I am unsure how to add a mesh collider onto an entity. If this is not possible, is it possible to create a game object with a mesh collider, with burst and ECS?

Note I am creating procedural terrain so everything must be runtime

You can use MeshCollider.Create (samples here, with other physics sample code in Unity’s EntityComponentSystemSamples repo). There are several overloads that accept varying inputs like Mesh, Mesh.MeshData, and arrays of vertices/triangles.

Make sure required components are added. PhysicsWorldIndex and PhysicsCollider are required, along with transform info (LocalTransform for dynamic entity, LocalTransform and/or LocalToWorld for static entity). Info on other optional components is here. A general sample about creating physics bodies at runtime can be found here.

Keep in mind that creating colliders manually in this manner at runtime circumvents subscene-based blob reference tracking and automatic disposal, so you will need to manually dispose the collider blob when the entity is to be destroyed.

2 Likes

Checkout Unity DOTS samples on github, if you haven’t yet. There are few categories of samples. One of them focuses on the runtime procedural mesh.

1 Like

Definetly closer, here is what i have now

    void CreateCollider()
    {
        NativeArray<float3> Vertices = new NativeArray<Vector3>(TerrainMesh.vertices, Allocator.TempJob).Reinterpret<float3>();
        NativeArray<int3> Tris = new NativeArray<int3>(TerrainMesh.triangles, Allocator.TempJob);
        BlobAssetReference<Unity.Physics.Collider> Collider = Unity.Physics.MeshCollider.Create(Vertices, Tris, Unity.Physics.CollisionFilter.Default, Unity.Physics.Material.Default);
    }

However, i cant get int3 from int in the mesh’s triangles data

As I said, other overloads exist, like one that takes a Mesh directly.

I think that method is outdated, as i have searched the docs and tried but it doesnt work
It only takes native arrays

I linked you the exact method overload and sample code using it in my first message. The method also definitely exists in the package code.
The functionality has been there since 1.1.0-exp.1 which is over a year old. If you’re on something old, update to 1.3.2.

1 Like