Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Join us on March 30, 2023, between 5 am & 1 pm EST, in the Performance Profiling Dev Blitz Day 2023 - Q&A forum and Discord where you can connect with our teams behind the Memory and CPU Profilers.
    Dismiss Notice

Batching queries

Discussion in 'Physics for ECS' started by Tony_Max, Nov 5, 2019.

  1. Tony_Max


    Feb 7, 2017
    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:
    1. 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()
    2. 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?)
    Last edited: Nov 5, 2019
  2. Tony_Max


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


    Mar 19, 2019
    Yeah, you can do this sort of thing to minimally make up unsafe sections
    Code (CSharp):
    1.             CollisionFilter filter = CollisionFilter.Default;
    2.             unsafe { filter = MyPhysicsColliderComponent.ColliderPtr->Filter; }
    3.             // use filter
    Tony_Max likes this.