- Home /
How to raycast on multi core ECS
I am have trouble trying to replicate the ScheduleBatchRayCast from the ECS documentation. Have been trying for a while and was able to make a burstcomplied version in mono but i want to raycast in ECS on multiple threads. The Errors i get is about the native containers or references, and when i correct one error then i get another error.
Can anyone point out what i am doing wrong and possible how to correct it? Would like to have a working example so i have something i can experiment with.
public class ECSRaycastTesting : SystemBase
{
private BuildPhysicsWorld buildPhysicsWorldSystem;
private NativeList<RaycastInput> raycastInputs;
private NativeList<RaycastHit> raycastHitList;
protected override void OnCreate()
{
buildPhysicsWorldSystem = World.GetExistingSystem<BuildPhysicsWorld>();
}
protected override void OnUpdate()
{
NativeList<RaycastInput> raycastInputs = new NativeList<RaycastInput>(Allocator.TempJob);
NativeList<RaycastHit> raycastHitList = new NativeList<RaycastHit>(Allocator.TempJob);
var collisionWorld = buildPhysicsWorldSystem.PhysicsWorld.CollisionWorld;
var raycastCollection = ScheduleBatchRayCast(collisionWorld, raycastInputs,raycastHitList);
Entities.ForEach((ref ECSRaycastTestingData ecsRaycastTestingData, in Translation translation) =>
{
for (int i = 0; i < ecsRaycastTestingData.raycastAmount; i++)
{
float angle = 36 * i;
float3 startPos = math.mul(quaternion.RotateY(angle), new float3(0, 0, 0.25f));
RaycastInput ray = new RaycastInput()
{
Start = translation.Value + startPos,
End = translation.Value + (startPos + new float3(0, -5, 0)),
Filter = CollisionFilter.Default
};
RaycastHit hit;
collisionWorld.CastRay(ray, out hit);
raycastHitList.Add(hit);
raycastInputs.Add(ray);
};
raycastCollection.Complete();
raycastInputs.Dispose();
raycastHitList.Dispose();
}).Schedule();
buildPhysicsWorldSystem.AddInputDependencyToComplete(Dependency);
}
[BurstCompile]
public struct RaycastJob : IJobParallelFor
{
[ReadOnly] public Unity.Physics.CollisionWorld collisionWorld;
[ReadOnly] public NativeArray<Unity.Physics.RaycastInput> raycastInput;
public NativeArray<Unity.Physics.RaycastHit> raycastHit;
public void Execute(int index)
{
RaycastHit hit;
collisionWorld.CastRay(raycastInput[index], out hit);
raycastHit[index] = hit;
}
}
public static JobHandle ScheduleBatchRayCast(Unity.Physics.CollisionWorld collisionWorld, NativeArray<Unity.Physics.RaycastInput> raycastInput, NativeArray<Unity.Physics.RaycastHit> raycastHit)
{
JobHandle rcj = new RaycastJob
{
raycastInput = raycastInput,
raycastHit = raycastHit,
collisionWorld = collisionWorld
}.Schedule(raycastInput.Length, 10);
return rcj;
}
}
Your answer
Follow this Question
Related Questions
DOTS storing navmesh data in sharedComponent? 1 Answer
[ECS] Move a collider to another collider 1 Answer
Is there a way to use Physics.Raycast to find an Entity? 0 Answers
workaround for parallelizing function calls from Unity API? 0 Answers
[SOLVED] How to set an object's rotation parallel to a wall. 1 Answer