## php教程

php教程，PHP资料，PHP技术交流

# UnityMathf中的数学运算

Mathf.Abs绝对值

Mathf.Acos反余弦

staticfunction Acos (f : float) : float

Mathf.Approximately近似

staticfunction Approximately (a : float, b: float) : bool

publicclass example : MonoBehaviour {

publicvoid Awake() {

if(Mathf.Approximately(1.0F, 10.0F / 10.0F))

print("same");

}

}

Mathf.Asin反正弦

staticfunction Asin (f : float) : float

Mathf.Atan2反正切

staticfunction Atan2 (y : float, x :float) : float

publicclass example : MonoBehaviour {

publicTransform target;

voidUpdate() {

Vector3relative =transform.InverseTransformPoint(target.position);

floatangle =Mathf.Atan2(relative.x, relative.z) * Mathf.Rad2Deg;

transform.Rotate(0,angle, 0);

}

}

Mathf.Atan反正切

staticfunction Atan (f : float) :float

Mathf.CeilToInt最小整数

staticfunction CeilToInt (f : float) : int

Mathf.Ceil上限值

staticfunction Ceil (f : float) : float

Mathf.Clamp01限制0~1

staticfunction Clamp01 (value : float) :float

Mathf.Clamp限制

staticfunction Clamp (value : float, min :float, max : float) : float

staticfunction Clamp (value : int, min :int, max : int) : int

Mathf.ClosestPowerOfTwo最近的二次方

staticfunction ClosestPowerOfTwo (value :int) : int

Mathf.Cos余弦

staticfunction Cos (f : float) : float

staticvar Deg2Rad : float

staticvar Rad2Deg : float

Mathf.DeltaAngle增量角

staticfunction DeltaAngle (current :float, target : float) : float

//Prints 90

Debug.Log(Mathf.DeltaAngle(1080,90));

Mathf.Epsilon小正数

staticvar Epsilon : float

-    anyValue + Epsilon = anyValue

-    anyValue - Epsilon = anyValue

-    0 + Epsilon = Epsilon

-    0 - Epsilon = -Epsilon

publicclass example : MonoBehaviour {

boolisEqual(float a, float b) {

if(a >= b -Mathf.Epsilon && a <= b + Mathf.Epsilon)

returntrue;

else

returnfalse;

}

}

Mathf.Exp指数

staticfunction Exp (power : float) : float

Mathf.FloorToInt最大整数

staticfunction FloorToInt (f : float) :int

Mathf.Floor下限值

staticfunction Floor (f : float) : float

Mathf.Infinity正无穷

staticvar Infinity : float

Mathf.InverseLerp反插值

//现在参数是3/5

floatparameter =Mathf.InverseLerp(walkSpeed, runSpeed, speed);

Mathf.IsPowerOfTwo是否2的幂

staticfunction IsPowerOfTwo (value : int): bool

//prints false

Debug.Log(Mathf.IsPowerOfTwo(7));

//prints true

Debug.Log(Mathf.IsPowerOfTwo(32));

Mathf.LerpAngle插值角度

staticfunction LerpAngle (a : float, b :float, t : float) : float

a和b是代表度数。

publicclass example : MonoBehaviour {

publicfloat minAngle = 0.0F;

publicfloat maxAngle = 90.0F;

voidUpdate() {

floatangle =Mathf.LerpAngle(minAngle, maxAngle, Time.time);

transform.eulerAngles=new Vector3(0, angle, 0);

}

}

Mathf.Lerp插值

staticfunction Lerp (from : float, to :float, t : float) : float

Mathf.Log10基数10的对数

staticfunction Log10 (f : float) : float

Mathf.Log对数

staticfunction Log (f : float, p : float): float

//logarithm of 6 in base 2

//以2为底6的对数

//prints 2.584963

print(Mathf.Log(6,2));

Mathf.Max最大值

staticfunction Max (a : float, b : float): float

staticfunction Max (params values :float[]) : float

Mathf.Min最小值

staticfunction Min (a : float, b : float): float

staticfunction Min (params values :float[]) : float

