protected override JobHandle OnUpdate(JobHandle inputDeps) { int count = DetectHitQuery.CalculateEntityCount(); if (count == 0) { return(inputDeps); } var resultsArray = new NativeArray <RaycastHit>(count, Allocator.TempJob); NativeArray <RaycastCommand> commands = new NativeArray <RaycastCommand>(count, Allocator.TempJob); var setupCommandsJob = new SetupCommandsJob { prevPositions = DetectHitQuery.ToComponentDataArray <PreviousTranslation>(Allocator.TempJob), curPositions = DetectHitQuery.ToComponentDataArray <Translation>(Allocator.TempJob), rotations = DetectHitQuery.ToComponentDataArray <Rotation>(Allocator.TempJob), cmds = commands, hitMask = Parameters.GetProjectileHitLayer() }.Schedule(count, 1, inputDeps); RaycastCommand.ScheduleBatch(commands, resultsArray, 32, setupCommandsJob).Complete(); var entities = DetectHitQuery.ToEntityArray(Allocator.TempJob); var ownerIDs = DetectHitQuery.ToComponentDataArray <OwnerID>(Allocator.TempJob); var projectiles = DetectHitQuery.ToComponentDataArray <Projectile>(Allocator.TempJob); HitHandlerData handlerData; for (int i = 0; i < resultsArray.Length; i++) { if (resultsArray[i].collider != null) { handlerData = new HitHandlerData { Entity = entities[i], ProjectileID = projectiles[i].ID, OwnerID = ownerIDs[i].Value }; onHitSystemFinish?.Invoke(handlerData, resultsArray[i]); } } entities.Dispose(); ownerIDs.Dispose(); projectiles.Dispose(); commands.Dispose(); resultsArray.Dispose(); return(inputDeps); }
protected override JobHandle OnUpdate(JobHandle inputDeps) { // if there are no entities - return int count = MultiHitQuery.CalculateEntityCount(); if (count == 0) { return(inputDeps); } // Get the matching entities from the query var entities = MultiHitQuery.ToEntityArray(Allocator.TempJob); var ownerIDs = MultiHitQuery.ToComponentDataArray <OwnerID>(Allocator.TempJob); var projectiles = MultiHitQuery.ToComponentDataArray <Projectile>(Allocator.TempJob); // Setup Ray Commands NativeArray <RaycastCommand> rayCommands = new NativeArray <RaycastCommand>(count, Allocator.TempJob); JobHandle setupJob = new SetupCommandsJob { // these get the data from the m_HitGroup query prevPositions = MultiHitQuery.ToComponentDataArray <PreviousTranslation>(Allocator.TempJob), positions = MultiHitQuery.ToComponentDataArray <Translation>(Allocator.TempJob), rotations = MultiHitQuery.ToComponentDataArray <Rotation>(Allocator.TempJob), commands = rayCommands, hitLayer = HitMask, multiHits = MultiHitQuery.ToComponentDataArray <MultiHit>(Allocator.TempJob) }.Schedule(count, 32, inputDeps); setupJob.Complete(); #region Default raycast Ray ray; int hitCount; HitHandlerData handlerData; // for every projectile for (int i = 0; i < count; i++) { ray = new Ray { origin = rayCommands[i].from, direction = rayCommands[i].direction }; handlerData = new HitHandlerData { Entity = entities[i], ProjectileID = projectiles[i].ID, OwnerID = ownerIDs[i].Value }; // if we have any hits hitCount = Physics.RaycastNonAlloc(ray, Hits, rayCommands[i].distance, rayCommands[i].layerMask); if (hitCount > 0) { NativeArray <RaycastHit> tmpHits = new NativeArray <RaycastHit>(hitCount, Allocator.TempJob, NativeArrayOptions.UninitializedMemory); // for every hit for (int j = 0; j < hitCount; j++) { tmpHits[j] = Hits[j]; } // call our individual multihit handler onMultiHitSystemFinish?.Invoke(handlerData, tmpHits); tmpHits.Dispose(); } } #endregion entities.Dispose(); ownerIDs.Dispose(); projectiles.Dispose(); rayCommands.Dispose(); return(inputDeps); }