Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Bug Collider.GetChild doesn't return a valid runtime entity

Discussion in 'Physics for ECS' started by Occuros, Nov 17, 2023.

  1. Occuros

    Occuros

    Joined:
    Sep 4, 2018
    Posts:
    299
    The physics collider provides a method to get the ChildCollider when a specific colliderKey is provided.

    The returned entity of the ChildCollider maps to a nonexistent runtime entity.
     
  2. daniel-holz

    daniel-holz

    Unity Technologies

    Joined:
    Sep 17, 2021
    Posts:
    265
    Hi @Occuros .
    Could you please provide the version in which you are experiencing this, and in which context. Are these bake time entities for baked compounds, or runtime entities for manually created compounds?

    We have found just recently that the entities associated with child colliders in compounds that were automatically associated during baking were invalid and only valid by chance. The reason for this was that the bake time entities might get copied to the actual runtime world without maintaining their internal ids and the
    Entity
    entries within the compound colliders are not automatically migrated to these new ids in these situations. In the newer versions of Unity Physics (1.1+) we no longer set the entities for child colliders during baking for that reason.

    Since the Entity field is still around in child colliders within compounds, it is possible however to associate entities with the children when manually creating a compound at runtime using
    CompoundCollider.Create
    . These will remain valid as long as the internal ids of the referenced entities are not changed.

    In order for you to recover the original bake-time entity from a child collider safely you can use the
    PhysicsColliderKeyEntityPair
    buffer which will be located on every baked entity that holds a baked compound collider:
    upload_2023-11-21_12-1-32.png

    These entities are even updated when entities are moved around and their internal id changes. This is true for every Entity field located in
    IComponentData
    and
    IBufferElementData
    structs.
    So, you can also manually add such a buffer to your entities for collider key / entity mapping and benefit from this automatic entity migration.