public void RoadPosition(Vector3 position, ICollection <NetworkItem> possibleItems, out NetworkLane networkLane, out NetworkItem networkItem) { var currentPosition = MMKExtensions.ToVector2(position); float minimalDistance = float.MaxValue; networkItem = null; networkLane = null; // Traverse all lanes and find the nearest foreach (NetworkItem item in possibleItems) { foreach (NetworkLane lane in item.GetAllLanes()) { List <Vector3> vertices = lane.vertices; for (int i = 0; i < vertices.Count - 1; i++) { var laneA = MMKExtensions.ToVector2(vertices [i]); var laneB = MMKExtensions.ToVector2(vertices [i + 1]); float currentDistance = Mathf.Abs(MMKExtensions.DistanceLineToPoint(laneA, laneB, currentPosition, true)); if (currentDistance < minimalDistance) { minimalDistance = currentDistance; networkLane = lane; networkItem = item; } } } } }
public void CurrentRoadPosition(out NetworkLane networkLane, out NetworkItem networkItem) { var currentItems = currentRoads.Values; RoadPosition(transform.position, currentItems, out networkLane, out networkItem); }