Collision event issues

Hello, I’m having trouble getting trigger events between 2 entities and I could use a reality check. Here’s my job and the authoring for both prefabs:

using Unity.Burst;
using Unity.Collections;
using Unity.Entities;
using Unity.Physics;
using Unity.Physics.Systems;

[UpdateInGroup(typeof(FixedStepSimulationSystemGroup))]
[UpdateAfter(typeof(StepPhysicsWorld))]
[UpdateBefore(typeof(EndFramePhysicsSystem))]
public partial class ApplyOnHitEffectJobSystem : SystemBase
{
    private StepPhysicsWorld m_StepPhysicsWorld;

    protected override void OnCreate()
    {
        m_StepPhysicsWorld = World.GetOrCreateSystem<StepPhysicsWorld>();
        RequireForUpdate(GetEntityQuery(new EntityQueryDesc
        {
            All = new ComponentType[] { typeof(ApplyOnHitEffect) }
        }));
    }

    protected override void OnStartRunning()
    {
        this.RegisterPhysicsRuntimeSystemReadOnly();
    }

    protected override void OnUpdate()
    {
        //Dependency.Complete();

        var applyOnHitEffectJob = new ApplyOnHitEffectJob
        {
            commandBuffer = World.GetExistingSystem<EndSimulationEntityCommandBufferSystem>().CreateCommandBuffer(),
            applyOnHitEffectFromEntity = GetComponentDataFromEntity<ApplyOnHitEffect>(true),
            applyAbilityEffectFromEntity = GetBufferFromEntity<ApplyAbilityEffect>(true),
            casterFromEntity = GetComponentDataFromEntity<Caster>(true),
        }.Schedule(m_StepPhysicsWorld.Simulation, Dependency);

        Dependency = applyOnHitEffectJob;

        //applyOnHitEffectJob.Complete();
    }

    [BurstCompile]
    public struct ApplyOnHitEffectJob : ITriggerEventsJob
    {
        public EntityCommandBuffer commandBuffer;
        [ReadOnly] public ComponentDataFromEntity<ApplyOnHitEffect> applyOnHitEffectFromEntity;
        [ReadOnly] public BufferFromEntity<ApplyAbilityEffect> applyAbilityEffectFromEntity;
        [ReadOnly] public ComponentDataFromEntity<Caster> casterFromEntity;

        public void Execute(TriggerEvent triggerEvent)
        {
            // Initialize necessary variables to apply effects
            var effectEntity    = Entity.Null;
            var caster          = Entity.Null;
            var target          = Entity.Null;

            if (applyOnHitEffectFromEntity.HasComponent(triggerEvent.EntityA))
            {
                effectEntity = applyOnHitEffectFromEntity[triggerEvent.EntityA].effect;
            }
            if (applyOnHitEffectFromEntity.HasComponent(triggerEvent.EntityB))
            {
                effectEntity = applyOnHitEffectFromEntity[triggerEvent.EntityB].effect;
            }

            if (casterFromEntity.HasComponent(triggerEvent.EntityA))
            {
                caster = casterFromEntity[triggerEvent.EntityA].caster;
            }
            if (casterFromEntity.HasComponent(triggerEvent.EntityB))
            {
                caster = casterFromEntity[triggerEvent.EntityB].caster;
            }

            if (applyAbilityEffectFromEntity.HasComponent(triggerEvent.EntityA))
            {
                target = triggerEvent.EntityA;
            }
            if (applyAbilityEffectFromEntity.HasComponent(triggerEvent.EntityB))
            {
                target = triggerEvent.EntityB;
            }

            // If the values are not set, exit early.
            if (effectEntity == Entity.Null ||
                caster == Entity.Null ||
                target == Entity.Null)
            {
                return;
            }

            // Apply the effect
            var effectInstance = commandBuffer.Instantiate(effectEntity);
            commandBuffer.AddComponent(effectInstance, new Caster { caster = caster });
            commandBuffer.AppendToBuffer(target, new ApplyAbilityEffect { effect = effectInstance });
        }
    }
}

The ability authoring and its “Collides With” categories:
8426955--1115472--upload_2022-9-8_23-27-34.png

The enemy (no categories since it’s “Everything”):
8426955--1115478--upload_2022-9-8_23-29-2.png

1st of all: The ability entity definitely has the ApplyOnHitEffect and Caster component and the enemies definitely have ApplyAbilityEffect buffers… so what am I doing wrong? In the attached gif I am expecting them to die (be destroyed) but they aren’t. This has to be a collision filter issue but I just don’t understand how.

8426955--1115481--Fireball4.gif

It all looks good to me. There are a few points that might cause issues. Otherwise I would just add a Debug.Log($“some info {someVariables}”) everywhere to see if stuff gets triggered.

  1. I think you are missing the “AddJobHandleForProducer” you need to tell the ECB that it depends on this system to finish before executing.

endECBSystem.AddJobHandleForProducer(Dependency);
where endECBsystem is a field holding your World.GetExistingSystem()

  1. I don’t know if this is a problem but I schedule all my triggers (so this might be worth a shot [pun intended]):
    UpdateAfter(typeof(ExportPhysicsWorld)), UpdateBefore(typeof(EndFramePhysicsSystem))

  2. there is a warning in your screenshots “the object… static … Add physics body … move it at run time”. I am not sure if this could cause issues.

It appears to have been something to do with the scale of the object not being uniform. Now that it’s uniform collisions work as expected.