- Home /
Question by
ItArth · May 09, 2021 at 10:20 PM ·
scripting problemclamp
Clamp a value and return 0.1 if its 3 or 3 if it 0.1 etc
I have a Drop system and each item have a weight i clamped the weight so its always returns 3 if item weight is greatear than 3 and 0.1 if is lower than 0.1 .
This script below is that what im searching but if i whant to change max value that is 3 or min value i need to change the entire code, the simplest way to do it?
private float DropForce (float num1)
{
//Num1 is clamped item weight.
if (num1 == 3)
{
num1 = 0.1f;
}
if (num1 == 2.9f)
{
num1 = 0.2f;
}
if (num1 == 2.8f)
{
num1 == 0.3f;
}
// And the inverse
if (num1 == 0.1f)
{
num1 == 3f;
}
if(num1 == 0.2)
{
num1 = 2.9f
}
//Etc
return num1;
}
Comment
Best Answer
Answer by vargata · May 10, 2021 at 03:01 AM
public float min = 0.1f;
public float max = 3.0f;
float functionname(float input){
float a = Mathf.Clamp(input, min, max);
a = min + max - a;
return a;
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613033527im_/https://answers.unity.com/themes/thub/images/avi.jpg)