Bounds has ClosestPoint but BoundsInt does not?
What is the best way to implement this?
This seems to work… but maybe a better way…
Thanks.
public static partial class BoundsIntExtensions
{
public static Vector3Int ClosestPoint(this BoundsInt bounds, Vector3Int p)
{
var closest = float.MaxValue;
var closestPoint = Vector3Int.zero;
foreach (var i in bounds.allPositionsWithin)
{
var test = (i - p).magnitude;
if (test < closest)
{
closest = test;
closestPoint = i;
}
}
return closestPoint;
}
}
EDIT: Thanks.
p.Clamp(bounds.min, bounds.max);