Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Raycast Command

Discussion in 'Entity Component System' started by Ruchir, Mar 27, 2020.

  1. Ruchir

    Ruchir

    Joined:
    May 26, 2015
    Posts:
    934
    The problem I'm facing is that even though I set the maxHits = 10, I'm still getting just one hit result
    Before I used Physics.RaycastNonAlloc which worked fine.
    Is there something I miss understood about the raycast command?

    Code (CSharp):
    1. NativeArray<RaycastCommand> commands = new NativeArray<RaycastCommand>(stepCount ,Allocator.TempJob);
    2.             NativeArray<RaycastHit> results = new NativeArray<RaycastHit>(stepCount * maxHits , Allocator.TempJob);
    3.            
    4.             for (int i = 0; i < stepCount; i++)
    5.             {
    6.                 float angularPos = -this.angle/2 + i * this.angle / (stepCount - 1);
    7.                 float3 origin = AngularPoint(angularPos, innerRadius);
    8.                 float3 direction = math.normalize(AngularPoint(angularPos, radius) - origin);
    9.                 commands[i] = new RaycastCommand(origin , direction,radius-innerRadius,mask,maxHits);
    10.             }
    11.  
    12.             JobHandle handle = RaycastCommand.ScheduleBatch(commands, results, 1);
    13.            
    14.             handle.Complete();
    I use these results to get the hit points and store them in this struct:
    Code (CSharp):
    1. [Serializable]
    2.         public struct RayHits
    3.         {
    4.             public List<RaycastHit> hits;
    5.             public float distance;
    6.             public float3 origin;
    7.             public float3 direction;
    8.         }
    using this:
    Code (CSharp):
    1. for (int i = 0; i < stepCount; i++)
    2.             {
    3.                 RayHits phits = new RayHits {hits = new List<RaycastHit>()};
    4.                 float angularPos = -this.angle/2 + i * this.angle / (stepCount - 1);
    5.                 phits.origin = AngularPoint(angularPos, innerRadius);
    6.                 phits.direction = math.normalize(AngularPoint(angularPos, radius) - phits.origin);
    7.                 phits.distance = (radius - innerRadius);
    8.                 for (int j = 0; j < maxHits; j++)
    9.                 {
    10.                     int index = i * maxHits + j;
    11.                     if (results[index].collider != null)
    12.                     {
    13.                         if (!ignoreCollider.Contains(results[index].collider))
    14.                         {
    15.                             phits.hits.Add(results[index]);
    16.                             phits.distance = results[index].distance;
    17.                             break;
    18.                         }
    19.                     }
    20.                     else
    21.                     {
    22.                         break;
    23.                     }
    24.                 }
    25.                 rayHits.Add(phits);
    26.             }
    Can anyone point out what's the problem in this code?

    Actually the problem arises when this list : ignoreCollider contains the hit collider, I just don't get the next collider it should have hit
     
  2. eizenhorn

    eizenhorn

    Joined:
    Oct 17, 2016
    Posts:
    2,683
  3. Ruchir

    Ruchir

    Joined:
    May 26, 2015
    Posts:
    934