RaycastCommand doesn't hit entities
The RaycastCommand job only hits on UnityEngine.Physics collider, but not on Unity.Physics collider. How do I cast rays against DOTS physic entities? Or am I just missing something?
protected override void OnUpdate()
{
// stores amount of queried entities
int dataCount = query.CalculateEntityCount();
// concurrent command buffer for use in parralel jobs
var commandBuffer = bufferSystem.CreateCommandBuffer().ToConcurrent();
Vector3 playerPos = PlayerController.player.position;
// Allocating native arrays to store data inbetween jobs
NativeArray<UnityEngine.RaycastHit> results = new NativeArray<UnityEngine.RaycastHit>(dataCount, Allocator.TempJob);
NativeArray<RaycastCommand> raycastCommand = new NativeArray<RaycastCommand>(dataCount, Allocator.TempJob);
NativeArray<bool> hit = new NativeArray<bool>(dataCount, Allocator.TempJob);
// building raycast commands based on selected entities
JobHandle jobHandle = Entities.WithStoreEntityQueryInField(ref query) // this stores the amount of affected entities and adjusts the native arrays
.ForEach((Entity entity, int entityInQueryIndex,ref Follow follow, in Translation translation, in EnemyType type, in InRange inRange) =>
{
Vector3 origin = (Vector3)translation.Value + Vector3.up;
Vector3 direction = (playerPos) - ((Vector3)translation.Value + Vector3.up);
direction = direction.normalized;
raycastCommand[entityInQueryIndex] = new RaycastCommand(origin, direction,500);
}).ScheduleParallel(Dependency);
JobHandle handle = RaycastCommand.ScheduleBatch(raycastCommand, results, results.Length, jobHandle);
// Wait till raycasts are done because they must be processed on the main thread
handle.Complete();
// Handling raycast results on the main thread
for(int i = 0; i< dataCount;i++)
{
if(results[i].collider == null)
{
hit[i] = true;
}
else
{ hit[i] = false; }
Debug.Log(results[i].collider);
}
// Adding Components to entities
JobHandle addComponent = Entities
.ForEach((Entity entity, int entityInQueryIndex,ref Follow follow, in InRange inRange) =>
{
if (hit[entityInQueryIndex])
{
commandBuffer.SetComponent<Follow>(entityInQueryIndex, entity, new Follow { style = 1});
}
else
{
commandBuffer.SetComponent<Follow>(entityInQueryIndex, entity, new Follow { style = 0 });
}
}).ScheduleParallel(handle);
addComponent.Complete();
// Dispose Native Arrays
results.Dispose();
raycastCommand.Dispose();
hit.Dispose();
// Buffer system depends on job "addComponent"
bufferSystem.AddJobHandleForProducer(addComponent);
}
Comment
Your answer
Follow this Question
Related Questions
How to raycast object as far as possible, without collision? 0 Answers
How to Destroy a PhysicsJoint Object in new Unity.Physics? 0 Answers
raycast gets wrong layer 2 Answers
RayCast(Ray ray, out RaycastHit hitInfo, float maxDistance, int layerMask) question 0 Answers
Everything ignore my path(Layer and Tag) 0 Answers