Search Unity

Confusion In Collision Triggers

Discussion in 'Entity Component System' started by Filter_Feeder, Aug 9, 2021.

  1. Filter_Feeder

    Filter_Feeder

    Joined:
    Oct 19, 2017
    Posts:
    45
    Okay, so I'm very confused as to how the ECS event system is working.

    I'm for the fun of it fooling around with a molecule simulator, where particles that collide form "bindings" which keeps them teathered. I'm trying to have two molecules instantiate a new binding whenever they collide.

    I have got the instantiation procedure working, but I'm not sure how I can get data from the two molecules into the job that is instantiating the binding (features of the molecules needs to affect the property of the binding)

    I tried using "ComponentDataFromEntity" but without much success. I'm unsure how to implement it as I am not certain what goes on behind the scenes when I have things set up like I do.

    P.S The "Entity" in the execute-part of the Job is a converted entity using the conversion workflow.
    I made a comment in the code where I want to get the component data.

    Any help would be much appreciated



    Code (CSharp):
    1. EndSimulationEntityCommandBufferSystem commandBufferSystem;
    2.  
    3.     }
    4.  
    5.     private struct TriggerJob : ITriggerEventsJob
    6.     {
    7.         [ReadOnly] public ComponentDataFromEntity<PhysicsVelocity> PhysicsVelocityGroup;
    8.         public Entity entity;
    9.         public EntityCommandBuffer commandBuffer;
    10.         public ComponentDataFromEntity<BindingSiteData> binding;
    11.         public ComponentDataFromEntity<BindingSiteData> thisBindingSite;
    12.         public void Execute(TriggerEvent triggerEvent)
    13.         {
    14.  
    15.             Entity entityA = triggerEvent.EntityA;
    16.             Entity entityB = triggerEvent.EntityB;
    17.  
    18.             if (binding.HasComponent(entityB))
    19.             {
    20.                 Entity myEntity = commandBuffer.Instantiate(entity);
    21.                 // This is where I want to get ComponentData from entityA and entityB
    22.  
    23.                 commandBuffer.AddComponent(myEntity, new BindingData
    24.                 {
    25.                     e1 = triggerEvent.EntityA,
    26.                     e2 = triggerEvent.EntityB
    27.                 });
    28.             }
    29.         }
    30.     }
    31.  
    32.  
    33.     private BuildPhysicsWorld physicsWorld;
    34.     private StepPhysicsWorld stepPhysicsWorld;
    35.  
    36.     protected override void OnCreate()
    37.     {
    38.         physicsWorld = World.GetOrCreateSystem<BuildPhysicsWorld>();
    39.         stepPhysicsWorld = World.GetOrCreateSystem<StepPhysicsWorld>();
    40.         commandBufferSystem = World.GetExistingSystem<EndSimulationEntityCommandBufferSystem>();
    41.     }
    42.  
    43.     protected override JobHandle OnUpdate (JobHandle inputDeps)
    44.     {
    45.         JobHandle handle;
    46.         TriggerJob triggerJob = new TriggerJob
    47.         {
    48.             entity = entityPrefab,
    49.             binding = GetComponentDataFromEntity<BindingSiteData>(),
    50.             PhysicsVelocityGroup = GetComponentDataFromEntity<PhysicsVelocity>(true),
    51.             commandBuffer = commandBufferSystem.CreateCommandBuffer()
    52.         };
    53.  
    54.         handle = triggerJob.Schedule(stepPhysicsWorld.Simulation, ref physicsWorld.PhysicsWorld, inputDeps);
    55.  
    56.         commandBufferSystem.AddJobHandleForProducer(handle);
    57.  
    58.         return handle;
    59.     }
     
  2. Filter_Feeder

    Filter_Feeder

    Joined:
    Oct 19, 2017
    Posts:
    45
    Okay! So I just had a little RTFM moment for myself and came across this.

    Code (CSharp):
    1. BindingSiteData thisBindingSite = binding[entityB];