- Home /
When using Mathf.Clamp, can I set a minimum but no maximum?
I'm working on a system using the Mathf.Clamp function and want to know if I can set up a way of making the minimum value be clamped but not the maximum... (or vice-versa if needed.)
Can the Mathf.Clamp work with a minimum but not a maximum or should I input a value for the max that's far, FAR greater than the user would ever hope to reach in this game?
Answer by Bonfire-Boy · Mar 30, 2015 at 04:50 PM
If you really want to use clamp, and really don't want to clamp at one of the ends, use float.MinValue or float.MaxValue rather than guessing at a bound that won't do anything.
float f = Mathf.Clamp(f, myMinValue, float.MaxValue);
But I wouldn't use that unless the lack of clamping was something I wanted to turn on or off (ie making the second parameter into a variable which I could set to float.MaxValue as required).
Two quicker ways spring to mind, which are explicitly only looking at the lower bound...
float f = Mathf.Max( f, myMinValue);
and
if (f < myMinValue) f = myMinValue;
Took a bit to get my head around this (as I'm new to coding in C#) but this helped.
Thank you!
Your answer
Follow this Question
Related Questions
limiting rotation of a rigidbody in C# 3 Answers
Touch control for a Pong Game 1 Answer
Clamping local movement 0 Answers
Trouble with using Mathf.Clamp function in order to create collision detection with raycasts 0 Answers
Mathf.Clamp01 CoRoutine 1 Answer