Clamping Values
A common task that we might want to perform in a game is clamping a value. What does clamping a value mean exactly? Clamping a value means making a value have an upper bound and/or a lower bound that it can operate at. In Unity, we have built in methods which allow us to do this. There are included in the Mathf library, and allow us to set minimums, maximums, and a combination of the two. Some examples at times that we would like to this could be clamping the position of a spawned object so that it is randomly spawned in a certain area, or clamping a players 2D movement between two points. We will go over the latter in this tutorial to provide you an example. In this tutorial, we will go over the Mathf.Clamp method, Mathf.Min method, Mathf.Max method, and an example involving clamping a player's movement.
Mathf.Clamp
The mathf.clamp method is not a very complicated method. It is called using Mathf.Clamp with a value that you would like to clamp, a minimum value that the value can be, and a maximum value that the value can be. One thing to note with this function is that it can either be an integer or a float return type. This defines how much detail the clamping can perform. If we would like to only clamp one of the values, we will use the Mathf.max / Mathf.min methods.
public static float Clamp(float value, float min, float max);
public static int Clamp(int value, int min, int max);
Unity Script Reference: https://docs.unity3d.com/ScriptReference/Mathf.Clamp.html
public static float Clamp(float value, float min, float max);
public static int Clamp(int value, int min, int max);
Unity Script Reference: https://docs.unity3d.com/ScriptReference/Mathf.Clamp.html
Mathf.Min / Mathf.Max
The other way that you can clamp values is by a maximum or minimum value. This is useful for when you want to limit a value in one direction and not the other. For instance, let's say that you wanted a vehicle to have no maximum speed, but have a minimum speed of 0. You can use Mathf.Max to clamp the the value to the maximum of your current speed and 0. This seems like backwards thinking because you are working with a minimum. If you think of what is happening though, you are returning the value which is higher. In the case of any number less than 0, you will return 0; Effectively clamping the number to 0. Same concept works when you want to create a maximum; You will use the Mathf.Min method to clamp the number to a max value.
public static float Min(float a, float b);
public static float Min(params float[] values);
public static float Max(float a, float b);
public static float Max(params float[] values);
Unity Script Reference: https://docs.unity3d.com/ScriptReference/Mathf.Clamp.html
public static float Min(float a, float b);
public static float Min(params float[] values);
public static float Max(float a, float b);
public static float Max(params float[] values);
Unity Script Reference: https://docs.unity3d.com/ScriptReference/Mathf.Clamp.html
2D Clamped Movement
This example will show you how to clamp player movement so that we are able to see it in the editor. We will be clamping the players movement so that it is visible in the scene when we are testing our 'game'. If we wanted to deploy this 'game' to other users, we would want to account for differing screen sizes.
For more tutorials, follow us on Twitter and Youtube!