Search Unity

Question Collision event issues

Discussion in 'Physics for ECS' started by Blargenflargle, Sep 9, 2022.

  1. Blargenflargle

    Blargenflargle

    Joined:
    Feb 24, 2019
    Posts:
    92
    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:
    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(StepPhysicsWorld))]
    9. [UpdateBefore(typeof(EndFramePhysicsSystem))]
    10. public partial class ApplyOnHitEffectJobSystem : SystemBase
    11. {
    12.     private StepPhysicsWorld m_StepPhysicsWorld;
    13.  
    14.     protected override void OnCreate()
    15.     {
    16.         m_StepPhysicsWorld = World.GetOrCreateSystem<StepPhysicsWorld>();
    17.         RequireForUpdate(GetEntityQuery(new EntityQueryDesc
    18.         {
    19.             All = new ComponentType[] { typeof(ApplyOnHitEffect) }
    20.         }));
    21.     }
    22.  
    23.     protected override void OnStartRunning()
    24.     {
    25.         this.RegisterPhysicsRuntimeSystemReadOnly();
    26.     }
    27.  
    28.     protected override void OnUpdate()
    29.     {
    30.         //Dependency.Complete();
    31.  
    32.         var applyOnHitEffectJob = new ApplyOnHitEffectJob
    33.         {
    34.             commandBuffer = World.GetExistingSystem<EndSimulationEntityCommandBufferSystem>().CreateCommandBuffer(),
    35.             applyOnHitEffectFromEntity = GetComponentDataFromEntity<ApplyOnHitEffect>(true),
    36.             applyAbilityEffectFromEntity = GetBufferFromEntity<ApplyAbilityEffect>(true),
    37.             casterFromEntity = GetComponentDataFromEntity<Caster>(true),
    38.         }.Schedule(m_StepPhysicsWorld.Simulation, Dependency);
    39.  
    40.         Dependency = applyOnHitEffectJob;
    41.  
    42.         //applyOnHitEffectJob.Complete();
    43.     }
    44.  
    45.     [BurstCompile]
    46.     public struct ApplyOnHitEffectJob : ITriggerEventsJob
    47.     {
    48.         public EntityCommandBuffer commandBuffer;
    49.         [ReadOnly] public ComponentDataFromEntity<ApplyOnHitEffect> applyOnHitEffectFromEntity;
    50.         [ReadOnly] public BufferFromEntity<ApplyAbilityEffect> applyAbilityEffectFromEntity;
    51.         [ReadOnly] public ComponentDataFromEntity<Caster> casterFromEntity;
    52.  
    53.         public void Execute(TriggerEvent triggerEvent)
    54.         {
    55.             // Initialize necessary variables to apply effects
    56.             var effectEntity    = Entity.Null;
    57.             var caster          = Entity.Null;
    58.             var target          = Entity.Null;
    59.  
    60.             if (applyOnHitEffectFromEntity.HasComponent(triggerEvent.EntityA))
    61.             {
    62.                 effectEntity = applyOnHitEffectFromEntity[triggerEvent.EntityA].effect;
    63.             }
    64.             if (applyOnHitEffectFromEntity.HasComponent(triggerEvent.EntityB))
    65.             {
    66.                 effectEntity = applyOnHitEffectFromEntity[triggerEvent.EntityB].effect;
    67.             }
    68.  
    69.             if (casterFromEntity.HasComponent(triggerEvent.EntityA))
    70.             {
    71.                 caster = casterFromEntity[triggerEvent.EntityA].caster;
    72.             }
    73.             if (casterFromEntity.HasComponent(triggerEvent.EntityB))
    74.             {
    75.                 caster = casterFromEntity[triggerEvent.EntityB].caster;
    76.             }
    77.  
    78.             if (applyAbilityEffectFromEntity.HasComponent(triggerEvent.EntityA))
    79.             {
    80.                 target = triggerEvent.EntityA;
    81.             }
    82.             if (applyAbilityEffectFromEntity.HasComponent(triggerEvent.EntityB))
    83.             {
    84.                 target = triggerEvent.EntityB;
    85.             }
    86.  
    87.             // If the values are not set, exit early.
    88.             if (effectEntity == Entity.Null ||
    89.                 caster == Entity.Null ||
    90.                 target == Entity.Null)
    91.             {
    92.                 return;
    93.             }
    94.  
    95.             // Apply the effect
    96.             var effectInstance = commandBuffer.Instantiate(effectEntity);
    97.             commandBuffer.AddComponent(effectInstance, new Caster { caster = caster });
    98.             commandBuffer.AppendToBuffer(target, new ApplyAbilityEffect { effect = effectInstance });
    99.         }
    100.     }
    101. }
    The ability authoring and its "Collides With" categories:
    upload_2022-9-8_23-27-34.png
    upload_2022-9-8_23-27-56.png

    The enemy (no categories since it's "Everything"):
    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.
     

    Attached Files:

  2. Arnold_2013

    Arnold_2013

    Joined:
    Nov 24, 2013
    Posts:
    286
    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<EndSimulationEntityCommandBufferSystem>()

    2. 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))

    3. 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.
     
  3. Blargenflargle

    Blargenflargle

    Joined:
    Feb 24, 2019
    Posts:
    92
    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.