- Home /
The question is answered, right answer was accepted
LinecastNonAlloc, documented syntax gives error...
I found out about the non memory allocating version of Linecast and want to move to it, but I can't for the life of me figure out how to get the syntax right to let it compile. The documentation simply says to at least have your start/end Vector 2's, as usual, and then RaycastHit2D[] for the third argument. But this does not compile, it will say "Unexpected symbol "[". The doc provides no actual script example, sadly =/
So has anyone used this version of Linecast yet and gotten it to work? Here's an example of the code line I'm trying.
Physics2D.LinecastNonAlloc(groundCheckL_start.position, groundCheckL_end.position, RaycastHit2D[]);
I've tried it in a fresh/clean script as well, just to make sure it wasn't an issue caused by something else in my script. And it's definitely not the first 2 arguments, or at least shouldn't be, as they are Vector2 positions which worked fine with the regular Linecast.
Reference: http://docs.unity3d.com/Documentation/ScriptReference/Physics2D.LinecastNonAlloc.html
I think it requires an array of RaycastHit2D's, so you should try creating an array beforehand and it should pass the values to it
RaycastHit2D[] hits;
Physics2D.LinecastNonAlloc(..., ..., out hits);
Though the 3D ray/linecasts use the out keyword to set the variables values, the documentation didn't mention it for this one so I'm not exactly sure how it should work.
Doesn't seem to work that way unfortunately, or at least I'm not understanding how to utilize it. It will accept the hits array and compile, but it doesn't seem to do anything with it when used like that? And yeah, out definitely doesn't work, tells you to remove it.
Yeah, out is used with the other functions so that it can return a boolean for if it hit anything as well as pass the information to another variable - I'm not sure if it's something I haven't learned about but without it I don't see how it would populate the array.
The documentation says "the results are returned in the supplied array - but the results array will not be resized if it doesn't contain enough elements to report all the results". So it cannot resize the uninitialized array as out would allow, 0 elements will not let it pass any information. I'm not sure how it would populate it but give it a try, if you only need the first intersection then only set it's length to 1.
RaycastHit2D[] hits = new RaycastHit2D[1];
I would test it but I'm holding off updating Unity for now
Answer by HappyMoo · Jan 05, 2014 at 05:23 PM
const int maxReturnedIntersections = 5;
private RaycastHit2D[] hits = new RaycastHit2D[maxReturnedIntersections];
...
...
int foundIntersections = Physics2D.LinecastNonAlloc(start, end, hits);
if (foundIntersections>maxReturnedIntersections) Debug.LogWarning("Got More Intersections than expected. Consider increasing maxReturnedIntersections");
int intersections = Mathf.min(maxReturnedIntersections, foundIntersections);
for(int i = 0; i<intersections; i++)
{
Debug.Log(hits[i]);
}
Thanks! Got my surface checks converted now thanks to this. $$anonymous$$an, the documentation definitely needs more elaboration on this function haha
Follow this Question
Related Questions
Get the name of the collided object 2 Answers
Problem with raycast vertical 1 Answer
Calculating angle using a raycastHit2D 1 Answer
Physics2D.Linecast ignoring walls 1 Answer
Using methods from other classes on a linecast hit 0 Answers