I have a PhsyicsShape on a prefab which has ConvertToEntity with the following selected for BelongsTo
When trying to use a collision filter, in a raycast, the hit result of that object always has first bit set (irrespective of selection, making entry 0 useless?)
var raycastInput = new RaycastInput
{
Start = mouseRay.origin,
End = mouseRay.GetPoint(_farClipPlane),
Filter = new CollisionFilter
{
CollidesWith = 1u << 0,
BelongsTo = 1u << 0,
}
};
physicsWorld.CastRay(raycastInput, out var hit);
The above gets set as 5 (0b00101) rather than what I would expect which is 4 (0b0100).
Checking/Unchecking entry 0, looks like it makes no difference to the filter, no matter what bit 0 is always set.
Not sure if this is expected behaviour and I just couldn’t find it anywhere in the docs? (I am new to Unity)
I am using Unity 2020.1.11f1 and Unity Physics 0.5.1-preview.2
This is really strange, especially since you have another place where things work fine. Do the default physics categories work fine for you (PhysicsCategoryNames)? Like the ones you would see by default in the samples project.
Edit: to answer your question, it’s normal to have values 1, 2, 4, 8, … so it’s definitely an issue if you have 0 always set.
The problem seems to be due to the fact that the Cube has both a legacy BoxCollider and a PhysicsShape component.
This means for your Cube body, you are actually creating a CompoundCollider with 2 BoxColliders as children - one from the legacy BoxCollider and setting the CollisionFilter.BelongsTo based on the gameObject.layer, and the second from the PhysicsShape component with your own settings that you are expecting.
At the top level the CompoundCollider combines the CollisionFilters of all the children into a single Filter for the compound, which is why you are seeing the first bit set (from the gameObject.layer) and the other bits from the BelongsToCatergories.
Just ran into this and spent a few hours debugging it. Would be nice if there was some kind of warning message in the physics shape inspector letting you know that you’re basically doubling up your collider and messing up your filters by having the legacy collider on there too
Adding multiple colliders to one Body is a valid use-case though. If you get any of the children from the compound collider, they will have the expected filters on any single one of them. The problem is that the filter at the root is a combination of all the child filters. A warning could mention the mixing of legacy and dots colliders but it isn’t actually a problem.