- Home /
RaycastNonAlloc return value ordering?
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.
Answer by MelvMay · Sep 08, 2016 at 07:04 AM
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.
Answer by slake_it · Jan 28, 2019 at 01:28 PM
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 https://docs.unity3d.com/ScriptReference/Physics2D.RaycastAll.html "The colliders in the array are sorted in order of distance from the origin point"
3D https://docs.unity3d.com/ScriptReference/Physics.RaycastAll.html "Note that order is not guaranteed."
Answer by Netuddmeg · Mar 21, 2018 at 08:00 AM
I'm note sure. According to my observations the result is not ordered.
@Netuddmeg All raycast methods use a single internal function to perform the sort. This single raycast method performs a sort based upon hit-fraction (normalized distance from start).
@$$anonymous$$elv$$anonymous$$ay This is not true. Doing some debugging on RaycastNonAlloc. $$anonymous$$y first entry in the results array has a distance of ~30 units. The second entry has a distance of ~8 units. I'm including triggers in the parameters and excluding anything on the IgnoreRaycast layer. I'll have to do my own distance sorting on this.
It is true. Results are sorted by distance before being returned. I have no idea how you can have $$anonymous$$us values, distances are zero and above.
Those are not $$anonymous$$us signs. They are tildes, meaning about. The only thing I know of that uses ~ as a negative operator is Standard $$anonymous$$L, which has nothing to do with Unity. In all other mathematical and computational uses when placed in front of a numerical value, it either means a logical not (which wouldn't make contextual sense in this case) or approximately. I am expressing that I am removing all the decimal points from my statement. Because you are right, the properties of the distance formula prevent a negative value being returned.
The 30 value is first in the array. The 8 value is next. Clearly, the values are not sorted by distance. It may be the case that they are sorted if you use only one layer and don't include triggers in the calculation. But, that is not what I am doing, and they are not sorted, and people should be aware that the values are not sorted in this case. $$anonymous$$y best hypothesis is that colliders are sorted and entered into the array and then triggers are, but I have not investigated this further.