Search code examples
c#unity-game-enginefloating-pointclamp

How to clamp a float using other floats


I know this may sound like quite a dumb question, sorry, but is it possible to clamp a float, using other floats. ie:

public float float1;
public float float2 -1;
public float float3 1;

void Start()
{
   Mathf.Clamp (float1, float2, float3)
}

Solution

  • In general, yes it is, Mathf.Clamp has two overloads:

    public static float Clamp(float value, float min, float max);

    public static int Clamp(int value, int min, int max);

    But, well you have to assign the result to something ;)

    float1 = Mathf.Clamp (float1, float2, float3);
    

    and your declarations are missing =

    public float float1;
    public float float2 = -1;
    public float float3 = 1;