#Leveraging Layers and Colliders in ECS without importing Samples code?

1 messages · Page 1 of 1 (latest)

junior venture
#

If I create a GO scene with a falling cube belonging to layer "Players" and set Exclude Layers to "Players" on the mesh of the plane then the cube falls through the plane as expected.
If I create the same scene but in ECS the layers settings have no effect and the cube collides with the plane even though it should not.
Yes yes, I know that I could/should? go for PhysicsShape (and use the Collision Filter here) and PhysicsBody instead of RigidBody and Colliders for Physics in ECS 🙂
This is not really desirable though for the following reasons:

  1. I read between the lines from various ok recent forum posts that the reason why PhysicsShape and PhysicsBody have been moved to the Samples section of Unity Physics package is that they are targeted to be deprecated? (Is this correctly understood?)
  2. I would really prefer not to include Samples project code in our production codebase.

What is the recommended approach as of October 2023?
Should I go with the Samples code PhysicsBody and PhysicsShape until further notice or is there an alternative way of leveraging layers in and ECS world still using RigidBody and "normal" colliders?

junior venture
#

Question is really:
How can I leverage collision filters in ECS on an entity that has only Ridigbody and Collider and not PhysicsBody and PhysicsShape? (I still really want to avoid importing Unity Physics Samples project code in our production code ;-).

Can it be done in a similar way to how one currently have to handle e.g. InverseInertia and InverseMass by making a custom system to bake the values from the Rigidbody to the PhysicsMass component?

I simply cannot find a way to set the BelongsTo and CollidesWith collision filters (are they also only part of the Samples code) and using classic Layers Overrides does not work at all.

junior venture
#

Based on a reply from tangell in Unity forum I managed to get everything working
I ended up creating a CollisionFilterAuthoring component with two custom (flagged enum) properties and called them BelongsTo and CollidesWith which when put on a Prefab will set a TemporaryBakingType on the entity and then a CustomCollisionFilterSystem that picks this up and sets the collision filters on the PhysicsCollider (I set everything is set up to run only when baking systems run)
Of course this bypasses the built-in layers functionality but if you wish to use that, the same setup can easily be adapted to tap into Include Layers, Exclude Layers and GetLayer() of the Prefab and set Collision Filters accordingly.
Link to post: https://forum.unity.com/threads/entities-1-1-experimental-release-now-available.1494323/#post-9404102