What is the proper way of instantiating an entity prefab with child physics bodies?

Honestly I am not sure what happens with joints as child entities. When baking this doesn’t happen unless I am mistaken.
Maybe because they are children of a dynamic entity, they automatically also obtain transformation components, but that would surprise me when runtime created.
The transforms on joint entities are not considered. So moving them or not makes no difference.

As to where to do your transformation or instantiation, I suggest the AfterPhysicsSystemGroup. That’s a safe moment for pretty much anything physics related. Alternatively, the BeforePhysicsSystemGroup would work as well I think.

So we should do something like this?

LocalTransform parentTransform = LocalTransform.FromPosition(x * blockSize, 0, y * blockSize);
Entity blockEntity = state.EntityManager.Instantiate(prefab);

state.EntityManager.SetComponentData(blockEntity, parentTransform);

var elements = state.EntityManager.GetBuffer<LinkedEntityGroup>(blockEntity);

foreach(var element in elements) {
    if (element.Value != blockEntity && !state.EntityManager.HasComponent<Parent>(element.Value)) {
        var childTransform = state.EntityManager.GetComponentData<LocalTransform>(element.Value);

        state.EntityManager.SetComponentData(element.Value, childTransform.TransformTransform(parentTransform));
    }
}

Edit: fixed by Alejandro, i was missing the part where i have to check if the entity is linked to a parent.
ty!!

1 Like

Great! I’m glad it worked.
And many thanks for sharing the working code for others which have the same question to benefit from.