Search Unity

Examples of a system similar to "OnTriggerExit"?

Discussion in 'Entity Component System' started by MostHated, Sep 21, 2019.

  1. MostHated

    MostHated

    Joined:
    Nov 29, 2015
    Posts:
    1,235
    Hey all,
    I implemented some sphere colliders on a few entities. I have everything working so that when the two colliders intersect something becomes active on one of the entities within the pair. I found some sample code in another post that I am using:

    Code (CSharp):
    1.  private struct CollisionJob : ITriggerEventsJob
    2.         {
    3.             public ComponentDataFromEntity<PedDataComponent> pedData;
    4.             public EntityCommandBuffer CommandBuffer;
    5.             public int puppetTimer;
    6.  
    7.             public void Execute(TriggerEvent triggerEvent)
    8.             {
    9.                 Entity pedestrian = new Entity();
    10.  
    11.                 if (pedData.Exists(triggerEvent.Entities.EntityA))
    12.                 {
    13.                     pedestrian = triggerEvent.Entities.EntityA;
    14.                 }
    15.                 else if (pedData.Exists(triggerEvent.Entities.EntityB))
    16.                 {
    17.                     pedestrian = triggerEvent.Entities.EntityB;
    18.                 }
    19.  
    20.                 if (pedestrian != Entity.Null)
    21.                 {
    22.                     var data = pedData[pedestrian];
    23.                     data.puppetTimer = puppetTimer;
    24.                     CommandBuffer.SetComponent(pedestrian, data);
    25.                 }
    26.             }
    27.         }

    Currently, when the trigger is activated, and for each subsequent frame for the duration of the colliders intersection the job will set a value to a component on one of the entities so that it simply refreshes the int value.

    In another job I have a system checking all entities with that component for it's value. Once the value becomes > 1 it activates a system on the entity and then decrements the value by 1 each frame, so I can just have it set the value to something small so that once the trigger stops refreshing that value it will decrement to 0 and then that disables the system again on the entity.

    While that seems to be working ok, I was wondering if there was another way to detect if there is no longer a collision via some other means more "official" to the physics/trigger similar to using OnTriggerExit from the legacy physics system? I have been trying to locate examples but have not really found anything yet. Does anyone happen to know of where I might be able to locate one?

    Thanks,
    -MH