Hi Guys
I’m having trouble understanding the ORDER a ray casts returns all hits. Basically the raycast works and returns an array populated with the many gameobjects it passed through however the ORDER of the actual gameobjects in the array is not consistent or reliable and not in the order I intended.
I’ve attached a picture which outlines my situation (located at the bottom).
My code in it’s basic form:
var hits : RaycastHit[];
hits = Physics.RaycastAll(goTopMostGameObject.transform.position, goTopMostGameObject.transform.forward, Mathf.Infinity);
for (var i = 0; i < hits.length; i++)
{
Debug.Log(i.ToString() + ", " + hits[i].collider.gameObject.name + ", " + hits[i].collider.gameObject.tag);
}
I was hoping the raycast would order the gameobjects in the hits array in the same order the raycast passes through the gameobjects in the scene, … however this isn’t the case. If it ordered the gameobjects accordingly then I could compare the top gameobject with the one directly beneath it and check for gameobjects that have the same tag.
Based on my image below I though I’d get the following results (for clarity I’m not using actual code)
hits(typeA, typeB, typeA, typeC, typeC);
Instead it could order them this way:
hits(typeB, typeA, typeC, typeA, typeC);
Originally I had the gameobjects closer and I though the box colliders might be overlapping which I thought might cause the unpredictible order however I did move them apart more and reduced the size (z) of the Box collider without luck.
Any suggestions?
Cheers
Dean
