Search Unity

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

Question Destroying converted child objects in ITriggerEventsJob

Discussion in 'Physics for ECS' started by DigitalSwell, Aug 4, 2020.

  1. DigitalSwell

    DigitalSwell

    Joined:
    Aug 18, 2015
    Posts:
    5
    HI All.
    This is my first post and I really hope this can be asked here.
    Otherwise I am sorry for posting in the wrong forum.

    I have a prefab, basically an simple textured FBX crate model (mesh and material).

    Crate has;
    - "converted to entity“ component with "create and destroy"
    - physics body
    - physics shape
    ...



    Upon starting the sample, the crate and mesh get converted to Entities.

    When the collision happens i want to destroy crate and all of its childs. In my case, Empty_Mesh.
    In my case when i destroy Crate the mesh is still there, which makes sense since both are independent entities after convert.

    I am destroying my all of this in „ITriggerEventsJob"
    "EntityCommandBuffer“.DestroyEntity(crate);


    Any hints on how to effectively destroy both entities on collision trigger ?
     

    Attached Files:

  2. petarmHavok

    petarmHavok

    Joined:
    Nov 20, 2018
    Posts:
    461
    Hey, this is a good place to ask these sorts of questions.

    Why do you have 2 entities for this? Did you add physics body and physics shape to both of them? Typically, when we have a hierarchy like this, we add the body on the top level (crate in your example) and add the shape scripts to the children (Empty_Mesh for you). That will create a compound collider with one body and a single entity. So when you get a trigger event on the crate body, and you want to destroy it, it will destroy all children as well. Is this what you would like to achieve?
     
  3. DigitalSwell

    DigitalSwell

    Joined:
    Aug 18, 2015
    Posts:
    5
    Thank you for your answer.
    This is exactly what I want to achieve.

    I had/have physics body, shape and convert script only on parent (crate). Empty_Mesh has no scripts or special DOTS components.

    I tried as you mentioned, added body to the parent (Crate) and shape to the child (Empty_mesh).
    When I Start i still see two Entities. crate and Empty_Mesh.

    My Convert to entity script is added to the parent (Crate). I assume child somehow also gets converted as own Entity.
    I am gone retry maybe there is still something else I did wrong :(
     
    Last edited: Aug 5, 2020
  4. petarmHavok

    petarmHavok

    Joined:
    Nov 20, 2018
    Posts:
    461
    So you can do ConvertToEntity in 2 ways, adding a script or ticking a box at the very top of the inspector. Maybe you are doing that for your child entity?
     
  5. petarmHavok

    petarmHavok

    Joined:
    Nov 20, 2018
    Posts:
    461
    DigitalSwell likes this.
  6. DigitalSwell

    DigitalSwell

    Joined:
    Aug 18, 2015
    Posts:
    5
    I will do so and report back if in general childs are removed once parent is destroyed.

    Thank you for the info.
     
    petarmHavok likes this.
  7. DigitalSwell

    DigitalSwell

    Joined:
    Aug 18, 2015
    Posts:
    5
    Whatever I did the child does not go away if I remove its parent entity. I can for sure move my mesh renderer etc.. to parent object/entity (crate) and remove remove redundand child object (Empty_Mesh). But as soon as I have some child attached to the parent it automatically gets converted to Entity and is not destroyed when destroying its parent.

    I think i found a similar topic and will try it out: (LinkedEntityGroup)

    https://forum.unity.com/threads/des...stroy-childs-same-things-with-disable.829518/
     
  8. petarmHavok

    petarmHavok

    Joined:
    Nov 20, 2018
    Posts:
    461
    I think the root cause of this is the fact that you get 2 entities after conversion. This hierarchy should definitely end up with only one entity if everything is setup correctly.

    If you could share a small project that contains this setup of yours (nothing else is needed), I can take a look.
     
  9. DigitalSwell

    DigitalSwell

    Joined:
    Aug 18, 2015
    Posts:
    5
    I think getting two entities is intended behavior if the parent has ConvertToEntity script.

    The problem in my opinion is that there is no link between Entities when they get created. So I found an article that states that per default no link is created for parent/child Entities when they are created. (that is how I understood it :))

    https://gametorrahod.com/game-object-conversion-and-subscene/

    So to "solve" or at least understand my issue I added following script to my parent;

    Code (CSharp):
    1.  
    2. public class CrateConvert : MonoBehaviour, IConvertGameObjectToEntity
    3.     {
    4.         public void Convert(Entity entity, EntityManager dstManager, GameObjectConversionSystem conversionSystem)
    5.         {
    6.             conversionSystem.DeclareLinkedEntityGroup(this.gameObject);
    7.         }
    8.     }
    9.  
     
  10. papopov

    papopov

    Joined:
    Jun 29, 2020
    Posts:
    32
    I've just checked what happens when we create compound collider - 1 entity for rigid body containing compound collider + 1 entity for each leaf collider. Destroying the parent entity will not destroy child entities by default. There's 2 ways to destroy them:
    1. Manually - each compound collider has a dynamic buffer of it's children - so you would need to go over that buffer and destroy the chilldren
    2. Link them in a group, and then destroy the parent entity
     
    DigitalSwell likes this.