Search Unity

  1. We are migrating the Unity Forums to Unity Discussions by the end of July. Read our announcement for more information and let us know if you have any questions.
    Dismiss Notice
  2. Dismiss Notice

Question How to make collider created with BoxCollider.Create in baking system not unique

Discussion in 'Physics for ECS' started by Tigrian, Mar 27, 2024.

  1. Tigrian


    Mar 21, 2021

    I have a set of wheels with classic gameobject sphere colliders.
    I would like to swap the collider to another primitive (box or cylinder) during editor time but keep collider sharing for wheels with same radius. The swapping choice is made on a singleton entity, that would swap all the sphere colliders wheels to the collider I want.

    In a baking system, I retrieve the blob asset store, create a custom hash based on my data (radius mainly), and TryGet the blob collider with the custom hash. If it does not exist, I create a collider with BoxCollider/CylinderCollider, and add it to the blob asset store with TryAdd.
    Code (CSharp):
    1. case PhysicWheelTypeAuthoring.WheelColliderType.Cylinder8:
    2.     if (collider.ValueRO.Value.Value.Type != ColliderType.Cylinder)
    3.     {
    4.         if (blobAssetStore.TryGet(hash, out BlobAssetReference<Collider> blob))
    5.         {
    6.             collider.ValueRW.Value = blob;
    7.         }
    8.         else
    9.         {
    10.             var blobAssetReference = CylinderCollider.Create(new()
    11.             {
    12.                 Center =,
    13.                 Radius = baker.ValueRO.Radius,
    14.                 Height = baker.ValueRO.Radius/2f,
    15.                 BevelRadius = 0.05f,
    16.                 Orientation = quaternion.RotateY(90f * math.TORADIANS),
    17.                 SideCount = 8
    18.             });
    19.             blobAssetStore.TryAdd(hash, ref blobAssetReference);
    20.             collider.ValueRW.Value = blobAssetReference;
    21.         }
    22.     }
    23.     break;
    But when I inspect my colliders in the entity hierarchy, they indeed share the same blob hash, but are all marked as unique. How can I keep collider sharing in this scenario?