Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question How to add collision event programmatically

Discussion in 'Physics for ECS' started by soundeos, Jan 6, 2022.

  1. soundeos

    soundeos

    Joined:
    Mar 4, 2014
    Posts:
    21
    Hi

    I'm generating my entities with the pure ecs way. Here is the code I'm generating the collider;

    Code (CSharp):
    1. var boxGeometry = new BoxGeometry();
    2.         boxGeometry.Center = mesh.bounds.center;
    3.         boxGeometry.Size = colliderSize;
    4.         boxGeometry.Orientation = quaternion.identity;
    5.         BlobAssetReference<Unity.Physics.Collider> boxCollider = Unity.Physics.BoxCollider.Create(boxGeometry, CollisionFilter.Default);
    6.         manager.SetComponentData(entity, new PhysicsCollider
    7.         {
    8.             Value = boxCollider
    9.         });
    It's working fine with raycasting. But I'm not able to trigger the collision events. Here is my collision system;

    Code (CSharp):
    1. public class TestCollisionSystem : JobComponentSystem
    2. {
    3.     private BuildPhysicsWorld buildPhysicsWorld;
    4.     private StepPhysicsWorld stepPhysicsWorld;
    5.  
    6.     protected override void OnCreate()
    7.     {
    8.         buildPhysicsWorld = World.GetOrCreateSystem<BuildPhysicsWorld>();
    9.         stepPhysicsWorld = World.GetOrCreateSystem<StepPhysicsWorld>();
    10.     }
    11.  
    12.     [BurstCompile]
    13.     struct TestCollisionSystemJob : ICollisionEventsJob
    14.     {
    15.         [ReadOnly] public ComponentDataFromEntity<CarInfo> carInfoGroup;
    16.  
    17.         public void Execute(CollisionEvent collisionEvent)
    18.         {
    19.             Entity entityA = collisionEvent.EntityA;
    20.             Entity entityB = collisionEvent.EntityB;
    21.  
    22.             Debug.Log(entityA);
    23.             Debug.Log(entityB);
    24.         }
    25.     }
    26.  
    27.     protected override JobHandle OnUpdate(JobHandle inputDeps)
    28.     {
    29.         var job = new TestCollisionSystemJob();
    30.         job.carInfoGroup = GetComponentDataFromEntity<CarInfo>(true);
    31.  
    32.         JobHandle jobHandle = job.Schedule(stepPhysicsWorld.Simulation, ref buildPhysicsWorld.PhysicsWorld, inputDeps);
    33.         return jobHandle;
    34.     }
    35. }
    What is missing here? What component do I need to add while generating the entities?
     
  2. TRS6123

    TRS6123

    Joined:
    May 16, 2015
    Posts:
    246
    You have to pass a Material into the Create method with a CollisionResponse set to "CollideRaiseCollisionEvents"
     
  3. NT_Ninetails

    NT_Ninetails

    Joined:
    Jan 21, 2018
    Posts:
    195
  4. soundeos

    soundeos

    Joined:
    Mar 4, 2014
    Posts:
    21
    I've added the material but still not working.

    Code (CSharp):
    1. var physicsMaterial = new Unity.Physics.Material
    2.         {
    3.             CustomTags = Unity.Physics.Material.Default.CustomTags,
    4.             CollisionResponse = CollisionResponsePolicy.CollideRaiseCollisionEvents
    5.         };
    6.  
    7. var boxGeometry = new BoxGeometry();
    8.         boxGeometry.Center = mesh.bounds.center;
    9.         boxGeometry.Size = colliderSize;
    10.         boxGeometry.Orientation = quaternion.identity;
    11.  
    12.  
    13.         BlobAssetReference<Unity.Physics.Collider> boxCollider = Unity.Physics.BoxCollider.Create(boxGeometry, CollisionFilter.Default , physicsMaterial);
    14.  
    15.         manager.SetComponentData(entity, new PhysicsCollider
    16.         {
    17.             Value = boxCollider
    18.         });
     
  5. NT_Ninetails

    NT_Ninetails

    Joined:
    Jan 21, 2018
    Posts:
    195
     
  6. NT_Ninetails

    NT_Ninetails

    Joined:
    Jan 21, 2018
    Posts:
    195
    in the link:
    Did you try this?

     
  7. soundeos

    soundeos

    Joined:
    Mar 4, 2014
    Posts:
    21
    Yes I did. But not working.
     
  8. NT_Ninetails

    NT_Ninetails

    Joined:
    Jan 21, 2018
    Posts:
    195
    Try getting rid of the [BurstCompile] tag in your job and disable Burst Compilation in your project and try again.