Search Unity

[SOLVED] Syntax for LinecastNonAlloc

Discussion in 'Scripting' started by ChrisIsAwesome, May 30, 2017.

  1. ChrisIsAwesome

    ChrisIsAwesome

    Joined:
    Mar 18, 2017
    Posts:
    184
    I'm trying to test to see if this could be a good alternative to Raycast, but I can't get it to work. The syntax is

    Physics2D.LinecastNonAlloc (Vector2, Vector2)

    Right?

    The problem is it won't work with 2 Vector2s in it. Keeps saying that there's no appropriate version of it for those constructors. Here's my code:

    Physics2D.LinecastNonAlloc (transform.potition, transform.position.y - 5)

    I've even tried:

    Physics2D.LinecastNonAlloc (Vector2 (0, 0), Vector2 (0, -17))

    The -17 in the above line is what the end point would be if 5 is subtracted from the Y position.

    But neither of these are working.

    Any help would be greatly appreciated!

    -- Chris
     
  2. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,537
    from the documentation:
    https://docs.unity3d.com/ScriptReference/Physics2D.RaycastNonAlloc.html

    It has 3 required parameters, as well as several optional.

    The 3rd parameter is the array to put the results in.

    Which is the necessary one... since that's how it doesn't need to instantiate an array for the results for you... making it non-allocating or 'NonAlloc'.
     
  3. ChrisIsAwesome

    ChrisIsAwesome

    Joined:
    Mar 18, 2017
    Posts:
    184
    Thank you for the help! Didn't know that was a required one :oops:
     
  4. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,537
  5. ChrisIsAwesome

    ChrisIsAwesome

    Joined:
    Mar 18, 2017
    Posts:
    184
    I initially did do Raycast. Hasn't been working for me though, so wanted to try this to see if it would solve my problem. Still working out the kinks with Linecast, but it looks like it's working better for me than Raycast was. My first issue (still current) is here.
     
  6. Invertex

    Invertex

    Joined:
    Nov 7, 2013
    Posts:
    1,550
    The thing about the regular Raycast methods in both 3D and 2D is that they generate garbage with each usage because an class object to store the hit info has to be generated each time. With NonAlloc you provide a pre-defined container for the info to be output to instead, reducing workload and creating zero garbage.
     
  7. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,537
    Raycast creates a single struct, the RaycastHit info, no garbage.

    RaycastAll allocates memory, because the array of RaycastHit, and arrays are a class type.

    RaycastNonAlloc is like RaycastAll, without the array allocation, since you create it yourself and can recycle it.

    And the 3D version of Raycast actually returns a bool, you explicitly pass in a struct ref for the RaycastHit to be assigned to. Which isn't necessary, so you can just test if a hit occurred, and not get the hit info.