This article is from Unity Connect blogger Beijing Linyun Information Technology Co., LTD

A brief analysis of Mathf Mathematics Function library

Mathf. Absolute value of Abs

Computes and returns the absolute value of the specified argument f.

Mathf.Acos inverse cosine

static function Acos (f : float) : float

Computes in radians and returns the arccosine of the number specified in argument f.

3. Mathf

static function Approximately (a : float, b: float) : bool

Compare two floating point values to see if they are very close. Since floating point values are imprecise, it is not recommended to use equal to compare them. For example, 1.0==10.0/10.0 May not return true.

public class example : MonoBehaviour {
            public void Awake() {
              if(Mathf. Approximately (5.0 F, F / 50.0 50.0 F))print("Approximation"); }}Copy the code

Mathf.Asin arcsine

static function Asin (f : float) : float

Computs in radians and returns the arcsine of the number specified in parameter f.

Mathf.Atan2 arctangent

static function Atan2 (y : float, x :float) : float

Evaluates in radians and returns the arctangent of y/x. The return value represents the Angle opposite the right triangle, where x is the length of the adjacent side and y is the length of the opposite side.

The return value is the Angle between the X-axis and a two-dimensional vector that starts at 0 and ends at (x,y).

public class example : MonoBehaviour {
            public Transform target;
            void Update() {
                  Vector3 relative = transform.InverseTransformPoint(target.position);
                  floatangle = Mathf.Atan2(relative.x, relative.z) * Mathf.Rad2Deg; transform.Rotate(0,angle, 0); }}Copy the code

Mathf.Atan arctangent

static function Atan (f : float) :float

Computes and returns the arctangent value of the number specified in argument f. The return value is between negative two PI and positive two PI.

Mathf.CeilToInt Minimum integer

static function CeilToInt (f : float) : int

Returns the smallest integer greater than or equal to f.

8. Mathf.Ceil upper limit

static function Ceil (f : float) : float

Returns the upper limit of a number or expression specified by f. The upper limit of a number is the nearest integer greater than or equal to that number.

9, Mathf.Clamp01 limit 0~1

static function Clamp01 (value : float) :float

Limit value between 0 and 1 and return value. If value is less than 0, 0 is returned. Returns 1 if value is greater than 1, otherwise returns value.

10. Mathf.Clamp restrictions

static function Clamp (value : float, min :float, max : float) : float

Limit the value of value to between min and Max, and return min if value is less than min. If value is greater than Max, return Max, otherwise return value.

static function Clamp (value : int, min :int, max : int) : int

Limit the value of value between min and Max and return value.

Mathf.ClosestPowerOfTwo ClosestPowerOfTwo

static function ClosestPowerOfTwo (value :int) : int

Returns the nearest power of 2 to value.

Mathf. Cosine

static function Cos (f : float) : float

Returns the cosine of the Angle (between -1.0 and 1.0) specified by the argument f.

Mathf.Deg2Rad degrees turn radians

static var Deg2Rad : float

Degrees to radians. (read-only)

This is PI times 2 over 360.

Mathf.Rad2Deg Radian turn

static var Rad2Deg : float

Radian to degree conversion constant. (read-only)

This is equal to 360 over PI times 2.

Mathf.DeltaAngle

static function DeltaAngle (current :float, target : float) : float

Compute the shortest difference between two given angles.

/ / Prints 90 Debug Log (Mathf DeltaAngle (1080, living));Copy the code

Mathf.Epsilon small positive number

static var Epsilon : float

A small floating point value. (read-only)

The smallest floating point value, different from 0.

The following rules:

– anyValue + Epsilon = anyValue

– anyValue – Epsilon = anyValue

– 0 + Epsilon = Epsilon

– 0 – Epsilon = -Epsilon

A value between any number and Epsilon will result in a truncation error at any number.

public class example : MonoBehaviour {
bool isEqual(float a, float b) {
  if(a >= b - Mathf.Epsilon && a <= b + Mathf.Epsilon)
    return true;
    else
    return false; }}Copy the code

Mathf.Exp index

static function Exp (power : float) : float

Return e to the power.

18. Mathf.FloorToInt Maximum number

static function FloorToInt (f : float) :int

Returns the largest integer, less than or equal to f.

