Search Unity

RaycastNonAlloc not functional?

Discussion in '2D' started by Krileon, Feb 8, 2015.

  1. Krileon

    Krileon

    Joined:
    Oct 30, 2012
    Posts:
    642
    When using RaycastNonAlloc it never returns any results. No matter what I do the results array is empty and the INT return value is always 0. RaycastAll works fine and returns the appropriate results. The issue is I'm doing this on every frame in Update for my 2D collision detection so I don't want to allocate to GC; although it's not allocating much (couple of bytes) I would rather avoid the GC if possible. Any idea what's going on? Code is as follows.

    Code (CSharp):
    1.         if ( Physics2D.RaycastNonAlloc( bottomGround, Vector2.right, bottomHits, col.bounds.size.x, mask ) > 0 ) {
    2.             foreach ( RaycastHit2D bottomHit in bottomHits ) {
    3.                 if ( ( bottomHit.collider != null ) && ( ! Physics2D.GetIgnoreCollision( bottomHit.collider, col ) ) ) {
    4.                     grounded = true;
    5.                     break;
    6.                 }
    7.             }
    8.         }
     
  2. Krileon

    Krileon

    Joined:
    Oct 30, 2012
    Posts:
    642
    To understand what's going on I cast a line across the bottom of the collider. Anything it hits is grounded tested. The reason for this is it's a tiled system. So a single Raycast will not work, because it could hit 2-3 tiles at once and would only test against one of those. Some tiles maybe set to ignore to allow fall through so anything hit needs to be tested. This all works wonderfully, but the GC is like 200-300 B for 1 character and as it's a lemmings style game there maybe upwards of 50, which causes FPS to tank from GC.

    I originally had a 3 Raycast system. 1 from bottom left, 1 from bottom middle, and 1 from bottom right. The issue with this is it's not accurate. Example cases are going up stairs and the stairs turns into a point. When the character covers over the edge of the stairs none of these Raycasts hit, but he's still grounded until he completely goes over the edge. If everything is assumed squared this isn't an issue, but any triangles or circles completely break the 3 ray usage. I guess I could increase it to a 5 ray system and see how that goes, but that seams a bit absurd when a single ray across the bottom works perfectly; I just need to fix its allocation.

    Below is a screenshot of the collision detection.



    The blue bars hit and detect if collision needs to be shut off. For example if going through the bottom of a platform or from behind a stair case. The green determines if he should turn around. The red determines if he's going up hill. The bottom yellow determines grounded and the top yellow determines if his head hit anything.

    If there was only going to be 1 on screen I'd ignore the GC, but since up to 50 maybe on screen it needs to be more efficient with GC. Which is why I need the non alloc function to actually work. Normally I'd just use layer collision detection and then use a single Raycast instead of RaycastAll, but that can't work in this situation as again there will be up to 50 of these guys on screen and all have their own collision to be concerned with.
     
    Last edited: Feb 8, 2015
  3. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,456
    Ensure that you're not passing an empty array. The call can only return results up to the size of the array you provide.
     
  4. Krileon

    Krileon

    Joined:
    Oct 30, 2012
    Posts:
    642
    Aha, yup that was it.