1

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)
}

1 Answers1

2

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;
derHugo
  • 83,094
  • 9
  • 75
  • 115
  • thanks, that was why it wasn't working, I'll keep this is mind for next time :) – CyberScope _ Aug 18 '21 at 15:03
  • also, sorry to bother you again but do you put mathf.clamp in the start function or the update function or both? – CyberScope _ Aug 18 '21 at 15:06
  • 1
    @CyberScope_ I'd say that pretty much depends on where you need it ^^ If you put it into `Start` it is only applied once and any later change on these fields doesn't matter ... if you need to continously clamp this every frame then it goes into `Update` – derHugo Aug 18 '21 at 15:08
  • Thanks again I think I understand also thanks for reminding me about the equal signs – CyberScope _ Aug 18 '21 at 15:10