Search Unity

ICollisionEventsJob Not Firing on entities with a dynamically created physics collider

Discussion in 'Physics for ECS' started by Ender_7, Aug 30, 2020.

  1. Ender_7

    Ender_7

    Joined:
    Nov 22, 2015
    Posts:
    11
    Im dynamically spawning a bunch of entities which I am dynamically resizing.
    In order to affect the colliders scale i am making a new Physics collider to represent the new size using:

    Code (CSharp):
    1. BlobAssetReference<Unity.Physics.Collider> collider = Unity.Physics.SphereCollider.Create(new SphereGeometry
    2. {
    3.         Center = float3.zero,
    4.         Radius = radius
    5. });
    6.                
    7.                 commandBuffer.SetComponent(entityInQueryIndex, instantiatedEntity, new PhysicsCollider { Value = collider});
    However when i do this i no longer have my CollisionEventsJob fire. (They fire properly if i dont set the PhysicsCollider component)

    I assume this is because the collision response is not set to raise collision events on the dynamically instantiated PhysicsCollider.

    Is there a way to set the collision response dynamically? Or could this be caused by some other data that i need to set on the PhysicsCollider?

    Thanks.
     
  2. Ender_7

    Ender_7

    Joined:
    Nov 22, 2015
    Posts:
    11
    Of course I manage to resolve my issue after I post about it...
    I found that I can assign a physicsMaterial in the Create() method. So i made a new one and set its collisionResponsePolicy and now my events are firing.
    Fixed code:
    Code (CSharp):
    1. Unity.Physics.Material physMat = Unity.Physics.Material.Default;
    2.                 physMat.CollisionResponse = CollisionResponsePolicy.CollideRaiseCollisionEvents;
    3.                 BlobAssetReference<Unity.Physics.Collider> collider = Unity.Physics.SphereCollider.Create(new SphereGeometry
    4.                 {
    5.                     Center = float3.zero,
    6.                     Radius = radius
    7.                 }, CollisionFilter.Default, physMat);
    8.                
    9.                 commandBuffer.SetComponent(entityInQueryIndex, instantiatedEntity, new PhysicsCollider { Value = collider});
     
    Egad_McDad and petarmHavok like this.