Children with colliders being de-coupled from parent

When I make a prefab with a child that has a collider and instantiate it, the child will become a top-level entity and no longer be a child of the instantiated entity. This is making my strategy for spawning pieces of terrain non-functional. If I spawn a prefab with several child-entities that have colliders, they'll be stuck at 0,0,0 when I try to change the localTransform of the "parent" entity.

Are there any workarounds here? Is this even intended behavior?

This is becoming even more of an impediment to continued progress on my project. I want to be able to instantiate prefabs that include children with colliders. How is this not supported behavior? What am I missing? Thanks.

Hi,

I'm not sure what you are trying to achieve, I had something similar with a destruction plugin I used, but I'm not sure if it is quite the same. You said you plan to spawn pieces of terrain, so I assume that you got a terrain prefab, constituted of multiple part that are children of the prefab root, and that each got their own collider? If this is the correct assumption, you indeed get everything baked in a compound collider.

So my case is similar in the fact that I have one prefab (a pre-fractured mesh), that has plenty of children that represent multiple depths of fracture. Upon destruction, I needed to instantiate the children bodies of the next depth level corresponding to the fracture, each with their own collider. Because I can't store an array of collider in a Blob Asset (because colliders are blob assets already), I just made a huge compound of all my children colliders, and used this unsafe method to get the collider index out of the compound :

public static BlobAssetReference<Collider> GetChildOfCompoundFromIndex(BlobAssetReference<Collider> compoundValue, int index)
{
    var compoundPtr = (CompoundCollider*)compoundValue.GetUnsafePtr();
    var key = compoundPtr->ConvertChildIndexToColliderKey(index);
    compoundValue.Value.GetChild(ref key, out var child);
    return child.Collider->Clone();
}

In my case, my hierarchy was flat (only one level under the root of prefab), and the depth level of the fracture was managed elsewhere, so put your child index in this method to retrieve the collider.

Please, tell me if this helps you, as I'm not sure your problem is similar to mine. And If you need help with the authoring part, feel free to ask.

This is indeed similar to what I'm trying to do. Please post your full solution.

I gave a lot more details in this thread : https://discussions.unity.com/t/910049 . I'm not home right now, I'll post the authoring part when i get back.

I've just read your post on the other thread. Are your rocks dynamics (or have any kind of physic body)? If this is the case, you could try my solution, instantiating the island first, and then instantiating the rocks one by one, at their correct transform by using a rock prefab and assigning the rendering and physic component at runtime. Although in your case, i'm unsure if it will get you a gain over the hack of moving all the rocks manualy after instantiating the island. I'm still away from computer so i can't check right now.

But if your rocks are just statics, there is something I don't understand, because adding a physic body to the root of the prefab should have merged all the rocks (and other island colliders) into a compound collider, that will be on the root entity of the prefab. So moving the island after instantiating it should have moved the colliders.

1 Like

Thank you @Tigrian , however I'm still not sure I fully understand. When I modify the "test + cube" example I gave in the other thread and add a Mesh Collider component to the otherwise generic test gameobject, nothing changes. What exactly should I do to "add a physics body to the root"? A physics body is not a component. Thank you.

Edit: To clarify, my rocks are not dynamic. I just did not know about this root entity trick, and still don't fully understand it. Thanks.

Edit: I found this but I can't see the PhysicsBody or PhysicsBodyAuthoring in my project. Intellisense isn't picking it up at all.

Edit: I also found this writing on compound colliders, but it uses a rigidbody. Is this appropriate for my use case? Either way when I try to do this it doesn't seem to work.


Have you read the parent page?
https://docs.unity3d.com/Packages/com.unity.physics@1.0/manual/custom-samples-physics-components.html

Ah I see. In the samples. I should have guessed. @Spy-Master and @Tigrian . You're both angels.

1 Like