This section of the Physics docs shows how to modify a CollisionFilter at runtime:
Core components | Unity Physics | 0.51.1-preview.21 see "
Modifying PhysicsCollider".
I have two jobs, one that sets a new CollisionFilter and one that plays back the result in the console:
Entities
.WithAll<UnitNeedsSetup>()
.ForEach((Entity entity, int entityInQueryIndex, ref PhysicsCollider collider, in Unit unit) =>
{
collider.Value.Value.Filter = new CollisionFilter()
{
BelongsTo = unit.team == 0 ? 1u << 0 : 1u << 10,
CollidesWith = ~0u
};;
Debug.Log("[assignment] team " + unit.team + " assigned " + collider.Value.Value.Filter.BelongsTo) ;
}).Run();
Entities
.WithAll<UnitNeedsSetup>()
.ForEach((Entity entity, int entityInQueryIndex, in PhysicsCollider collider, in Unit unit) =>
{
Debug.Log("[playback] team " + unit.team + " assigned " + collider.Value.Value.Filter.BelongsTo) ;
}).Run();
Results in the output:
[assignment] team 0 assigned 1
[playback] team 1 assigned 1
[playback] team 0 assigned 1```
Why is the collision filter not remaining set? Also, if I set:
```BelongsTo = 1u << 10;```
It seems to stick.
SOLVED EDIT: This seems to be because my colliders are not unique. Checking the "Force Unique" checkbox in the Physics Shape component does not work for runtime: https://discussions.unity.com/t/780547
I need to generate a brand new collider on the component when it is instantiated, something like:
var newCollider = new PhysicsCollider
{Value = CapsuleCollider.Create(capsuleGeo, spawner.team == 0 ? collisionFilterTeamOne : collisionFilterTeamTwo)};
commandBuffer.SetComponent(entityInQueryIndex, instance, newCollider);