A common thing in game development is getting a random number between a minimum and maximum.
Maybe something has a min and max attack damage, attack range, reaction time.
Or maybe you need to spawn a min and max number of things for a procedural level.
For all of these cases, we need to control the min and max. Which is where our Odin attribute [MinMaxSlider()] comes in.
MinMax is a set of two values stored in a Vector2, where x is the min value and y is the max value. So, the variable we declare has to be of type Vector2, and we pass the absolute minimum and maximum for the slider into the attribute as floats. So here, we’re making a slider between 10 and 15 for the damage range:
1 2 3 |
[MinMaxSlider(10f, 15f)] public Vector2 DamageRange; |
…which turns into this in the inspector:
1 2 3 4 5 |
float GetDamageAmount() { return Random.Range(DamageRange.x, DamageRange.y); } |
And there you go, a neat way of managing ranges in your projects.