Search Unity

Question DOTS collisions isn't working

Discussion in 'Physics for ECS' started by goldendonut, Sep 1, 2022.

  1. goldendonut

    goldendonut

    Joined:
    Dec 27, 2017
    Posts:
    7
    I have been working on a conveyer system similar to facotrio. but I cant seem to get collision to work.
    if there are any better ways to handle conveyer systems please let me know.

    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. public partial class RETRY : ComponentSystem
    11. {
    12.     private BuildPhysicsWorld buildPhysicsWorld;
    13.     private StepPhysicsWorld stepPhysicsWorld;
    14.  
    15.     private EndSimulationEntityCommandBufferSystem ecb;
    16.  
    17.     protected override void OnCreate()
    18.     {
    19.         base.OnCreate();
    20.         buildPhysicsWorld = World.GetOrCreateSystem<BuildPhysicsWorld>();
    21.         stepPhysicsWorld = World.GetOrCreateSystem<StepPhysicsWorld>();
    22.         ecb = World.GetOrCreateSystem<EndSimulationEntityCommandBufferSystem>();
    23.     }
    24.  
    25.     //[BurstCompatible]
    26.     struct ConveyerSystemJob : ICollisionEventsJob
    27.     {
    28.         [ReadOnly] public ComponentDataFromEntity<ItemComponent1> AllItems;
    29.         public ComponentDataFromEntity<ConveyerComponent> allConveyers;
    30.         [ReadOnly] public ComponentDataFromEntity<ConveyerTag> allTaggedConveyers;
    31.         [ReadOnly] public ComponentDataFromEntity<LocalToWorld> World;
    32.  
    33.         public EntityCommandBuffer ecb;
    34.         public void Execute(CollisionEvent collisonEvent)
    35.         {
    36.             Entity entityA = collisonEvent.EntityA;
    37.             Entity entityB = collisonEvent.EntityB;
    38.  
    39.  
    40.             if (allTaggedConveyers.HasComponent(entityA) && allTaggedConveyers.HasComponent(entityB))
    41.             {
    42.                 return;
    43.             }
    44.  
    45.             if (AllItems.HasComponent(entityA) && allTaggedConveyers.HasComponent(entityB))
    46.             {
    47.                 ConveyerComponent conveyerComp = allConveyers[entityB];
    48.  
    49.                 if (conveyerComp.next != Entity.Null)
    50.                     return;
    51.  
    52.                 conveyerComp.next = entityA;
    53.                 LocalToWorld LWorld = World[entityB];
    54.  
    55.                 ecb.AddComponent(entityA, new ItemConveyerComponent());
    56.                 ItemConveyerComponent New = new ItemConveyerComponent();
    57.  
    58.                 New.distance = 1;
    59.                 New.direction = LWorld.Forward;
    60.  
    61.                 ecb.SetComponent(entityA, New);
    62.                 allConveyers[entityB] = conveyerComp;
    63.             }
    64.             else if (AllItems.HasComponent(entityB) && allTaggedConveyers.HasComponent(entityA))
    65.             {
    66.                 ConveyerComponent conveyerComp = allConveyers[entityA];
    67.  
    68.                 if (conveyerComp.next != Entity.Null)
    69.                     return;
    70.  
    71.                 conveyerComp.next = entityB;
    72.                 LocalToWorld LWorld = World[entityA];
    73.  
    74.                 ecb.AddComponent(entityB, new ItemConveyerComponent());
    75.                 ItemConveyerComponent New = new ItemConveyerComponent();
    76.  
    77.                 New.distance = 1;
    78.                 New.direction = LWorld.Forward;
    79.  
    80.                 ecb.SetComponent(entityB, New);
    81.                 allConveyers[entityA] = conveyerComp;
    82.             }
    83.         }
    84.     }
    85.  
    86.     protected JobHandle OnUpdate(JobHandle inputDeps)
    87.     {
    88.         var job = new ConveyerSystemJob();
    89.         job.AllItems = GetComponentDataFromEntity<ItemComponent1>(false);
    90.         job.allConveyers = GetComponentDataFromEntity<ConveyerComponent>(false);
    91.         job.World = GetComponentDataFromEntity<LocalToWorld>(true);
    92.         job.allTaggedConveyers = GetComponentDataFromEntity<ConveyerTag>(true);
    93.         job.ecb = ecb.CreateCommandBuffer();
    94.  
    95.         JobHandle jobHandle = job.Schedule(stepPhysicsWorld.Simulation, inputDeps);
    96.         //jobHandle.Complete();
    97.         ecb.AddJobHandleForProducer(jobHandle);
    98.         return jobHandle;
    99.     }
    100.  
    101.     protected override void OnUpdate()
    102.     {
    103.         throw new System.NotImplementedException();
    104.     }
    105. }
    that's the code:::

    I got it working with the bare bones of collision and it worked, then I changed bits to this and now it isn't working. I also tried going back to the original code but still isn't working

    added what the conveyer and item are i did scale the conveyer down so it was easier to work with...
     

    Attached Files:

  2. Arnold_2013

    Arnold_2013

    Joined:
    Nov 24, 2013
    Posts:
    287
    Your collision response is on "Collide", but the collisionJob works on the collision Events. So Change the "Collision Response" to "Collide raise collision events".
     
  3. goldendonut

    goldendonut

    Joined:
    Dec 27, 2017
    Posts:
    7
    thanks, but I just figured out it was the code that wasn't working, and I accidentally added the same photo twice.

    Thankyou for responding though...
     
    JMPM-UNITY likes this.