很有意思的Mathf功能

Mathf.Abs 绝对值

返回指定参数 f 绝对值

Debug.Log(Mathf.Abs(-3));	// Prints 3

Mathf.Approximately 近似

如果a和b相似,则返回true。

if (Mathf.Approximately(1.0f, 10.0f / 10.0f))	//sometimes return true;

Mathf.Ceil 向上取整

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

Mathf.Floor 向下取整

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

Mathf.Round 取整

如果数字末尾是.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

Mathf.Max 最大值 / Mathf.Min最小值

Debug.Log(Mathf.Max(1,3,5,7,9));	//Prints 9
Debug.Log(Mathf.Min(1,3,5,7,9));	//Prints 1

Mathf.Clamp 限制

限制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

Mathf.Clamp01 限制0~1

限制value的值在0和1之间并返回值。

Debug.Log(Mathf.Clamp01(0.5));		// Prints 0.5
Debug.Log(Mathf.Clamp01(1.5));		// Prints 1

Mathf.Lerp 插值 / Mathf.InverseLerp 反插值

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

Mathf.MoveTowards 移向

改变一个当前值向目标值靠近,速度不会超过maxDelta。

void Update()
{
    
    
	currStrength = Mathf.MoveTowards(currStrength, maxStrength, recoveryRate * Time.deltaTime);
}

Mathf.Pow 次方/ Mathf.Sqrt 开方

debug.Log(Mathf.Pow(2, 3));		//2^3 = 8
debug.Log(Mathf.Sqrt(9));		//开方9 =3

Mathf.PingPong 乒乓 / Mathf.Repeat重复

乒乓值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

Mathf.Sign 符号

Debug.Log(Mathf.Sign(-10));	//Prints -1
Debug.Log(Mathf.Sign(0)); //Prints 1
Debug.Log(Mathf.Sign(10)); //Prints 1

虽然不是Mathf而是运算符但还是记下

bool yes = true;
debug.Log(yes ? 50 : 3);	//Prints 50
bool no = false;
debug.Log(no ? 30 : 5);		//Prints 5

猜你喜欢

转载自blog.csdn.net/MikeW138/article/details/95611555