Mathf.Floor lower limit

static function Floor (f : float) : float

Returns the lower limit of the number or expression specified in argument f. A lower limit is the nearest integer less than or equal to a specified number or expression.

20, Mathf.Infinity is Infinity

static var Infinity : float

Which means infinity, which is infinity, infinity (read only)

21, InverseLerp inverse interpolation

Evaluates the Lerp parameter between the two values. The ratio of value between from and to.

// Now the parameter is 3/5float parameter =Mathf.InverseLerp(walkSpeed, runSpeed, speed);Copy the code

22, mathf. IsPowerOfTwo is a power of 2

static function IsPowerOfTwo (value : int): bool

Returns true if the value is a power of 2.

// prints false 
Debug.Log(Mathf.IsPowerOfTwo(7));
// prints true  
Debug.Log(Mathf.IsPowerOfTwo(32));Copy the code

23, MathF. LerpAngle Interpolation Angle

static function LerpAngle (a : float, b :float, t : float) : float

The same principle as Lerp ensures correct interpolation when they circle 360 degrees.

A and B are degrees.

public class example : MonoBehaviour {
public floatMinAngle = 0.0 F; publicfloatMaxAngle = 90.0 F; voidUpdate() {
      floatangle = Mathf.LerpAngle(minAngle, maxAngle, Time.time); transform.eulerAngles= new Vector3(0, angle, 0); }}}Copy the code

24, MathF. Lerp interpolation

static function Lerp (from : float, to :float, t : float) : float

Returns the interpolation between a and b based on the floating point t, which is limited to 0 ~ 1.

Returns from when t = 0, returns to when t = 1. Returns the average value of from and to when t = 0.5.

The logarithm of base 10

static function Log10 (f : float) : float

Returns the logarithm of f, base 10.

26, Mathf.Log

static function Log (f : float, p : float): float

Returns the logarithm of the argument f.

// logarithm of 6 inBase 2 // Logs of 6 base 2 // prints 2.584963print(Mathf.Log(6, 2));Copy the code

27, Mathf.Max Max

static function Max (a : float, b : float): float

static function Max (params values :float[]) : float

Returns the largest of two or more values.

28. Minimum mathf. Min

static function Min (a : float, b : float): float

static function Min (params values :float[]) : float

Returns the smallest of two or more values.

Mathf.MoveTowardsAngle

static function MoveTowardsAngle (current :float, target : float, maxDelta : float) : float

Like MoveTowards, but when they go around 360 degrees make sure the interpolation is correct.

The variables current and target are used as degrees. For optimization reasons, maxDelta negative values are not supported and may cause oscillations. Push current from target Angle and add 180 degree Angle instead.

30, MoveTowards

static function MoveTowards (current :float, target : float, maxDelta : float) : float

Change a current value towards the target value.

This is actually the same as mathF.lerp, but the function will ensure that we don’t outpace maxDelta. MaxDelta is negative to push the target away from.

Mathf.NegativeInfinity

static var NegativeInfinity : float

Minus infinity, which is infinitesimal, minus infinity (read only)

Mathf.NextPowerOfTwo next power of 2

//Prints 8 to the console 
Debug.Log(Mathf.NextPowerOfTwo(7));Copy the code

33, How do you do

static function PingPong (t : float, length: float) : float

Round trip between 0 and length. T is never greater than length, and it’s never less than 0.

The returned value will move back and forthbetween 0 and length.

The return value will move back and forth between 0 and length.

Mathf.PI PI

static var PI : float

The value of the PI (pai) read, that is, the value of the PI (PI) 3.14159265358979323846… (read-only)

MathF.POW

static function Pow (f : float, p : float): float

Compute and return f to the p.

36, Mathf.Repeat

static function Repeat (t : float, length :float) : float

The loop is between t, 0 and length. T is never greater than length, and it’s never less than 0.

This is similar to modular operators, but can use floating point numbers.

public class example : MonoBehaviour {
void Update() { transform.position= new Vector3(Mathf.Repeat(Time.time, 3), transform.position.y,transform.position.z); }}Copy the code

Mathf.RoundToInt Round to integer

static function RoundToInt (f : float) :int

Returns the value specified by f rounded to the nearest integer.

If the number ends at 0.5, so it is between two integers, whether even or odd, an even number will be returned.

