Mathf.Clamp Unity C# tutorial

There is no game development with mathematics. We see multiple mathematic calculations during the game development process. Mathf in Unity is the combination of all mathematic functions that you will need for your game. Mathf is inbuild in the Unity engine namespace so, you don’t need to add any to use Mathf.

Mathf has lot of static functions. You can get the complete list from Unity docs. In this tutorial, we are going to cover only the frequently used function Mathf.Clamp with examples.

Mathf.Clamp function in Unity clamps the value of a Float or an Integer between the given range. Mathf.Clamp takes three arguments. The first one is the variable which needs to be clamped, second input is the minimum range and the last input is the maximum range.

It will return the minimum range if the variable value is less than the minimum range and will return the maximum value if the variable is larger than the maximum range. If the value is between the range, then the output will be equal to the variable.

Free Unity asset banner

C# script to clamp a Float

using UnityEngine;

public class Mathf_example1: MonoBehaviour
{
   float my_value=0f;
   float min_value=10.0f;
   float max_value=20.0f;

   void Update()
   {
      my_value+=1.0f;
      my_value=Mathf.Clamp(my_value,min_value,max_value);
      Debug.Log(my_value);
   }

}

Mathf.Clamp example output

Tip: If you need to clamp a value between 0 and 1. Then you can use Mathf.Clamp01. It takes only one variable as input and clamps the value between 0 and 1.

How to clamp a Vector3 in Unity

You cannot clamp all the axis of a Vector3 in one sentence. You can either clamp the magnitude of the Vector3 or You need to clamp the individual axis. To clamp the magnitude of a Vector3, we can just can use Vector3.ClampMagnitude.

Let’s use Mathf.Clamp and try to clamp position of a game object in the Y axis. You can implement the same for the other axis.

  1. Create a new script call vector_clamp.
  2. Add it to the game object you want to clamp.
  3. Set the value where you want to clamp the Vector3 Y axis.
  4. Play the game and see the result.
using UnityEngine;

public class vector_clamp : MonoBehaviour
{
    Vector3 pos;
    float height_restriction=10f;
    // Start is called before the first frame update
    void Start()
    {
        pos=transform.position;
        
    }

    // Update is called once per frame
    void Update()
    {
        pos.y+=0.5f;
        pos.y=Mathf.Clamp(pos.y,0,height_restriction);
        transform.position=pos;
        
    }
}

Clamp an Integer value in Unity

Mathf.Clamp can take both floats and integers. Depending on the data type specified in the minimum and maximum ranges, it will change the data type of the output value.

C# script to Clamp an Integer

using UnityEngine;

public class Mathf_example1: MonoBehaviour
{
   int my_value=0;
   int min_value=10;
   int max_value=20f;

   void Update()
   {
      my_value+=1;
      my_value=Mathf.Clamp(my_value,min_value,max_value);
      Debug.Log(my_value);
   }

}

As you can see in the above script. We have used the same script from the float example, the only different between them is all the data types of the parameters included in the Mathf.Clamp function are of Integer type.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.