Mathf.MoveTowardsAngle移动角

staticfunction MoveTowardsAngle (current :float, target : float, maxDelta : float) :float

Mathf.MoveTowards移向

staticfunction MoveTowards (current :float, target : float, maxDelta : float) : float

Mathf.NegativeInfinity负无穷

staticvar NegativeInfinity : float

Mathf.NextPowerOfTwo下个2的幂

Mathf.PingPong乒乓

staticfunction PingPong (t : float, length: float) : float

0到length之间往返。t值永远不会大于length的值，也永远不会小于0。

Thereturned value will move back and forthbetween 0 and length.

Mathf.PI圆周率

staticvar PI : float

PI（读pai）的值，也就是圆周率（π）的值3.14159265358979323846...（只读）

Mathf.Pow次方

staticfunction Pow (f : float, p : float): float

Mathf.Repeat重复

staticfunction Repeat (t : float, length :float) : float

publicclass example : MonoBehaviour {

voidUpdate() {

transform.position= newVector3(Mathf.Repeat(Time.time, 3), transform.position.y,transform.position.z);

}

}

Mathf.RoundToInt四舍五入到整数

staticfunction RoundToInt (f : float) :int

Mathf.Round四舍五入

staticfunction Round (f : float) : float

Mathf.Sign符号

staticfunction Sign (f : float) : float

Mathf.Sin正弦

staticfunction Sin (f : float) : float

Mathf.SmoothDampAngle平滑阻尼角度

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

current

target

currentVelocity

smoothTime

thetarget faster.

maxSpeed

deltaTime

//一个简单的平滑跟随摄像机

//跟随目标的朝向

publicclass example : MonoBehaviour {

publicTransform target;

publicfloat smooth = 0.3F;

publicfloat distance = 5.0F;

privatefloat yVelocity = 0.0F;

voidUpdate() {

//从目前的y角度变换到目标y角度

floatyAngle = Mathf.SmoothDampAngle(transform.eulerAngles.y,target.eulerAngles.y,ref yVelocity, smooth);

//target的位置

Vector3position =target.position;

//然后，新角度之后的距离偏移

position+=Quaternion.Euler(0, yAngle, 0) * new Vector3(0, 0, -distance);

//应用位置

transform.position=position;

//看向目标

transform.LookAt(target);

}

}

Mathf.SmoothDamp平滑阻尼

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

current

target

currentVelocity

smoothTime

maxSpeed

deltaTime

publicclass example : MonoBehaviour {

publicTransform target;

publicfloat smoothTime = 0.3F;

privatefloat yVelocity = 0.0F;

voidUpdate() {

floatnewPosition =Mathf.SmoothDamp(transform.position.y, target.position.y, refyVelocity,smoothTime);

transform.position= newVector3(transform.position.x, newPosition, transform.position.z);

}

}

Mathf.SmoothStep平滑插值

staticfunction SmoothStep (from : float,to : float, t : float) : float

publicclass example : MonoBehaviour {

publicfloat minimum = 10.0F;

publicfloat maximum = 20.0F;

voidUpdate() {

transform.position= newVector3(Mathf.SmoothStep(minimum, maximum, Time.time), 0, 0);

}

}

Mathf.Sqrt平方根

staticfunction Sqrt (f : float) : float

Mathf.Tan正切

staticfunction Tan (f : float) : float

#### Unity常用数学计算公式

2016-11-16 17:48:20

#### unity中Mathf数学函数

2017-09-27 19:34:55

#### Unity中的Mathf类

2015-07-30 21:30:19

#### c#自带的数学运算函数

2015-06-23 10:30:58

#### Unity Mathf类 笔记

2017-05-06 16:07:30

#### Mathf类

2016-09-08 11:47:58

#### Unity Mathf 数学运算(C#)

2017-07-19 10:29:26

#### Unity3d Mathf数学运算说明

2015-07-07 14:35:46

#### Unity Mathf 数学运算(C#)

2015-09-30 10:56:23

#### Unity Mathf 数学运算(C#)

2017-08-23 17:06:21