Search Unity

  1. We are migrating the Unity Forums to Unity Discussions by the end of July. Read our announcement for more information and let us know if you have any questions.
    Dismiss Notice
  2. Dismiss Notice

Feature Request Add `ColliderHit.transformId` and `RaycastHit.transformId`.

Discussion in 'C# Job System' started by Enderlook, Apr 23, 2024.

  1. Enderlook


    Dec 4, 2018
    When using the Unity jobs physics APIs like `OverlapSphereCommand`, they return a `ColliderHit` or `RaycastHit`, and if you want to mutate (or read) their transform position in parallel you have to do a lot of steps:
    1) Materialize (convert the id into an object, potentially allocating GC if it's the first time the collider object is requested) the collider into a C# Collider object. Which is a Unity call into native code.
    2) Call de `Collider.transform` property, which also materializes the Transform and is a call to native code.
    3) Use `TransformAccessArray.Add` to append the transform to a job that will read/write the values.
    From these steps, the three of them can only be done in the main thread, and to make it worse, the first 2 can't be Burst compiled.
    Allowing us to avoid some of these steps would be great.

    Having a property like `Collider.transformId` and `RaycastHit.transformId` would be super useful, as we could do `TransformAccessArray.Add(colliderHits.transformId)`, which avoids 2 materializations, 1 native calls, and since everything is unmanaged code, we can Burst compile it.

    And it shouldn't be complex to implement on the Unity side either, I mean, they already have that information on their side. They just have to call the appropriate methods in the native part to avoid the bridge cost between both sides and allow us to use Burst.
    gshape likes this.