Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Question RayCastCommands Job - how to track the hits?

Discussion in 'Physics' started by rc82, Mar 1, 2024.

  1. rc82

    rc82

    Joined:
    Jan 28, 2020
    Posts:
    44
    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:

    Code (CSharp):
    1.  private void moveProjectilesAndJobRaycast()
    2. {
    3.      //For raycast Job
    4.      raycastHits = new NativeArray<RaycastHit>(2, Allocator.TempJob);
    5.      raycastCommands = new NativeArray<RaycastCommand>(projectileToMoveList.Count, Allocator.TempJob);
    6.    
    7.      for(int i = 0;i < projectileToMoveList.Count;i++) {
    8.          tempTransform = projectileToMoveList[i].transform;
    9.  
    10.          //Job Values
    11.          tempOrigin = tempTransform.position;
    12.          tempDirection = tempTransform.forward;
    13.  
    14.          //Moving the Projectile
    15.          tempVelocity = dictProjectileAndWeaponStats[projectileToMoveList[i]].projectileSpeed;
    16.          tempDistanceMoved = tempVelocity * Time.deltaTime;
    17.          tempTransform.position += tempTransform.forward * tempDistanceMoved;
    18.            
    19.          //populate the raycast commands.
    20.          raycastCommands[i] = new RaycastCommand(tempOrigin, tempDirection, QueryParameters.Default,tempDistanceMoved);
    21.  
    22.      }
    23.          //Schedule the raycasts
    24.          handle = RaycastCommand.ScheduleBatch(raycastCommands, raycastHits, 5, 5, default);
    25. }

    CALLED IN LATE UPDATE: finish the job.
    Code (CSharp):
    1. private void completeProjectileHits()
    2. {
    3.      handle.Complete();
    4.      for(int i = 0; i < raycastCommands.Count(); i++)
    5.      {
    6.          if (raycastHits[i].collider != null)
    7.          {
    8.              Debug.Log(" HIT!: " + raycastHits[i].collider.name);
    9.              tempProjectilesToRemove.Add(projectileToMoveList[i]);
    10.  
    11.          }
    12.      }
    13.  
    14.      //If impacted,remove the projectiles.
    15.      for(int i = 0;i < tempProjectilesToRemove.Count; i++)
    16.      {
    17.          projectileToMoveList.Remove(tempProjectilesToRemove[i]);
    18.          dictProjectileAndWeaponStats.Remove(projectileToMoveList[i]);
    19.          dictProjectileToImpact.Remove(projectileToMoveList[i]);
    20.      }
    21.      //clear for next frame
    22.      tempProjectilesToRemove.Clear();
    23.    
    24.      raycastCommands.Dispose();
    25.      raycastHits.Dispose();
    26.    
    27. }
     
  2. Spy-Master

    Spy-Master

    Joined:
    Aug 4, 2022
    Posts:
    776
    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.
    https://docs.unity3d.com/ScriptReference/RaycastCommand.html
     
    rc82 likes this.
  3. rc82

    rc82

    Joined:
    Jan 28, 2020
    Posts:
    44

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

    Thank you!!!!