ITriggerEventsJob Execute not firing in OnUpdate

Hi, I’m exploring DOTS and doing else perfectly working tutorial and got stuck no this code with triggering collisions. Code, as you see below, throws warning: "
“Ignoring invalid [UpdateAfter] attribute on Systems.PickupOnTriggerSystem targeting Unity.Physics.Systems.EndFramePhysicsSystem.
This attribute can only order systems that are members of the same ComponentSystemGroup instance.
Make sure that both systems are in the same system group with [UpdateInGroup(typeof(Unity.Entities.SimulationSystemGroup)],
or by manually adding both systems to the same group’s update list.”

That I fixed with attribute above PickupOnTriggerSystem : [UpdateInGroup(typeof(FixedStepSimulationSystemGroup))]
So I get no warning, but Execute of the job is not triggered.

Also tried to add [UpdateAfter(typeof(StepPhysicsWorld))] attribute, but that do not seems to help either.

I need help with this, if anyone could, please.

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

namespace Systems
{
    // [UpdateInGroup(typeof(FixedStepSimulationSystemGroup))]
    // [UpdateAfter(typeof(StepPhysicsWorld))]
    [UpdateAfter(typeof(EndFramePhysicsSystem))]
    public class PickupOnTriggerSystem : JobComponentSystem
    {
        private BuildPhysicsWorld buildPhysicsWorld;
        private StepPhysicsWorld stepPhysicsWorld;

        private EndSimulationEntityCommandBufferSystem commandBufferSystem;

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

        [BurstCompile]
        struct PickupOnTriggerSystemJob : ITriggerEventsJob
        {
            [ReadOnly] public PhysicsWorld World;
           
            [ReadOnly] public ComponentDataFromEntity<PickupTag> allPickups;
            [ReadOnly] public ComponentDataFromEntity<PlayerTag> allPlayers;

            public EntityCommandBuffer entityCommandBuffer;

            public void Execute(TriggerEvent triggerEvent)
            {
                Entity entityA = triggerEvent.EntityA;
                Entity entityB = triggerEvent.EntityB;

                if (allPickups.HasComponent(entityA) && allPickups.HasComponent(entityB))
                {
                    return;
                }

                if (allPickups.HasComponent(entityA) && allPlayers.HasComponent(entityB))
                {
                    UnityEngine.Debug.Log($"Pickup Entity A: {entityA} collided with Player Entity B: {entityB}");
                    entityCommandBuffer.DestroyEntity(entityA);
                }
                else if (allPlayers.HasComponent(entityA) && allPickups.HasComponent(entityB))
                {
                    UnityEngine.Debug.Log($"Player Entity A: {entityA} collided with PickUp Entity B: {entityB}");
                    entityCommandBuffer.DestroyEntity(entityB);
                };
            }
        }

        protected override JobHandle OnUpdate(JobHandle inputDependencies)
        {
            var job = new PickupOnTriggerSystemJob
            {
                allPickups = GetComponentDataFromEntity<PickupTag>(true),
                allPlayers = GetComponentDataFromEntity<PlayerTag>(true),
                entityCommandBuffer = commandBufferSystem.CreateCommandBuffer(),
            };

            JobHandle jobHandle = job.Schedule(stepPhysicsWorld.Simulation, ref buildPhysicsWorld.PhysicsWorld,
                inputDependencies);

            commandBufferSystem.AddJobHandleForProducer(jobHandle);
            jobHandle.Complete();
            return jobHandle;
        }
    }
}

Solved this. Collision Response on Physics Shape was not set to Raise Trigger Events. can’t find the option to delete this post so leaving the hint here.

Hey folks. I’m having a similar problem as reported by @PZ_Alda

using Unity.Burst;
using Unity.Collections;
using Unity.Entities;
using Unity.Jobs;
using Unity.Mathematics;
using Unity.Transforms;
using Unity.Physics;
using Unity.Physics.Systems;

using Test.General;

namespace Test.Runner3D.WorldElement
{
    [UpdateAfter(typeof(EndFramePhysicsSystem))]
    public class PickupCollectableSystem : JobComponentSystem
    {
        private BuildPhysicsWorld buildPhysicsWorld;
        private StepPhysicsWorld stepPhysicsWorld;
        private EndSimulationEntityCommandBufferSystem commandBufferSystem;

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

        protected override JobHandle OnUpdate(JobHandle inputDeps)
        {
            TriggerJob triggerJob = new TriggerJob
            {
                players = GetComponentDataFromEntity<PlayerTag>(),
                tracks = GetComponentDataFromEntity<TrackTag>(),
                entitiesToDelete = GetComponentDataFromEntity<DeleteTag>(),
                entityCommandBuffer = commandBufferSystem.CreateCommandBuffer()
            };           

            var jobHandle = triggerJob.Schedule(stepPhysicsWorld.Simulation, ref buildPhysicsWorld.PhysicsWorld, inputDeps);

            commandBufferSystem.AddJobHandleForProducer(jobHandle);

            jobHandle.Complete();

            return jobHandle;
        }

        private struct TriggerJob : ITriggerEventsJob
        {
            [ReadOnly]
            public ComponentDataFromEntity<PlayerTag> players;
            [ReadOnly]
            public ComponentDataFromEntity<TrackTag> tracks;
            [ReadOnly]
            public ComponentDataFromEntity<DeleteTag> entitiesToDelete;

            public EntityCommandBuffer entityCommandBuffer;

            public void Execute(TriggerEvent triggerEvent)
            {
                EntityTrigger(triggerEvent.EntityA, triggerEvent.EntityB);
                EntityTrigger(triggerEvent.EntityB, triggerEvent.EntityA);
            }

            private void EntityTrigger(Entity entityA, Entity entityB)
            {
                Debugs.Log("Check", players, tracks, entitiesToDelete, entityA, entityB);

                if (!players.HasComponent(entityA)) return;
                if (tracks.HasComponent(entityB)) return;

                Debugs.Log("HasComponent", entityA);

                if (entitiesToDelete.HasComponent(entityB)) return;

                Debugs.Log("!HasComponent", entityB);

                //entityCommandBuffer.AddComponent(entityB, new DeleteTag());
            }
        }
    }
}

The TriggerJob struct just “Execute” in the first moment of the interaction with the other collider. It’s like was stopped after some seconds. If a try collides other elements in the trigger Physics shappe, the rise Trigger Event doesn’t work anymore.

I’m working in Unity 2019.4.4 and I use this packages:

I made an Update to Unity 2019.4.20, but still with the problem. I’m trying other alternatives to work. If someone found the solution, write here, please hahahahha

Hi @JoaoSantos , here are some things you could try:

  1. Add [UpdateInGroup(typeof(FixedStepSimulationSystemGroup))] to your system. I guess it’s already running under that group (you can check in profiler or entities debugger), but it doesn’t hurt to make sure it’s there.
  2. Extend SystemBase instead of JobComponentSystem
  3. Make it work without using the EntityCommandBuffer in the job (temporarily remove commandBufferSystem.AddJobHandleForProducer(jobHandle); ), just to see if expected console outputs will be there.
  4. Upgrade to latest Unity Physics - I think we fixed some bugs related to triggers in last few months

Please let me know if this helps.

P.S. It’d be great if this thread could be moved to Unity Engine - Unity Discussions subforum, I’ll try to find someone who can do it.

1 Like

Thanks for the answer, @milos85miki .

I made some adjustments and a think was a problem of my side, using entity and monobehaviors with injection in trigger jobs. When a transform every element in entity, worked great.