Hi! I’m very new to scripting in Unity, so please forgive the stupid question.
I need to set minimum and maximum values for a var, in order to stay within a specific range (e.g. 1 - 10).
I’ve got a camera set up that allows me to zoom in and out using the mouse scroll wheel. This is determined by a var called “distance”. It works fine, but the player would be able to zoom in and out infinitely. That’s why I want to set min max values for “distance”.
Also, even if one was to do that, it would be better as
if (distance > 10.0)
else if (distance < 1.0)
Otherwise, without the “else”, it always has to evaluate both “if” statements, which is a waste of CPU cycles, since the second one can’t ever be true if the first one is true. (Not a lot of CPU cycles, to be sure, but it’s such a simple optimization that one might as well get in the habit of doing it.) Also it’s slightly faster to use the correct type for the numbers, namely floats instead of ints in this case. Otherwise they have to be converted, which again is a waste, and again is not much of a waste, but it’s so easy to do it optimally that one might as well. Also it makes the code clearer as to what types you’re dealing with exactly.