Convert-to-Entity automatically makes CompoundColliders??

Edit - I found the problem. I found a mesh collider hidden in one of the child objects.

Why is the Convert-to-Entity script converting the physics shape that is set to BoxCollider into a CompoundCollider??

My goal is to fetch the box collider at runtime and read some of the values from it (like size/center/filter). I thought this would be easy, but it’s not.

I setup a prefab with Convert-to-Entity like this…

And then setup a system with some code like this:

Entities.WithReadOnly(world).ForEach((...selectors..., in PhysicsCollider collider) => {
    UnityEngine.Debug.Log(collider.ColliderPtr->Type);
}).Run();

It’s static, so I didn’t add a physics body.

The entity debugger shows that a PhysicsCollider exists for the entity I’m querying:
6356604--707016--upload_2020-9-28_1-19-40.png

This prints “Compound”… I was expecting it to print “Box”.

I tried typecasting the ColliderPtr to Unity.Physics.BoxCollider* even though it’s listed as Compound, but it seems like the values in it are junk… for the screenshot above it reports size as (0, -0.1, -0.1), when I expect it to be (4, 12, 4).

Is it not possible to access a collider that was generated via the Convert-to-Entity script, or am I just doing something stupid?

Does it have a parent in the hierarchy? If that’s the case, that is why you are getting a compound collider. It was meant to work for a case where you set a parent and then a number of children that all have individual colliders. If you have a parent and a single child you will get a compound with that one child.

2 ways for this to work: either remove the parent and have your box collider at the top level, or use compound collider’s GetChild() to get your ChildCollider that you can safely cast to BoxCollider.

For the latter, you need a ColliderKey. It needs 2 things in the PushSubKey() method, numSubKeyBits that you can get from CompoundCollider.NumColliderKeyBits, and a subKey, which is 0 in your case (only child).

1 Like

Thanks for the response petarmHavok. It does not have a parent in the hierarchy.

There is only one “Physics Shape” in the entire prefab, and it is at the root level.

All of the children have a mesh renderer and a mesh filter. Some of them have attached scripts. I double and triple checked - the ONLY physics shape is at the root level, and it is set to Box.

1 Like

Maybe that is normal behavior then?

Is this a true statement? : If a prefab with a box collider physics shape on the root object has one or more children without colliders, then the box collider is converted to a compound collider automatically.

If that is true, then is there any way to force the conversion to keep the box collider instead of turning it into a compound? I suppose I could use the ChildCollider thing, but a compound collider seems really hacky and less performant when there is no need for a compound collider.

<------- This guy is stupid.

Ok… so… uh… this is really embarrassing… I screwed up. I found a mesh collider hiding in one of the child objects.

while (1) { BangHead(here); }

2 Likes

So glad you figured it out, after the first post where the box was at the parent I was out of ideas! :slight_smile:

2 Likes