how to get the shortest distance of the raycast to the target.

Hi I’m new in gamedevelopment and I am trying to develop a enemy ai for a golf game.
I use raycast reflection for the enemy to search for the goal and it’s working perfectly.
the ball game object will keep rotating until the raycast reflection hit the goal and the ball will shoot to that direction.
However I want it to always use the shortest possible distance from the ball position to the goal because it’s always using the first raycast that hit the goal even if it’s not the shortest route and I have no idea yet on how to do that.

Here is the sample of the code that I have for the enemy AI.`enter code here

public void seek()
	{

		
		ray = new Ray2D(transform.position, transform.up);


		lineRenderer.positionCount = 1;
		lineRenderer.SetPosition(0, transform.position);
		float remainingLength = maxLength;

		move.SetDirection(ray.direction);

		for (int i = 0; i < reflections; i++)
		{
			
			 layerMask = 1 << 3;

			
			layerMask = ~layerMask;

			hit = Physics2D.Raycast(ray.origin, ray.direction, remainingLength, layerMask);
			dir = ray.direction;
			if (hit != null)
			{
				
				lineRenderer.positionCount += 1;
				lineRenderer.SetPosition(lineRenderer.positionCount - 1, hit.point);
				remainingLength -= Vector2.Distance(ray.origin, hit.point);
				ray = new Ray2D(hit.point + hit.normal * 0.001f, Vector2.Reflect(ray.direction, hit.normal));
				if (hit.collider.tag == "goal")
				{
					move.shoot();
					break;

				}
                else
                {
					transform.Rotate(new Vector3(0f, 0f, 1f));
					Debug.Log(hit.distance);
                }
			}
			else
			{
				
				lineRenderer.positionCount += 1;
				lineRenderer.SetPosition(lineRenderer.positionCount - 1, ray.origin + ray.direction * remainingLength);
			}
		}

I manage to fix it. What I did is I used the i variable to count how many reflections are left and use that to determine the shorter distance of the raycast.
firstray = reflections - i;
this code will get the initial reflection left per rotation;
Then
if (hit.collider.tag == “goal”)
{

					secondray = firstray - i;

					if( secondray >= firstray )
                    {
						move.shoot();
                    }
					else
                    {
						
						transform.Rotate(new Vector3(0f, 0f, 10f) * Time.deltaTime);
					}

I’m really not sure how the math works using that formula but It works perfectly.
whenever the 2nd ray is greater than the first ray meaning it got less reflection = shorter distance to the target.
Hope this help