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:
-
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).
-
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;
}
}
}