Hello everyone, I’m building an ECS system for my game and facing a major performance issue on a Galaxy A12 mobile device. When I move a sphere collider that collides with about 200 other objects and pulls them toward its center, everything runs fine in the editor. However, the profiler on the device shows that each frame takes around 600ms.
I checked the profiler and my jobs are working fine with time <= 1.01ms however NarrowPhase:ParallelCreateContantsJob is taking 724.36ms. Is this a system bug of Unity physics ECS? How can I solve it?
Hint 1:

Hint 2:
Check the profiler’s timeline view
Hint 3 so I’m a little more useful:
Please show the Profiler in Timeline view, not Hierarchy view.
Here’s what I think is happening:
- The game slows down (probably because of the large collision count)
- Because the previous frame took a long time, in the next frame the game needs to run physics systems multiple times to “catch up”
- Because the physics update had to be run multiple times, the frame took even longer to calculate, which means the next “catch up” will take even longer
In other words, your mobile device isn’t fast enough to run this. I would try to optimize the physics simulation. Colliding with 200 objects doesn’t sound like a great idea, can it be avoided? Maybe use a faster collider type if possible (e.g. avoid mesh colliders)? Are there physics settings you can tweak?
My collisions are all necessary. This is my current setting, and I have also tried changing various other settings, but none of them worked well.
I have an idea: instead of checking for physical collisions, I will write a system to check the positions of objects within the radius of a sphere collider and handle them manually. I think this will help me reduce unnecessary collisions.
After some testing i found out that my game is slowing down due to the layer transfer function of entities every time there is a collision. i need to change the layer of entities to change the way they collide with the environment. however i have not found any documentation that guides how to change the layer of entities in an optimal way. Below is my layer change function
[BurstCompile]
private static unsafe void UpdateColliderFilter(ref ComponentLookup<PhysicsCollider> colliderLookupLocal,
in EntityCommandBuffer.ParallelWriter ecb,
Entity entity, int entityInQueryIndex,
uint newLayer, uint colliderWith)
{
var collider = colliderLookupLocal[entity];
if (!collider.Value.IsCreated)
return;
int size = collider.Value.Value.MemorySize;
var newColliderPtr = (Unity.Physics.Collider*)Unity.Collections.LowLevel.Unsafe.UnsafeUtility.Malloc(size, 16, Allocator.Temp);
Unity.Collections.LowLevel.Unsafe.UnsafeUtility.MemCpy(newColliderPtr, collider.Value.GetUnsafePtr(), size);
var newFilter = newColliderPtr->GetCollisionFilter();
newFilter.BelongsTo = newLayer;
newFilter.CollidesWith = colliderWith;
newColliderPtr->SetCollisionFilter(newFilter);
var newCollider = BlobAssetReference<Unity.Physics.Collider>.Create(newColliderPtr, size);
Unity.Collections.LowLevel.Unsafe.UnsafeUtility.Free(newColliderPtr, Allocator.Temp);
collider.Value = newCollider;
ecb.SetComponent(entityInQueryIndex, entity, collider);
}
Looks like you’re creating a new collider every time you want to change the collision filter. (It’s a bit unfortunate that the collision filter is stored in the collider blob asset, hopefully this improves in the future.)
Instead of instantiating the colliders you can try forcing them to be unique so they’re baked separately:
Now you should be able to modify the collision filter directly without copying the blob asset:
Thank you very much. i will try it
i tried the code you mentioned but it seems it no longer works in entities version 1.3.10
What is the error? You should be able to do something very similar with the latest version of Unity Physics.
For some useful info on how layers and collision filters work in Unity Physics, have a look at the documentation:
Also, you could check out relevant sample scenes in the PhysicsSamples project:
The sample scene “5g4. Runtime Collision Filter Modification.unity” demonstrates how to modify collision filters at runtime, which seems to be what you are looking for:

Thank you! for the share how to change collision filter in runtime! ![]()



