Very interesting Mathf function

Mathf.Abs absolute value

Returns the absolute value of the specified parameter f

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

Mathf.Approximately approximate

If a and b are similar, return true.

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

Mathf.Ceil Improvement Arrangement

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 round down

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 round

If the end of the number is .5, so it is between two integers, whether it is even or odd, it will return an even number.

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 maximum / Mathf.Min minimum

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

Mathf.Clamp limitations

Limit the value of value between min and max. If value is less than min, return min. If value is greater than max, return max, otherwise return 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 limit 0~1

Limit the value of value between 0 and 1 and return the value.

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

Mathf.Lerp interpolation/ Mathf.InverseLerp inverse interpolation

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 move to

Change a current value to approach the target value, the speed will not exceed maxDelta.

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

Mathf.Pow power / Mathf.Sqrt square

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

Mathf.PingPong/ Mathf.Repeat

Ping-pong value t, the returned value will move back and forth between 0 and the length.
The loop value t will not be greater than the length, nor will it be less than 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);
}

Note: It is Time.time, not Time.deltaTime! !

for (int i = 0; i < 5; i++)
{
    
    
	Debug.Log(Mathf.PingPong(i,3));
	//0123210123210123210...
}

You can also use for loop

Mathf.Sign symbol

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

Although it is not Mathf but operator, but still write down

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

Guess you like

Origin blog.csdn.net/MikeW138/article/details/95611555