Search Unity

  1. Curious about what's going to be in 2020.1? Have a look at the 2020.1 beta blog post.
    Dismiss Notice
  2. Want to see 2020.1b in action? Sign up for our Beta 2020.1 Overview Webinar on April 20th for a live presentation from our evangelists and a Q&A session with guests from R&D.
    Dismiss Notice
  3. Interested in giving us feedback? Join our online research interviews on a broad range of topics and share your insights with us.
    Dismiss Notice
  4. New Unity Live Help updates. Check them out here!

    Dismiss Notice

Raycast Command

Discussion in 'Data Oriented Technology Stack' started by Ruchir, Mar 27, 2020.

  1. Ruchir

    Ruchir

    Joined:
    May 26, 2015
    Posts:
    114
    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:
    1,813
  3. Ruchir

    Ruchir

    Joined:
    May 26, 2015
    Posts:
    114
unityunity