返回指定参数 f 绝对值
Debug.Log(Mathf.Abs(-3)); // Prints 3
如果a和b相似,则返回true。
if (Mathf.Approximately(1.0f, 10.0f / 10.0f)) //sometimes return true;
Debug.Log(Mathf.Ceil(10.0F)); // Prints 10
Debug.Log(Mathf.Ceil(10.2F)); // Prints 11
Debug.Log(Mathf.Ceil(10.7F)); // Prints 11
Debug.Log(Mathf.Ceil(-10.0F)); // Prints -10
Debug.Log(Mathf.Ceil(-10.2F)); // Prints -10
Debug.Log(Mathf.Ceil(-10.7F)); // Prints -10
Debug.Log(Mathf.Floor(10.0F)); // Prints 10
Debug.Log(Mathf.Floor(10.2F)); // Prints 10
Debug.Log(Mathf.Floor(10.7F)); // Prints 10
Debug.Log(Mathf.Floor(-10.0F)); // Prints -10
Debug.Log(Mathf.Floor(-10.2F)); // Prints -11
Debug.Log(Mathf.Floor(-10.7F)); // Prints -11
如果数字末尾是.5,因此它是在两个整数中间,不管是偶数或是奇数,将返回偶数。
Debug.Log(Mathf.Round(10.5f)); //Prints 10
Debug.Log(Mathf.Round(11.5f)); //Prints 12
Debug.Log(Mathf.Round(-10.5f)); //Prints -10
Debug.Log(Mathf.Round(-11.5f)); //Prints -12
Debug.Log(Mathf.Max(1,3,5,7,9)); //Prints 9
Debug.Log(Mathf.Min(1,3,5,7,9)); //Prints 1
限制value的值在min和max之间, 如果value小于min,返回min。 如果value大于max,返回max,否则返回value
float min = 1;
float max = 2;
Debug.Log(Mathf.Clamp(0.5, min, max)); // Prints 1
Debug.Log(Mathf.Clamp(1.5, min, max)); // Prints 1.5
限制value的值在0和1之间并返回值。
Debug.Log(Mathf.Clamp01(0.5)); // Prints 0.5
Debug.Log(Mathf.Clamp01(1.5)); // Prints 1
debug.Log(Mathf.Lerp(50, 100, 0.5)); //Prints 75
debug.Log(Mathf.InverseLerp (50, 100, 75)); //Prints 0.5
debug.Log(Mathf.Lerp(-50, 50, 0.75)); //Prints 25
debug.Log(Mathf.InverseLerp (-50, 50, 25)); //Prints 0.75
debug.Log(Mathf.Lerp(0, 2, -1)); //Prints 0
debug.Log(Mathf.Lerp(0, -2, 1)); //Prints -2
debug.Log(Mathf.LerpUnclamped(0,2,-1)); //Prints -2
debug.Log(Mathf.InverseLerp (0, 2, 3)); //Prints 1
Mathf.Lerp(a, b, t) = (b - a) * Mathf.clamp01(t) + a ; || Mathf.clamp((b - a) * t + a, a, b) ;
Mathf.InverseLerp(a, b, t) = Mathf.clamp01((t - a) / (b - a)) ;
Mathf.Lerp(0, b, t) = Mathf.clamp01(t) * b ;
Mathf.InverseLerp(0, b, t) = Mathf.clamp01(t / b) ;
改变一个当前值向目标值靠近,速度不会超过maxDelta。
void Update()
{
currStrength = Mathf.MoveTowards(currStrength, maxStrength, recoveryRate * Time.deltaTime);
}
debug.Log(Mathf.Pow(2, 3)); //2^3 = 8
debug.Log(Mathf.Sqrt(9)); //开方9 =3
乒乓值t,返回的值将在0和长度之间来回移动。
循环数值t,不会大于长度,也不会小于0。
void Update()
{
// Set the x position to loop between 0 and 3
transform.position = new Vector3(Mathf.PingPong(Time.time, 3), 0,0);
transform.position = new Vector3(Mathf.Repeat(Time.time, 3), 0,0);
}
注意:是Time.time,不是Time.deltaTime!!
for (int i = 0; i < 5; i++)
{
Debug.Log(Mathf.PingPong(i,3));
//0123210123210123210...
}
亦可以用for loop
Debug.Log(Mathf.Sign(-10)); //Prints -1
Debug.Log(Mathf.Sign(0)); //Prints 1
Debug.Log(Mathf.Sign(10)); //Prints 1
bool yes = true;
debug.Log(yes ? 50 : 3); //Prints 50
bool no = false;
debug.Log(no ? 30 : 5); //Prints 5