Batching queries

Maybe i asking stupid question as always, so sorry

In my project NPC must be able to see another NPC'c. To implement vision i use unity physics distance query. But different NPC'c belongs to different physics categories (layers). Some time NPC may need to "see" 0 layer for one algorithm, sometime 1 layer for another, sometime both.

I see 2 possible approaches:

  • Make just one physics query inside some "VisionSystem" and setting the collision filter from outside of that system depending on what NPC need to see. As a result i have raw Entity collection that i need to sort (cause as i say entities will be used for different algorithms) using ComponentDataFromEntity.Exists()
  • Make physics query in every algorithm that need too "see" entities and have no centralized "VisionSystem".

As far as I can judge from testing, centralized query is more perfomant. But the more important reason why i want to use 1st approach is i have no need to write boilerplates with injecting all physics instances that i need like collision world and so on. Also, all the code for vision is in one place.

Is there a way to somehow sort result hits by physics categories? (using collectors?)

I see now, that i can access CollisionFilter data through RigidBody.Collider*, but it requires unsafe code(

Yeah, you can do this sort of thing to minimally make up unsafe sections

            CollisionFilter filter = CollisionFilter.Default;
            unsafe { filter = MyPhysicsColliderComponent.ColliderPtr->Filter; }
            // use filter
1 Like