Raycast Jobs and multi-threading

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?

/// <summary>
/// Parallel job for casting rays from projectiles using Unity.Physics
/// </summary>
[BurstCompile]
struct ProjectilePhysicsRayJob : IJobForEach<Translation, Projectile>
{
    public NativeArray<Unity.Physics.RaycastHit> raycastResultsInJob;
    [ReadOnly] public Unity.Physics.CollisionWorld collisionWorldInJob;
    [ReadOnly] public float deltaTime;

    public void Execute([ReadOnly] ref Translation position, ref Projectile projectile)
    {
        Unity.Physics.RaycastInput raycastInput = new Unity.Physics.RaycastInput
        {
            Start = position.Value,                   
            End = position.Value + (projectile.velocity * deltaTime),
            Filter = Unity.Physics.CollisionFilter.Default
        };

        if (collisionWorldInJob.CastRay(raycastInput, out Unity.Physics.RaycastHit hit))
        {
            if (hit.RigidBodyIndex == 0) { hit.Position = new float3(1f, 1f, 1f); }

            raycastResultsInJob[projectile._tempIdx] = hit;
        }
        else
        {
            // Dummy hit
            Unity.Physics.RaycastHit raycastHit = new Unity.Physics.RaycastHit();
            raycastHit.RigidBodyIndex = -1;
            raycastResultsInJob[projectile._tempIdx] = raycastHit;
        }
    }
}
  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.