RayCastCommands Job - how to track the hits?

Hi team! Any help is appreciated here.

So I have some projectiles, and I want to move them in a single method in an update loop, and also track collisions via jobified raycasts. I seem to be able to do the raycasts just fine, but my “Max Hits” buffer is too small - I set it for 5 max collisions per raycast command. I clearly don’t understand it?

It’s really late and my brain is foggy. Any advice here?

CALLED IN UPDATE: Moving projectiles (non jobbed for now) and preparing the raycast jobs:

 private void moveProjectilesAndJobRaycast()
{
     //For raycast Job
     raycastHits = new NativeArray<RaycastHit>(2, Allocator.TempJob);
     raycastCommands = new NativeArray<RaycastCommand>(projectileToMoveList.Count, Allocator.TempJob);
    
     for(int i = 0;i < projectileToMoveList.Count;i++) {
         tempTransform = projectileToMoveList[i].transform;

         //Job Values
         tempOrigin = tempTransform.position;
         tempDirection = tempTransform.forward;

         //Moving the Projectile
         tempVelocity = dictProjectileAndWeaponStats[projectileToMoveList[i]].projectileSpeed;
         tempDistanceMoved = tempVelocity * Time.deltaTime;
         tempTransform.position += tempTransform.forward * tempDistanceMoved;
           
         //populate the raycast commands.
         raycastCommands[i] = new RaycastCommand(tempOrigin, tempDirection, QueryParameters.Default,tempDistanceMoved);

     }
         //Schedule the raycasts
         handle = RaycastCommand.ScheduleBatch(raycastCommands, raycastHits, 5, 5, default);
}

CALLED IN LATE UPDATE: finish the job.

private void completeProjectileHits()
{
     handle.Complete();
     for(int i = 0; i < raycastCommands.Count(); i++)
     {
         if (raycastHits[i].collider != null)
         {
             Debug.Log(" HIT!: " + raycastHits[i].collider.name);
             tempProjectilesToRemove.Add(projectileToMoveList[i]);

         }
     }

     //If impacted,remove the projectiles.
     for(int i = 0;i < tempProjectilesToRemove.Count; i++)
     {
         projectileToMoveList.Remove(tempProjectilesToRemove[i]);
         dictProjectileAndWeaponStats.Remove(projectileToMoveList[i]);
         dictProjectileToImpact.Remove(projectileToMoveList[i]);
     }
     //clear for next frame
     tempProjectilesToRemove.Clear();
    
     raycastCommands.Dispose();
     raycastHits.Dispose();
    
}

You set it to keep up to 5 hits per command, but where are you going to receive them? In the 2-element hits buffer “raycastHits” that you pass to it? That wouldn’t make much sense. You need to pass a buffer that can actually take that many elements per command.

1 Like

OHMYGOD thank you!!! I can’t believe I missed that. It’s basically the first line too.

Thank you!!!