Search Unity

Bug ITriggerEventsJob reporting triggers but component attached to trigger not present

Discussion in 'Physics for ECS' started by Evgeni_Incineration, Nov 11, 2022.

  1. Evgeni_Incineration

    Evgeni_Incineration

    Joined:
    Jul 4, 2022
    Posts:
    27
    I have a trigger attached to enemies in my project.
    upload_2022-11-11_18-58-29.png

    I am adding DamageZoneComponent to decorate the trigger collision box.
    Next in a system I am trying to find all DamageZoneComponent decorated entities that are colliding with player.

    When I use ComponentLookup<DamageZoneComponent> to check if an entity has DamageZoneComponent non of them turn out to have it.

    This is impossible as I am decorating all DamageZones with it. There are no other trigger colliders in the scene. I do get
    triggerEvent.EntityB to have PlayerComponent but no DamageZoneComponents attached to triggerEvent.EntityA.

    Any tips what I might be doing wrong is appreciated, otherwise it looks like a bug to me.

    Attaching the whole system code as I might have some setup wrong.

    Code (CSharp):
    1. using Unity.Burst;
    2. using Unity.Collections;
    3. using Unity.Entities;
    4. using Unity.Physics;
    5. using Unity.Physics.Systems;
    6.  
    7. [UpdateInGroup(typeof(FixedStepSimulationSystemGroup))]
    8. [UpdateAfter(typeof(PhysicsSystemGroup))]
    9. [BurstCompile]
    10. public partial struct DamageZoneCollisionSystem : ISystem
    11. {
    12.     [BurstCompile]
    13.     public void OnCreate(ref SystemState state) { }
    14.  
    15.     [BurstCompile]
    16.     public void OnDestroy(ref SystemState state) { }
    17.  
    18.     [BurstCompile]
    19.     public void OnUpdate(ref SystemState state)
    20.     {
    21.         var simuilationSingleton = SystemAPI.GetSingleton<SimulationSingleton>();
    22.         var playerEntity = SystemAPI.GetSingletonEntity<PlayerComponent>();
    23.         var player = SystemAPI.GetAspectRW<PlayerAspect>(playerEntity);
    24.  
    25.         var damageZonesCollidingWithPlayer = new NativeList<Entity>(Allocator.TempJob);
    26.  
    27.         state.Dependency = new CheckDamageZoneCollisionWithPlayer()
    28.         {
    29.             DamageZonesCollidingWithPlayer = damageZonesCollidingWithPlayer,
    30.             DamageZoneLookup = SystemAPI.GetComponentLookup<DamageZoneComponent>(isReadOnly: true),
    31.             PlayerLookup = SystemAPI.GetComponentLookup<PlayerComponent>(isReadOnly: true),
    32.         }
    33.         .Schedule(simuilationSingleton, state.Dependency);
    34.  
    35.         state.Dependency.Complete();
    36.  
    37.         foreach (var (damageZone, damageZoneEntity) in SystemAPI.Query<RefRW<DamageZoneComponent>>().WithEntityAccess())
    38.         {
    39.             if (damageZonesCollidingWithPlayer.Contains(damageZoneEntity))
    40.             {
    41.                 damageZone.ValueRW.Countdown -= SystemAPI.Time.DeltaTime;
    42.                 if (damageZone.ValueRW.Countdown < 0)
    43.                 {
    44.                     player.Life -= damageZone.ValueRO.Damage;
    45.                     damageZone.ValueRW.Countdown = damageZone.ValueRO.Time;
    46.                 }
    47.             }
    48.             else
    49.             {
    50.                 damageZone.ValueRW.Countdown = -1f;
    51.             }
    52.         }
    53.  
    54.         damageZonesCollidingWithPlayer.Dispose();
    55.     }
    56.  
    57.     [BurstCompile]
    58.     struct CheckDamageZoneCollisionWithPlayer : ITriggerEventsJob
    59.     {
    60.         public NativeList<Entity> DamageZonesCollidingWithPlayer;
    61.         [ReadOnly] internal ComponentLookup<DamageZoneComponent> DamageZoneLookup;
    62.         [ReadOnly] internal ComponentLookup<PlayerComponent> PlayerLookup;
    63.  
    64.         [BurstCompile]
    65.         public void Execute(TriggerEvent triggerEvent)
    66.         {
    67.             if (DamageZoneLookup.HasComponent(triggerEvent.EntityA))
    68.             {
    69.                 UnityEngine.Debug.Log("EntityA is damage zone");
    70.             }
    71.             if (DamageZoneLookup.HasComponent(triggerEvent.EntityB))
    72.             {
    73.                 UnityEngine.Debug.Log("EntityB is damage zone");
    74.             }
    75.             if (PlayerLookup.HasComponent(triggerEvent.EntityA))
    76.             {
    77.                 UnityEngine.Debug.Log("EntityA is player");
    78.             }
    79.             if (PlayerLookup.HasComponent(triggerEvent.EntityB))
    80.             {
    81.                 UnityEngine.Debug.Log("EntityB is player");
    82.             }
    83.  
    84.             // if (PlayerLookup.HasComponent(triggerEvent.EntityA) && DamageZoneLookup.HasComponent(triggerEvent.EntityB))
    85.             // {
    86.             //     DamageZonesCollidingWithPlayer.Add(triggerEvent.EntityB);
    87.             // }
    88.             // if (PlayerLookup.HasComponent(triggerEvent.EntityB) && DamageZoneLookup.HasComponent(triggerEvent.EntityA))
    89.             // {
    90.             //     DamageZonesCollidingWithPlayer.Add(triggerEvent.EntityA);
    91.             // }
    92.         }
    93.     }
    94. }

     
    Last edited: Nov 11, 2022
  2. Evgeni_Incineration

    Evgeni_Incineration

    Joined:
    Jul 4, 2022
    Posts:
    27
    On further investigation it seems that creation of "CoumpundShape" is the problem as I am getting the entities from parent object. If there is a way to turn off this behavior I think ill get the result I am expecting.