# Efficient method for returning the shortest RayCastHit? **SOLVED**

Hello, as the title states I need an efficient method of returning the shortest RayCastHit of several.

I fire 7 rays in different directions each from the same position. It’s possible that some do not hit anything. Out of the ones that do hit I need to return the RayCastHit that is the shortest from the start point.

Thanks.

EDIT - I am getting an array index out of range and am not sure why here is my code. SOLVED

``````private void Update()
{
RaycastHit hit = GetShortestRayCastHit(trans.position);
}

// return the shortest hit
private RaycastHit GetShortestRayCastHit(Vector3 startPos)
{
Ray ray1 = new Ray(startPos, RayDir4.forward);
Ray ray2 = new Ray(startPos, RayDir5.forward);
Ray ray3 = new Ray(startPos, RayDir6.forward);
Ray ray4 = new Ray(startPos, RayDir1.up);
Ray ray5 = new Ray(startPos, RayDir2.up);
Ray ray6 = new Ray(startPos, RayDir3.up);
Ray ray7 = new Ray(startPos, RayDir4.up);

RaycastHit[] hits = new RaycastHit[7];

float[] distances = new float[7];

if (Physics.Raycast(ray1, out hits[0], 1.0f, TerrainMask))
distances[0] = Vector3.Distance(startPos, hits[0].point);
else
distances[0] = Mathf.Infinity;
if (Physics.Raycast(ray2, out hits[1], 1.0f, TerrainMask))
distances[1] = Vector3.Distance(startPos, hits[1].point);
else
distances[1] = Mathf.Infinity;
if (Physics.Raycast(ray3, out hits[2], 1.0f, TerrainMask))
distances[2] = Vector3.Distance(startPos, hits[2].point);
else
distances[2] = Mathf.Infinity;
if (Physics.Raycast(ray4, out hits[3], 1.0f, TerrainMask))
distances[3] = Vector3.Distance(startPos, hits[3].point);
else
distances[3] = Mathf.Infinity;
if (Physics.Raycast(ray5, out hits[4], 1.0f, TerrainMask))
distances[4] = Vector3.Distance(startPos, hits[4].point);
else
distances[4] = Mathf.Infinity;
if (Physics.Raycast(ray6, out hits[5], 1.0f, TerrainMask))
distances[5] = Vector3.Distance(startPos, hits[5].point);
else
distances[5] = Mathf.Infinity;
if (Physics.Raycast(ray7, out hits[6], 1.0f, TerrainMask))
distances[6] = Vector3.Distance(startPos, hits[6].point);
else
distances[6] = Mathf.Infinity;

float maxDist = Mathf.Infinity;
int distIndex = 0;

for (var i = 0; i < distances.Length; i++)
{
if (distances *< maxDist)*
``````

{
maxDist = distances*;*
distIndex = i;
}
}
return hits[distIndex];
}

Put the results of the raycasts into an array then scan it for least amount, something like this:

``````var distances : float[]; // fill this array with raycast distances

var maxDist : float = Mathf.infinity;
var distIndex : int = 0;

for (var i = 0; i < distances.length; i++)
{
if (distances *< maxDist)*
``````

{
maxDist = distances*;*
distIndex = i;
}
}
distIndex will be the index of the shortest one, which you can use to reference back to your raycast hits if you need to.

If you want to store 7 things in an array, the array then should have the length of 7.
So…
RaycastHit hits = new RaycastHit[7];

Same thing with distances.

The length of arrays and lists for computers is like we think as a human about. Only the indexing starts with 0, so that your last index is length minus 1. That’s why the condition i < distances.length is working correctly.