#Creating a Terrain Collider from a MonoBehaviour

1 messages · Page 1 of 1 (latest)

half sapphire
#

I can create the terrain collider just fine, but it's not displayed in the debug view and doesn't work.

#

Here is the code I'm using.

var entity = entityManager.CreateEntity();
            entityManager.SetName(entity, "Terrain Collider");

            // var terrainSize = terrain.terrainData.size;
            var terrainSize = new Vector3(100, 100, 100);

            Debug.Log("Terrain Width: " + terrainSize.x);
            Debug.Log("Terrain Length: " + terrainSize.z);

            var heights = new NativeArray<float>((int) terrainSize.x * (int) terrainSize.z, Allocator.Temp);
            heights[0] = 1;

            var terrainCollider = TerrainCollider.Create(
                heights,
                new int2((int) terrainSize.x, (int) terrainSize.z),
                new float3(1f, 1f, 1f),
                TerrainCollider.CollisionMethod.VertexSamples
            );

            entityManager.AddComponentData(entity, new PhysicsCollider {Value = terrainCollider});
            entityManager.AddComponent<PhysicsVelocity>(entity);
            entityManager.AddSharedComponentManaged(entity, new PhysicsWorldIndex {Value = 0});
            entityManager.AddComponentData(entity, PhysicsMass.CreateKinematic(MassProperties.UnitSphere));
            entityManager.AddComponentData(entity,
                new PhysicsDamping
                {
                    Linear = 0,
                    Angular = 0
                }
            );
            entityManager.AddComponentData(entity, new PhysicsGravityFactor {Value = 0});

            heights.Dispose();

#

Resulting Entity:

#

Here is what is should look like:

#

I'll try to add the missing components, maybe I can get it to work.

half sapphire
#

Yea that did the trick 😋

stoic schooner
#

@half sapphire thanks for posting this, it fixed my problem! In mine I was missing the PhysicsWorldIndex 😄