How to add collision event programmatically

Hi

I'm generating my entities with the pure ecs way. Here is the code I'm generating the collider;

var boxGeometry = new BoxGeometry();
        boxGeometry.Center = mesh.bounds.center;
        boxGeometry.Size = colliderSize;
        boxGeometry.Orientation = quaternion.identity;
        BlobAssetReference<Unity.Physics.Collider> boxCollider = Unity.Physics.BoxCollider.Create(boxGeometry, CollisionFilter.Default);
        manager.SetComponentData(entity, new PhysicsCollider
        {
            Value = boxCollider
        });

It's working fine with raycasting. But I'm not able to trigger the collision events. Here is my collision system;

public class TestCollisionSystem : JobComponentSystem
{
    private BuildPhysicsWorld buildPhysicsWorld;
    private StepPhysicsWorld stepPhysicsWorld;

    protected override void OnCreate()
    {
        buildPhysicsWorld = World.GetOrCreateSystem<BuildPhysicsWorld>();
        stepPhysicsWorld = World.GetOrCreateSystem<StepPhysicsWorld>();
    }

    [BurstCompile]
    struct TestCollisionSystemJob : ICollisionEventsJob
    {
        [ReadOnly] public ComponentDataFromEntity<CarInfo> carInfoGroup;

        public void Execute(CollisionEvent collisionEvent)
        {
            Entity entityA = collisionEvent.EntityA;
            Entity entityB = collisionEvent.EntityB;

            Debug.Log(entityA);
            Debug.Log(entityB);
        }
    }

    protected override JobHandle OnUpdate(JobHandle inputDeps)
    {
        var job = new TestCollisionSystemJob();
        job.carInfoGroup = GetComponentDataFromEntity<CarInfo>(true);

        JobHandle jobHandle = job.Schedule(stepPhysicsWorld.Simulation, ref buildPhysicsWorld.PhysicsWorld, inputDeps);
        return jobHandle;
    }
}

What is missing here? What component do I need to add while generating the entities?

You have to pass a Material into the Create method with a CollisionResponse set to "CollideRaiseCollisionEvents"

take a look at this page https://discussions.unity.com/t/757935

check out the Unity.Physic.Material

new Unity.Physics.Material{
                    CollisionResponse = CollisionResponsePolicy.CollideRaiseCollisionEvents
                }

I've added the material but still not working.

var physicsMaterial = new Unity.Physics.Material
        {
            CustomTags = Unity.Physics.Material.Default.CustomTags,
            CollisionResponse = CollisionResponsePolicy.CollideRaiseCollisionEvents
        };

var boxGeometry = new BoxGeometry();
        boxGeometry.Center = mesh.bounds.center;
        boxGeometry.Size = colliderSize;
        boxGeometry.Orientation = quaternion.identity;


        BlobAssetReference<Unity.Physics.Collider> boxCollider = Unity.Physics.BoxCollider.Create(boxGeometry, CollisionFilter.Default , physicsMaterial);

        manager.SetComponentData(entity, new PhysicsCollider
        {
            Value = boxCollider
        });

[/quote]

in the link:

Did you try this?

[quote=“MixGrey”, post:7, topic: 757935]
I think I figured it out. When creating the Collider, there’s a field for a Unity.Physics.Material which has a field for a Unity.Physics.Material.MaterialFlags, which is an enum. One of the options is EnableCollisionEvents. I’m not sure what the other two are.

Creating the Collider like this leads to CollisionEvents actually being generated.

var material = new Unity.Physics.Material
            {
                CustomTags = Unity.Physics.Material.Default.CustomTags,
                Flags = Unity.Physics.Material.MaterialFlags.EnableCollisionEvents |
                    Unity.Physics.Material.MaterialFlags.EnableMassFactors |
                    Unity.Physics.Material.MaterialFlags.EnableSurfaceVelocity,
                Friction = Unity.Physics.Material.Default.Friction,
                FrictionCombinePolicy = Unity.Physics.Material.Default.FrictionCombinePolicy,
                Restitution = Unity.Physics.Material.Default.Restitution,
                RestitutionCombinePolicy = Unity.Physics.Material.Default.RestitutionCombinePolicy,
            };

            var boxCollider = Unity.Physics.BoxCollider.Create(
                new BoxGeometry{
                    Orientation=quaternion.identity,
                    Size=new float3(0.25f,0.25f,0.25f)},
                CollisionFilter.Default,
                material
            );

Then: var colliderComponent = new PhysicsCollider { Value = boxCollider };
EntityManager.AddComponentData<PhysicsCollider>(entity, colliderComponent);
and so on.

I haven’t tested whether it sees all of the CollisionEvents it should, but at least it sees some, and that’s a start.
[/quote]

Yes I did. But not working.

Try getting rid of the [BurstCompile] tag in your job and disable Burst Compilation in your project and try again.