[SOLVED] Syntax for LinecastNonAlloc

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

from the documentation:

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’.

Thank you for the help! Didn’t know that was a required one :sweat_smile:

Well I mean… how else would you get RaycastHit2D info?

If all you need is a way to see if a ray hit something, anything, no regard for that hit info… use regular old Raycast:

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.

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.

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.