Home > other >  What does the opposite outcome of Unity Math.Clamp()?
What does the opposite outcome of Unity Math.Clamp()?

Time:01-17

Is there a method of making sure a integer can't be between two numbers, kind of like Unity's Mathf.Clamp() method but instead of not allowing it to be above or below two numbers, make sure its not between two numbers.

CodePudding user response:

If it's not between the two numbers, what should it be - the upper value or the lower value? If you want the closest, you could do:

int ClosestIfBetween(int val, int low, int high)
{
    if (val > low && val < high)
    {
        int mid = (high - low) / 2   low;
        return val < mid ? low : high;
    }
    return val;
}
  •  Tags:  
  • Related