- Home /
RaycastAll returning results in reverse order of collisions
I looping 6 times and performing 6 RaycastAll in various directions, some directions the results return as expected with the first result being the first collision. With a couple of the cast the results return in reverse collision order.
for(var i = 0; i < directions.length;i++){
var ray = new Ray(transform.position,directions[i]);
hitPoints = Physics.RaycastAll(ray,rayLength,layerMask2);
}
Why is this occurring?
Thanks, Caius.
Answer by whydoidoit · Jul 10, 2012 at 10:31 AM
The documentation specifically says that the order of collisions is not guaranteed.
I can't tell what language you are using but in c# you would order them like this:
using System.Linq;
....
hitpoints = Physics.RaycastAll(ray, raylength, layerMask2).OrderBy(h=>h.distance).ToArray();
Thanks $$anonymous$$ike, using JS trying to convert that little bit of code you gave me, where is "h" defined. I like the fact this line is compact and avoiding having to loop through the array to reorder.
Yeah the JS is a bit more wordy:
hitpoints = Physics.RaycastAll(ray, rayLength, layer$$anonymous$$ask2).OrderBy(function(h) { return h.distance; }).ToArray();
Really something is doing a JIT in that line? So that's happening because it doesn't have one of the types at compile time. IEnumerable generic of RaycastHit perhaps or an array of RaycastHit - true defining variables of those types.
Your answer
![](https://koobas.hobune.stream/wayback/20220613073954im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
graphic raycast with a certain direction ? 0 Answers
Broadcast Message using RaycastAll 1 Answer
EteeskiTutorials' bullets (raycast bullets with gravity) 1 Answer
Check when RaycastAll leaves an object 0 Answers
Bullets sometimes don't work 1 Answer