Hi all,
This question betrays me as a beginner. I am currently trying to do something relatively simple - I am using RaycastAll to return an array of hit, and I then wish to select the hit that contains the collider with the greatest transform.position.y value (and that also is tagged with the value “Node”). The following code accomplishes this:
RaycastHit2D[] hits = Physics2D.RaycastAll(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector2.zero);
RaycastHit2D selectedHit = hits[0];
bool hitSelected = false;
foreach (var hit in hits)
{
if (hit.collider != null)
{
if (hit.collider.tag == "Node")
{
if (!hitSelected)
{
selectedHit = hit;
hitSelected = true;
} else
{
if (hit.collider.transform.position.y > selectedHit.transform.position.y)
{
selectedHit = hit;
}
}
}
}
}
However, I can’t help but feel that this is a bit hackish. Is there a cleaner, more acceptable way of accomplishing this?