Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

ITriggerEventsJob Execute not firing in OnUpdate

Discussion in 'Entity Component System' started by PZ_Alda, Feb 3, 2021.

  1. PZ_Alda

    PZ_Alda

    Joined:
    Jan 5, 2021
    Posts:
    2
    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.

    Code (CSharp):
    1. using Tags;
    2. using Unity.Burst;
    3. using Unity.Collections;
    4. using Unity.Entities;
    5. using Unity.Jobs;
    6. using Unity.Physics;
    7. using Unity.Physics.Systems;
    8.  
    9. namespace Systems
    10. {
    11.     // [UpdateInGroup(typeof(FixedStepSimulationSystemGroup))]
    12.     // [UpdateAfter(typeof(StepPhysicsWorld))]
    13.     [UpdateAfter(typeof(EndFramePhysicsSystem))]
    14.     public class PickupOnTriggerSystem : JobComponentSystem
    15.     {
    16.         private BuildPhysicsWorld buildPhysicsWorld;
    17.         private StepPhysicsWorld stepPhysicsWorld;
    18.  
    19.         private EndSimulationEntityCommandBufferSystem commandBufferSystem;
    20.  
    21.         protected override void OnCreate()
    22.         {
    23.             base.OnCreate();
    24.             buildPhysicsWorld = World.GetOrCreateSystem<BuildPhysicsWorld>();
    25.             stepPhysicsWorld = World.GetOrCreateSystem<StepPhysicsWorld>();
    26.             commandBufferSystem = World.GetOrCreateSystem<EndSimulationEntityCommandBufferSystem>();
    27.         }
    28.  
    29.         [BurstCompile]
    30.         struct PickupOnTriggerSystemJob : ITriggerEventsJob
    31.         {
    32.             [ReadOnly] public PhysicsWorld World;
    33.            
    34.             [ReadOnly] public ComponentDataFromEntity<PickupTag> allPickups;
    35.             [ReadOnly] public ComponentDataFromEntity<PlayerTag> allPlayers;
    36.  
    37.             public EntityCommandBuffer entityCommandBuffer;
    38.  
    39.             public void Execute(TriggerEvent triggerEvent)
    40.             {
    41.                 Entity entityA = triggerEvent.EntityA;
    42.                 Entity entityB = triggerEvent.EntityB;
    43.  
    44.                 if (allPickups.HasComponent(entityA) && allPickups.HasComponent(entityB))
    45.                 {
    46.                     return;
    47.                 }
    48.  
    49.                 if (allPickups.HasComponent(entityA) && allPlayers.HasComponent(entityB))
    50.                 {
    51.                     UnityEngine.Debug.Log($"Pickup Entity A: {entityA} collided with Player Entity B: {entityB}");
    52.                     entityCommandBuffer.DestroyEntity(entityA);
    53.                 }
    54.                 else if (allPlayers.HasComponent(entityA) && allPickups.HasComponent(entityB))
    55.                 {
    56.                     UnityEngine.Debug.Log($"Player Entity A: {entityA} collided with PickUp Entity B: {entityB}");
    57.                     entityCommandBuffer.DestroyEntity(entityB);
    58.                 };
    59.             }
    60.         }
    61.  
    62.         protected override JobHandle OnUpdate(JobHandle inputDependencies)
    63.         {
    64.             var job = new PickupOnTriggerSystemJob
    65.             {
    66.                 allPickups = GetComponentDataFromEntity<PickupTag>(true),
    67.                 allPlayers = GetComponentDataFromEntity<PlayerTag>(true),
    68.                 entityCommandBuffer = commandBufferSystem.CreateCommandBuffer(),
    69.             };
    70.  
    71.             JobHandle jobHandle = job.Schedule(stepPhysicsWorld.Simulation, ref buildPhysicsWorld.PhysicsWorld,
    72.                 inputDependencies);
    73.  
    74.             commandBufferSystem.AddJobHandleForProducer(jobHandle);
    75.             jobHandle.Complete();
    76.             return jobHandle;
    77.         }
    78.     }
    79. }
     
  2. PZ_Alda

    PZ_Alda

    Joined:
    Jan 5, 2021
    Posts:
    2
    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.
     
  3. JoaoSantos

    JoaoSantos

    Joined:
    Mar 31, 2014
    Posts:
    20
    Hey folks. I'm having a similar problem as reported by @PZ_Alda

    Code (CSharp):
    1. using Unity.Burst;
    2. using Unity.Collections;
    3. using Unity.Entities;
    4. using Unity.Jobs;
    5. using Unity.Mathematics;
    6. using Unity.Transforms;
    7. using Unity.Physics;
    8. using Unity.Physics.Systems;
    9.  
    10. using Test.General;
    11.  
    12. namespace Test.Runner3D.WorldElement
    13. {
    14.     [UpdateAfter(typeof(EndFramePhysicsSystem))]
    15.     public class PickupCollectableSystem : JobComponentSystem
    16.     {
    17.         private BuildPhysicsWorld buildPhysicsWorld;
    18.         private StepPhysicsWorld stepPhysicsWorld;
    19.         private EndSimulationEntityCommandBufferSystem commandBufferSystem;
    20.  
    21.         protected override void OnCreate()
    22.         {
    23.             base.OnCreate();
    24.             buildPhysicsWorld = World.GetOrCreateSystem<BuildPhysicsWorld>();
    25.             stepPhysicsWorld = World.GetOrCreateSystem<StepPhysicsWorld>();
    26.             commandBufferSystem = World.GetOrCreateSystem<EndSimulationEntityCommandBufferSystem>();
    27.         }
    28.  
    29.         protected override JobHandle OnUpdate(JobHandle inputDeps)
    30.         {
    31.             TriggerJob triggerJob = new TriggerJob
    32.             {
    33.                 players = GetComponentDataFromEntity<PlayerTag>(),
    34.                 tracks = GetComponentDataFromEntity<TrackTag>(),
    35.                 entitiesToDelete = GetComponentDataFromEntity<DeleteTag>(),
    36.                 entityCommandBuffer = commandBufferSystem.CreateCommandBuffer()
    37.             };          
    38.  
    39.             var jobHandle = triggerJob.Schedule(stepPhysicsWorld.Simulation, ref buildPhysicsWorld.PhysicsWorld, inputDeps);
    40.  
    41.             commandBufferSystem.AddJobHandleForProducer(jobHandle);
    42.  
    43.             jobHandle.Complete();
    44.  
    45.             return jobHandle;
    46.         }
    47.  
    48.         private struct TriggerJob : ITriggerEventsJob
    49.         {
    50.             [ReadOnly]
    51.             public ComponentDataFromEntity<PlayerTag> players;
    52.             [ReadOnly]
    53.             public ComponentDataFromEntity<TrackTag> tracks;
    54.             [ReadOnly]
    55.             public ComponentDataFromEntity<DeleteTag> entitiesToDelete;
    56.  
    57.             public EntityCommandBuffer entityCommandBuffer;
    58.  
    59.             public void Execute(TriggerEvent triggerEvent)
    60.             {
    61.                 EntityTrigger(triggerEvent.EntityA, triggerEvent.EntityB);
    62.                 EntityTrigger(triggerEvent.EntityB, triggerEvent.EntityA);
    63.             }
    64.  
    65.             private void EntityTrigger(Entity entityA, Entity entityB)
    66.             {
    67.                 Debugs.Log("Check", players, tracks, entitiesToDelete, entityA, entityB);
    68.  
    69.                 if (!players.HasComponent(entityA)) return;
    70.                 if (tracks.HasComponent(entityB)) return;
    71.  
    72.                 Debugs.Log("HasComponent", entityA);
    73.  
    74.                 if (entitiesToDelete.HasComponent(entityB)) return;
    75.  
    76.                 Debugs.Log("!HasComponent", entityB);
    77.  
    78.                 //entityCommandBuffer.AddComponent(entityB, new DeleteTag());
    79.             }
    80.         }
    81.     }
    82. }
    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:

    upload_2021-2-19_11-29-49.png
     
  4. JoaoSantos

    JoaoSantos

    Joined:
    Mar 31, 2014
    Posts:
    20
    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
     
  5. milos85miki

    milos85miki

    Joined:
    Nov 29, 2019
    Posts:
    197
    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 https://forum.unity.com/forums/dots-physics.422/ subforum, I'll try to find someone who can do it.
     
    JoaoSantos likes this.
  6. JoaoSantos

    JoaoSantos

    Joined:
    Mar 31, 2014
    Posts:
    20
    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.