private bool CheckTargetInBeam(out Vector3 position, out Vector3 velocity) { position = default; velocity = default; var index = -1; float length; var minLength = float.MaxValue; foreach (var p in targetPositions) { if (AngleUtils.GetRangeBetweenAnglesAbs(p.y, beamOrientation.x) > beamSize.x / 2 || AngleUtils.GetRangeBetweenAnglesAbs(p.z, beamOrientation.y) > beamSize.y / 2) { continue; } length = Mathf.Pow(AngleUtils.GetRangeBetweenAnglesAbs(p.y, beamOrientation.x), 2) + Mathf.Pow(AngleUtils.GetRangeBetweenAnglesAbs(p.z, beamOrientation.y), 2); if (length < minLength) { minLength = length; index = targetPositions.IndexOf(p); } } if (index == -1) { return(false); } position = targetPositions[index]; velocity = targetVelocities[index]; return(true); }