public Vector3 GetNormDirectionCandidateRandomAttachPoint(AttachPointType p_Center, Unit p_Opponent) { return(GetNormDirectionCandidateRandomAttachPoint(AttachPoint[(int)p_Center].position, p_Opponent)); }
//Note: Avoid running this query in Update function because calculating Vector3.Distance requires sqr root calculation (expensive) //Instead, find strategic moments to update nearest pivot (i.e. only once when ToolTip becomes active) public static Vector3 FindClosestAttachPointToAnchor(Transform anchor, Transform contentParent, Vector3[] localPivotPositions, AttachPointType pivotType) { Vector3 nearPivot = Vector3.zero; Vector3 currentPivot = Vector3.zero; Vector3 anchorPosition = anchor.position; float nearDist = Mathf.Infinity; if (localPivotPositions == null || localPivotPositions.Length < NumPivotLocations) { return(nearPivot); } switch (pivotType) { case AttachPointType.Center: return(nearPivot); // Search all pivots case AttachPointType.Closest: for (int i = 0; i < localPivotPositions.Length; i++) { currentPivot = localPivotPositions[i]; float dist = Vector3.Distance(anchorPosition, contentParent.TransformPoint(currentPivot)); if (dist < nearDist) { nearDist = dist; nearPivot = currentPivot; } } break; // Search corner pivots case AttachPointType.ClosestCorner: for (int i = (int)AttachPointType.BotRightCorner; i < (int)AttachPointType.TopLeftCorner; i++) { currentPivot = localPivotPositions[i]; float dist = Vector3.Distance(anchorPosition, contentParent.TransformPoint(currentPivot)); if (dist < nearDist) { nearDist = dist; nearPivot = currentPivot; } } break; // Search middle pivots case AttachPointType.ClosestMiddle: for (int i = (int)AttachPointType.BotMiddle; i < (int)AttachPointType.LeftMiddle; i++) { currentPivot = localPivotPositions[i]; float dist = Vector3.Distance(anchorPosition, contentParent.TransformPoint(currentPivot)); if (dist < nearDist) { nearDist = dist; nearPivot = currentPivot; } } break; default: // For all other types, just use the array position // TODO error checking for array size (?) nearPivot = localPivotPositions[(int)pivotType]; break; } return(nearPivot); }