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.

Question Raycast Jobs and multi-threading

Discussion in 'Physics for ECS' started by sstrong, Mar 26, 2021.

  1. sstrong


    Oct 16, 2013
    I have a projectile system (which does many things) including Unity.Physics ray casting.

    I'm attempting to upgrade it from an earlier version. Currently I have a IJobForEach which seems to be running across multiple threads (which is good).

    A few questions:
    1. How can I update the IJobForEach to a more "modern" solution and yet retain all the flexibility I have in the current ProjectileSystem? (It currently also interacts with the gameobject world - it is not purely DOTS).

    2. The profiler capture below is running on a 6-core (12 logical processors) PC. Should I expect better utilization of the logical processors (currently only using 4 worker threads) or is the system smart enough to know that really only the physical cores will make any material difference to overall performance? Is thread scheduling happening inside Unity or is it delegating this task to the OS?

    Code (CSharp):
    1. /// <summary>
    2. /// Parallel job for casting rays from projectiles using Unity.Physics
    3. /// </summary>
    4. [BurstCompile]
    5. struct ProjectilePhysicsRayJob : IJobForEach<Translation, Projectile>
    6. {
    7.     public NativeArray<Unity.Physics.RaycastHit> raycastResultsInJob;
    8.     [ReadOnly] public Unity.Physics.CollisionWorld collisionWorldInJob;
    9.     [ReadOnly] public float deltaTime;
    11.     public void Execute([ReadOnly] ref Translation position, ref Projectile projectile)
    12.     {
    13.         Unity.Physics.RaycastInput raycastInput = new Unity.Physics.RaycastInput
    14.         {
    15.             Start = position.Value,                  
    16.             End = position.Value + (projectile.velocity * deltaTime),
    17.             Filter = Unity.Physics.CollisionFilter.Default
    18.         };
    20.         if (collisionWorldInJob.CastRay(raycastInput, out Unity.Physics.RaycastHit hit))
    21.         {
    22.             if (hit.RigidBodyIndex == 0) { hit.Position = new float3(1f, 1f, 1f); }
    24.             raycastResultsInJob[projectile._tempIdx] = hit;
    25.         }
    26.         else
    27.         {
    28.             // Dummy hit
    29.             Unity.Physics.RaycastHit raycastHit = new Unity.Physics.RaycastHit();
    30.             raycastHit.RigidBodyIndex = -1;
    31.             raycastResultsInJob[projectile._tempIdx] = raycastHit;
    32.         }
    33.     }
    34. }
  2. milos85miki


    Nov 29, 2019
    1. Can you use IJobParallelFor/IJobParallelForDefer? Not sure what you're doing with GO, but would suggest asking in DOTS subforum (parent of DOTS Physics).
    2. If you have Jobs->Use Job Threads ticked in the editor and are not overriding worker thread count in editor startup params, Unity should have 12 worker threads at disposal. What is actually used also depends on the current payload, so you shouldn't have to worry about it.

    Please let me know if you need any further help.