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.

Improvements for Batch Queries

Discussion in 'Physics Previews' started by albert-jurkoit, Feb 14, 2022.

  1. albert-jurkoit


    Unity Technologies

    Mar 7, 2017
    Hey everyone!

    We have been working on upgrading batch queries and their usability, and we would like to share an early access to it:

    Here are the things that were changed:
    • maxHits property was moved to the ScheduleBatch method. Now you can have a job for setting up commands without touching the main thread.

    • maxHits now properly works with all batch commands. Additionally, it’s guaranteed that you will get the closest hit if you set maxHits to 1.

    • We enabled support of multiple hits per 1 mesh.

    • We have introduced a new struct called QueryParameters for an easier and more configurable way of setting up commands.

    • We also changed signatures for all command structs:
    Old signature of RaycastCommand:

    RaycastCommand(Vector3 from, Vector3 direction, float distance, int layerMask, int maxHits)

    New signature:

    RaycastCommand(Vector3 from, Vector3 direction, QueryParameters queryParameters, float distance).
    • QueryParameters struct
      • LayerMask was moved to this structure.

      • Additionally we now support different options for batch queries:
        • Hit triggers - whether triggers return a hit.

        • Hit backfaces - whether backfaces return a hit..

        • Multiple hits per mesh - whether a single mesh can return multiple hits.
      • QueryParameters.Default is available for easier use with default values.

    We are attaching a demo project we’ve prepared for you to showcase how new batch queries are functioning.

    Also, would like to mention that this feature is going to be a part of the alpha release and will be available in 2022.2.0a6 and up.

    Your feedback is greatly appreciated!

    Attached Files:

    Last edited: Feb 14, 2022
    makaka-org, rdjadu, ohbado and 8 others like this.
  2. Nirlah


    May 6, 2018
    Are there any plans for adding more commands besides Raycast (like Overlap)?
    Or will the only possibility will be to use the ECS physics package for other commands?
    yant likes this.
  3. albert-jurkoit


    Unity Technologies

    Mar 7, 2017
    Hey @Nirlah,

    Thanks for the question! Yes, adding Overlaps to batch queries is in our backlog. But so far, can't tell what is the ETA for this one.
    Nirlah likes this.
  4. albert-jurkoit


    Unity Technologies

    Mar 7, 2017
    Would like to give an update on overlaps. OverlapBox, OverlapCapsule, and OverlapSphere were added to batch queries! They share the same maxHits logic as RaycastCommand. The Batch version of Overlaps should be available in 2022.2.0a12 and up!
  5. Cloudwalker_


    Jan 3, 2014
    Will this be ported to 2021? Could really use this!
  6. albert-jurkoit


    Unity Technologies

    Mar 7, 2017
    Hey @Cloudwalker_ ,

    Unfortunately, public API changes or any bigger feature works are usually not backported to any previous releases.
  7. Speeedy171


    Sep 2, 2018
    Hey everyone,

    I was wondering if anyone had a fast way to read the results of an Overlap batch query. Here is my go at it, but it seems quite slow:

    Code (CSharp):
    1. for (int i = 0; i < commands.Length; i++) {
    2.     for (int j = 0; j < MAX_HITS; j++) {
    3.         int index = (i * MAX_HITS) + j;
    4.         if (results[index].collider == null) break;
    5.         if (results[index].collider.gameObject != _agents[i].gameObject) {
    6.             Debug.DrawRay(_agents[i].transform.position, results[index].collider.transform.position - _agents[i].transform.position,;
    7.         }
    8.     }
    9. }
    Profiler with Debug.DrawRay:
    With DrawRay.png

    Profiler without Debug.DrawRay:
    Without DrawRay.png

    Profiler without any reading of the array (all lines above commented out):
    No Reading Array.png

    Thanks for any help!
    Last edited: Sep 12, 2023