Parallel Raycast/Collider Cast

So, I’m new to DOTS,
What I’ve is a component on a 100 entities with the distance and direction I want to raycast in and they store the result as two float3 (normal and position)
As stated in documentation it’s best to execute the raycast queries in parallel And the best solution for this that I found was this (from documentation):

  [BurstCompile]
    public struct RaycastJob : IJobParallelFor
    {
        [ReadOnly] public CollisionWorld world;
        [ReadOnly] public NativeArray<RaycastInput> inputs;
        public NativeArray<RaycastHit> results;

        public unsafe void Execute(int index)
        {
            RaycastHit hit;
            world.CastRay(inputs[index], out hit);
            results[index] = hit;
        }
    }

    public static JobHandle ScheduleBatchRayCast(CollisionWorld world,
        NativeArray<RaycastInput> inputs, NativeArray<RaycastHit> results)
    {
        JobHandle rcj = new RaycastJob
        {
            inputs = inputs,
            results = results,
            world = world

        }.Schedule(inputs.Length, 4);
        return rcj;
    }

I understand how this code works
But as I’m new I don’t understand how to actually implement this job(How do I pass all RaycastInput and even though I pass them and create this job how do I store the result back in the components) so that I can schedule the raycast queries for my components(I’m basically asking an example using this code)

There is an example of how to use such a job here: Manual iteration | Entities | 0.10.0-preview.6 Good luck!

1 Like

Is this the most efficient way?

An IJobChunk (Using IJobChunk jobs | Entities | 0.10.0-preview.6) is probably more efficient (but you may want to measure that, as always).