If I do RaycastNonAlloc (Physics2D if it makes a difference) and the provided array isn’t big enough to hold all of the results, does it prioritize the results closer to the origin? RaycastAll explicitly says the ordering is not guaranteed, but NonAlloc doesn’t say anything at all about what it does about overflowed results. I don’t expect to get more than a couple so I can just make sure the array is more than big enough if that’s what is expected, but it’d be nice to just be able to pass in a single item array since I only care about the first hit.
All Raycast are sorted by distance (closest to the start first). For the NonAlloc call, the first N (the size of the provided results array) are returned so they’ll be the closest.
You could easily check this out yourself with a simple test though.
This answer by @chriscode in the comments is great
"Bit belated, but if anyone else sees this and is confused. The 2D are sorted, the 3D are not.
2D Unity - Scripting API: Physics2D.RaycastAll “The colliders in the array are sorted in order of distance from the origin point”
3D Unity - Scripting API: Physics.RaycastAll “Note that order is not guaranteed.”
I’m note sure. According to my observations the result is not ordered.