First of all I would like to say that the new CameraRaycaster class is working great, it has however introduced a small bug where if you have more than 1 hit within the same priority layer, it will not check for the closest hit but just return the first hit it finds within the priority layer that it is currently checking.
In my game this made it impossible to click on and walk over certain bridges, as it found the hit on the terrain first and did never even check the hit on the bridges which were also in the Walkable layer.
In case anyone else is having the same problem I do provide my solution here below.
RaycastHit? FindTopPriorityHit (RaycastHit[] raycastHits)
{
// Form list of layer numbers hit
List<int> layersOfHitColliders = new List<int> ();
foreach (RaycastHit hit in raycastHits)
{
layersOfHitColliders.Add (hit.collider.gameObject.layer);
}
// Step through layers in order of priority looking for a gameobject with that layer
foreach (int layer in layerPriorities)
{
// A variable for holding the indext to the currently closest most highpriority hit
int topPriorityHitIndex = -1;
// Loop through all hits, need to check all hits for each priority layer in case
// there is more than one hit within the same priority layer
for (int i = 0; i < raycastHits.Length; i++)
{
if (raycastHits[i].collider.gameObject.layer == layer)
{
// Check if any previous hits with the same priority has already been found
if(topPriorityHitIndex >= 0)
{
// Check if this hit is closer
if(raycastHits[i].distance < raycastHits[topPriorityHitIndex].distance)
{
topPriorityHitIndex = i;
}
}
else
{
topPriorityHitIndex = i;
}
}
}
// Check if any hits were found for this priority layer
if(topPriorityHitIndex >= 0)
{
return raycastHits[topPriorityHitIndex];// Stop looking
}
}
return null; // because cannot use GameObject? nullable
}