天天看點

[Unity3d]Unity Mathf 數學運算(C#)

Mathf.Abs絕對值 

計算并傳回指定參數 f 絕對值。 

Mathf.Acos反餘弦 

static function Acos (f : float) : float 

以弧度為機關計算并傳回參數 f 中指定的數字的反餘弦值。 

Mathf.Approximately近似 

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

比較兩個浮點數值,看它們是否非常接近, 由于浮點數值不精确,不建議使用等于來比較它們。例如,1.0==10.0/10.0也許不會傳回true。 

public class example : MonoBehaviour { 

            publicvoid Awake() { 

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

                                    print("same"); 

            } 

Mathf.Asin反正弦 

static function Asin (f : float) : float 

以弧度為機關計算并傳回參數 f 中指定的數字的反正弦值。 

Mathf.Atan2反正切 

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

以弧度為機關計算并傳回 y/x 的反正切值。傳回值表示相對直角三角形對角的角,其中 x 是臨邊邊長,而 y 是對邊邊長。 

傳回值是在x軸和一個二維向量開始于0個結束在(x,y)處之間的角。 

            publicTransform target; 

            voidUpdate() { 

                        Vector3relative = transform.InverseTransformPoint(target.position); 

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

                        transform.Rotate(0,angle, 0); 

Mathf.Atan反正切 

static function Atan (f : float) :float 

計算并傳回參數 f 中指定的數字的反正切值。傳回值介于負二分之 pi 與正二分之 pi 之間。 

Mathf.CeilToInt最小整數 

static function CeilToInt (f : float) : int 

傳回最小的整數大于或等于f。 

Mathf.Ceil上限值 

static function Ceil (f : float) : float 

傳回 f 指定數字或表達式的上限值。數字的上限值是大于等于該數字的最接近的整數。 

Mathf.Clamp01限制0~1 

static function Clamp01 (value : float) :float 

限制value在0,1之間并傳回value。如果value小于0,傳回0。如果value大于1,傳回1,否則傳回value 。 

Mathf.Clamp限制 

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

限制value的值在min和max之間, 如果value小于min,傳回min。 如果value大于max,傳回max,否則傳回value 

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

限制value的值在min和max之間,并傳回value。 

Mathf.ClosestPowerOfTwo最近的二次方 

static function ClosestPowerOfTwo (value :int) : int 

傳回距離value最近的2的次方數。 

Mathf.Cos餘弦 

static function Cos (f : float) : float 

傳回由參數 f 指定的角的餘弦值(介于 -1.0 與 1.0 之間的值)。 

Mathf.Deg2Rad度轉弧度 

static var Deg2Rad : float 

度到弧度的轉化常量。(隻讀) 

這等于(PI * 2) / 360。 

Mathf.Mathf.Rad2Deg 弧度轉度 

static var Rad2Deg : float 

弧度到度的轉化常量。(隻讀) 

這等于 360 / (PI * 2)。 

Mathf.DeltaAngle增量角 

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

計算給定的兩個角之間最短的差異。 

// Prints 90 

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

Mathf.Epsilon小正數 

static var Epsilon : float 

一個很小的浮點數值。(隻讀) 

最小的浮點值,不同于0。 

以下規則: 

-    anyValue + Epsilon = anyValue 

-    anyValue - Epsilon = anyValue 

-    0 + Epsilon = Epsilon 

-    0 - Epsilon = -Epsilon 

一個在任意數和Epsilon的之間值将導緻在任意數發生截斷誤差。 

            boolisEqual(float a, float b) { 

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

                                    returntrue; 

                        else 

                                    returnfalse; 

Mathf.Exp指數 

static function Exp (power : float) : float 

傳回 e 的 power 次方的值。 

Mathf.FloorToInt最大整數 

static function FloorToInt (f : float) :int 

傳回最大的整數,小于或等于f。 

Mathf.Floor下限值 

static function Floor (f : float) : float 

傳回參數 f 中指定的數字或表達式的下限值。下限值是小于等于指定數字或表達式的最接近的整數。 

Mathf.Infinity正無窮 

static var Infinity : float 

表示正無窮,也就是無窮大,∞ (隻讀) 

Mathf.InverseLerp反插值 

計算兩個值之間的Lerp參數。也就是value在from和to之間的比例值。 

//現在參數是3/5 

float parameter =Mathf.InverseLerp(walkSpeed, runSpeed, speed); 

Mathf.IsPowerOfTwo是否2的幂 

static function IsPowerOfTwo (value : int): bool 

如果該值是2的幂,傳回true。 

// prints false 

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

// prints true 

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

Mathf.LerpAngle插值角度 

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

和Lerp的原理一樣,當他們環繞360度確定插值正确。 

a和b是代表度數。 

            publicfloat minAngle = 0.0F; 

            publicfloat maxAngle = 90.0F; 

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

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

Mathf.Lerp插值 

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

基于浮點數t傳回a到b之間的插值,t限制在0~1之間。 

當t = 0傳回from,當t = 1 傳回to。當t = 0.5 傳回from和to的平均值。 

Mathf.Log10基數10的對數 

static function Log10 (f : float) : float 

傳回f的對數,基數為10。 

Mathf.Log對數 

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

傳回參數 f 的對數。 

// logarithm of 6 in base 2 

//以2為底6的對數 

// prints 2.584963 

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

Mathf.Max最大值 

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

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

傳回兩個或更多值中最大的值。 

Mathf.Min最小值 

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

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

傳回兩個或更多值中最小的值。 

Mathf.MoveTowardsAngle移動角 

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

像MoveTowards,但是當它們環繞360度確定插值正确。 

變量current和target是作為度數。為優化原因,maxDelta負值的不被支援,可能引起振蕩。從target角推開current,添加180度角代替。 

Mathf.MoveTowards移向 

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

改變一個目前值向目标值靠近。 

這實際上和 Mathf.Lerp相同,而是該函數将確定我們的速度不會超過maxDelta。maxDelta為負值将目标從推離。 

Mathf.NegativeInfinity負無窮 

static var NegativeInfinity : float 

表示負無窮,也就是無窮小,-∞(隻讀) 

Mathf.NextPowerOfTwo下個2的幂 

Mathf.PingPong乒乓 

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

0到length之間往返。t值永遠不會大于length的值,也永遠不會小于0。 

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

傳回值将在0和length之間來回移動。 

Mathf.PI圓周率 

static var PI : float 

PI(讀pai)的值,也就是圓周率(π)的值3.14159265358979323846...(隻讀) 

Mathf.Pow次方 

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

計算并傳回 f 的 p 次方。 

Mathf.Repeat重複 

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

循環數值t,0到length之間。t值永遠不會大于length的值,也永遠不會小于0。 

這是類似于模運算符,但可以使用浮點數。 

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

Mathf.RoundToInt四舍五入到整數 

static function RoundToInt (f : float) :int 

傳回 f 指定的值四舍五入到最近的整數。 

如果數字末尾是.5,是以它是在兩個整數中間,不管是偶數或是奇數,将傳回偶數。 

Mathf.Round四舍五入 

static function Round (f : float) : float 

傳回浮點數 f 進行四舍五入最接近的整數。 

Mathf.Sign符号 

static function Sign (f : float) : float 

傳回 f 的符号。 

當 f 為正或為0傳回1,為負傳回-1。 

Mathf.Sin正弦 

static function Sin (f : float) : float 

計算并傳回以弧度為機關指定的角 f 的正弦值。 

Mathf.SmoothDampAngle平滑阻尼角度 

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

參數 

current 

目前的位置。 

target 

我們試圖達到的位置。 

currentVelocity 

目前速度,這個值在你通路這個函數的時候會被随時修改。 

smoothTime 

the target faster. 

要到達目标位置的近似時間,實際到達目标時要快一些。 

maxSpeed 

可選參數,允許你限制的最大速度。 

deltaTime 

上次調用該函數到現在的時間。預設為Time.deltaTime。 

随着時間的推移逐漸改變一個給定的角度到期望的角度。 

這個值通過一些彈簧減震器類似的功能被平滑。這個函數可以用來平滑任何一種值,位置,顔色,标量。最常見的是平滑一個跟随錄影機。 

//一個簡單的平滑跟随錄影機 

//跟随目标的朝向 

            publicfloat smooth = 0.3F; 

            publicfloat distance = 5.0F; 

            privatefloat yVelocity = 0.0F; 

//從目前的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平滑阻尼 

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

描述 

随着時間的推移逐漸改變一個值到期望值。 

這個值就像被一個不會崩潰的彈簧減振器一樣被平滑。這個函數可以用來平滑任何類型的值,位置,顔色,标量。

            publicfloat smoothTime = 0.3F; 

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

                        transform.position= new Vector3(transform.position.x, newPosition, transform.position.z); 

Mathf.SmoothStep平滑插值 

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

和lerp類似,在最小和最大值之間的插值,并在限制處漸入漸出。 

            publicfloat minimum = 10.0F; 

            publicfloat maximum = 20.0F; 

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

Mathf.Sqrt平方根 

static function Sqrt (f : float) : float 

計算并傳回 f 的平方根。 

Mathf.Tan正切 

static function Tan (f : float) : float 

計算并傳回以弧度為機關 f 指定角度的正切值。

本文轉蓬萊仙羽51CTO部落格,原文連結:http://blog.51cto.com/dingxiaowei/1366220,如需轉載請自行聯系原作者