38, Mathf.Round Round

static function Round (f : float) : float

Returns the floating point number f rounded to the nearest integer.

If the number ends in 0.5, so it is between two integers, even or odd, an even number will be returned.

39. The mathf. Sign symbol

static function Sign (f : float) : float

Returns the symbol of f.

Returns 1 if f is positive or 0, and -1 if f is negative.

40, Mathf. Sine

static function Sin (f : float) : float

Computes and returns the sine of the specified Angle f in radians.

41. Mathf.SmoothDampAngle

static function SmoothDampAngle (current :float, target : float, ref currentVelocity : float, smoothTime : float,maxSpeed : float = Mathf.Infinity, deltaTime : float = Time.deltaTime) : float

parameter

Current: indicates the current position.

Target: The position we are trying to achieve.

CurrentVelocity: the currentVelocity, which will be modified whenever you access the function.

SmoothTime (the Target faster) : Approximate time to reach the target location, but actually reach the target faster.

MaxSpeed: Optional parameter that allows you to limit the maximum speed.

DeltaTime: Time since the last time this function was called. The default value is time.deltatime.

Gradually change a given Angle to the desired Angle over time. This value is smoothed by some spring-like function of shock absorbers. This function can be used to smooth any kind of value, position, color, scalar. The most common is to smooth a follow camera.

Public class example: MonoBehaviour {public Transform target; // A simple smooth follow camera // follow target orientation public class example: MonoBehaviour {public Transform target; publicfloatSmooth = 0.3 F; publicfloatShort = 5.0 F; privatefloatYVelocity = 0.0 F; voidUpdate() {// Switch from the current y Angle to the target Y AnglefloatyAngle = Mathf.SmoothDampAngle(transform.eulerAngles.y, target.eulerAngles.y,ref yVelocity, smooth); Vector3 position = target. Position; Position += Quaternion.Euler(0, yAngle, 0) * new Vector3(0, 0, -distance); Transform. position= position; // LookAt the target transform.lookat (target); }}Copy the code

42, Mathf.SmoothDamp

static function SmoothDamp (current :float, target : float, ref currentVelocity : float, smoothTime : float,maxSpeed : float = Mathf.Infinity, deltaTime : float = Time.deltaTime) : float

parameter

Current: indicates the current position.

Target: The position we are trying to achieve.

CurrentVelocity: the currentVelocity, which will be modified whenever you access the function.

SmoothTime: Approximate time to reach the target position, faster when actually reaching the target.

MaxSpeed: Optional parameter that allows you to limit the maximum speed.

DeltaTime: Time since the last time this function was called. The default value is time.deltatime.

describe

Gradually change a value to the expected value over time.

This value is smoothed out as if by a spring shock absorber that does not crash. This function can be used to smooth any type of value, position, color, scalar.

public class example : MonoBehaviour {
    public Transform target;
    public floatSmoothTime = 0.3 F; privatefloatYVelocity = 0.0 F; voidUpdate() {
        floatnewPosition = Mathf.SmoothDamp(transform.position.y, target.position.y, refyVelocity, smoothTime); transform.position= new Vector3(transform.position.x, newPosition, transform.position.z); }}Copy the code

43, Mathf.SmoothStep Interpolation

static function SmoothStep (from : float,to : float, t : float) : float

Similar to LERP, interpolating between minimum and maximum, tapering in and out of limits.

public class example : MonoBehaviour {
    public floatMinimum = 10.0 F; publicfloatMaximum = 20.0 F; voidUpdate() { transform.position= new Vector3(Mathf.SmoothStep(minimum, maximum, Time.time), 0, 0); }}Copy the code

44, Mathf.Sqrt square root

static function Sqrt (f : float) : float

Compute and return the square root of f.

45, MathF. Tan tangent

Static function Tan (f: float) : float computes and returns the tangent of the specified Angle in radians f.

Unity3D official Mathematics library learning tutorial:

Docs.unity3d.com/ScriptRefer…

Math function diagrams used in C#


C # Math chart



The original link: connect.unity.com/p/unity3d-s…

You are welcome to click the link above, download the Unity official APP, and discuss with the bloggers. Here you can meet many interesting friends, and answer questions about online technology, and more practical things you can find.