http://www.manew.com/blog-76890-3801.html
數(shù)學運算在編程中挺重要的,所以看到了就順便摘錄下來,方便以后用。
1.Mathf.Abs絕對值計算并返回指定參數(shù) f 絕對值。2Mathf.Acos反余弦static function Acos (f : float) : float以弧度為單位計算并返回參數(shù) f 中指定的數(shù)字的反余弦值。3.Mathf.ApPRoximately近似static function Approximately (a : float, b: float) : bool比較兩個浮點數(shù)值,看它們是否非常接近, 由于浮點數(shù)值不精確,不建議使用等于來比較它們。例如,1.0==10.0/10.0也許不會返回true。public class example : MonoBehaviour { publicvoid Awake() { if(Mathf.Approximately(1.0F, 10.0F / 10.0F)) print("same"); }}4.Mathf.Asin反正static function Asin (f : float) : float以弧度為單位計算并返回參數(shù) f 中指定的數(shù)字的反正弦值。5.Mathf.Atan2反正切static function Atan2 (y : float, x :float) : float以弧度為單位計算并返回 y/x 的反正切值。返回值表示相對直角三角形對角的角,其中 x 是臨邊邊長,而 y 是對邊邊長。返回值是在x軸和一個二維向量開始于0個結束在(x,y)處之間的角。public class example : MonoBehaviour { publicTransform target; voidUpdate() { Vector3relative = transform.InverseTransformPoint(target.position); floatangle = Mathf.Atan2(relative.x, relative.z) * Mathf.Rad2Deg; transform.Rotate(0,angle, 0); }}6.Mathf.Atan反正切static function Atan (f : float) :float計算并返回參數(shù) f 中指定的數(shù)字的反正切值。返回值介于負二分之 pi 與正二分之 pi 之間。7.Mathf.CeilToInt最小整數(shù)static function CeilToInt (f : float) : int返回最小的整數(shù)大于或等于f。8.Mathf.Ceil上限值static function Ceil (f : float) : float返回 f 指定數(shù)字或表達式的上限值。數(shù)字的上限值是大于等于該數(shù)字的最接近的整數(shù)。9.Mathf.Clamp01限制0~1static function Clamp01 (value : float) :float限制value在0,1之間并返回value。如果value小于0,返回0。如果value大于1,返回1,否則返回value 。10.Mathf.Clamp限制static function Clamp (value : float, min :float, max : float) : float限制value的值在min和max之間, 如果value小于min,返回min。 如果value大于max,返回max,否則返回valuestatic function Clamp (value : int, min :int, max : int) : int限制value的值在min和max之間,并返回value。11.Mathf.ClosestPowerOfTwo最近的二次方static function ClosestPowerOfTwo (value :int) : int返回距離value最近的2的次方數(shù)。12.Mathf.Cos余弦static function Cos (f : float) : float返回由參數(shù) f 指定的角的余弦值(介于 -1.0 與 1.0 之間的值)。13.Mathf.Deg2Rad度轉弧度static var Deg2Rad : float度到弧度的轉化常量。(只讀)這等于(PI * 2) / 360。14.Mathf.Mathf.Rad2Deg 弧度轉度static var Rad2Deg : float弧度到度的轉化常量。(只讀)這等于 360 / (PI * 2)。15.Mathf.DeltaAngle增量角static function DeltaAngle (current :float, target : float) : float計算給定的兩個角之間最短的差異。// Prints 90Debug.Log(Mathf.DeltaAngle(1080,90));16.Mathf.Epsilon小正數(shù)static var Epsilon : float一個很小的浮點數(shù)值。(只讀)最小的浮點值,不同于0。以下規(guī)則:- anyValue + Epsilon = anyValue- anyValue - Epsilon = anyValue- 0 + Epsilon = Epsilon- 0 - Epsilon = -Epsilon一個在任意數(shù)和Epsilon的之間值將導致在任意數(shù)發(fā)生截斷誤差。public class example : MonoBehaviour { boolisEqual(float a, float b) { if(a >= b - Mathf.Epsilon && a <= b + Mathf.Epsilon) returntrue; else returnfalse; }}17.Mathf.Exp指數(shù)static function Exp (power : float) : float返回 e 的 power 次方的值。18.Mathf.FloorToInt最大整數(shù)static function FloorToInt (f : float) :int返回最大的整數(shù),小于或等于f。19.Mathf.Floor下限值static function Floor (f : float) : float返回參數(shù) f 中指定的數(shù)字或表達式的下限值。下限值是小于等于指定數(shù)字或表達式的最接近的整數(shù)。20.Mathf.Infinity正無窮static var Infinity : float表示正無窮,也就是無窮大,∞ (只讀)21.Mathf.InverseLerp反插值計算兩個值之間的Lerp參數(shù)。也就是value在from和to之間的比例值。//現(xiàn)在參數(shù)是3/5float parameter =Mathf.InverseLerp(walkSpeed, runSpeed, speed);Mathf.IsPowerOfTwo是否2的冪static function IsPowerOfTwo (value : int): bool如果該值是2的冪,返回true。// prints falseDebug.Log(Mathf.IsPowerOfTwo(7));// prints trueDebug.Log(Mathf.IsPowerOfTwo(32));22.Mathf.LerpAngle插值角度static function LerpAngle (a : float, b :float, t : float) : float和Lerp的原理一樣,當他們環(huán)繞360度確保插值正確。a和b是代表度數(shù)。public class 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); }}23.Mathf.Lerp插值static function Lerp (from : float, to :float, t : float) : float基于浮點數(shù)t返回a到b之間的插值,t限制在0~1之間。當t = 0返回from,當t = 1 返回to。當t = 0.5 返回from和to的平均值。24.Mathf.Log10基數(shù)10的對數(shù)static function Log10 (f : float) : float返回f的對數(shù),基數(shù)為10。25.Mathf.Log對數(shù)static function Log (f : float, p : float): float返回參數(shù) f 的對數(shù)。// logarithm of 6 in base 2//以2為底6的對數(shù)// prints 2.584963print(Mathf.Log(6, 2));26.Mathf.Max最大值static function Max (a : float, b : float): floatstatic function Max (params values :float[]) : float返回兩個或更多值中最大的值。27.Mathf.Min最小值static function Min (a : float, b : float): floatstatic function Min (params values :float[]) : float返回兩個或更多值中最小的值。28.Mathf.MoveTowardsAngle移動角static function MoveTowardsAngle (current :float, target : float, maxDelta : float) : float像MoveTowards,但是當它們環(huán)繞360度確保插值正確。變量current和target是作為度數(shù)。為優(yōu)化原因,maxDelta負值的不被支持,可能引起振蕩。從target角推開current,添加180度角代替。29.Mathf.MoveTowards移向static function MoveTowards (current :float, target : float, maxDelta : float) : float改變一個當前值向目標值靠近。這實際上和 Mathf.Lerp相同,而是該函數(shù)將確保我們的速度不會超過maxDelta。maxDelta為負值將目標從推離。30.Mathf.NegativeInfinity負無窮static var NegativeInfinity : float表示負無窮,也就是無窮小,-∞(只讀)31.Mathf.NextPowerOfTwo下個2的冪32.Mathf.PingPong乒乓static function PingPong (t : float, length: float) : float0到length之間往返。t值永遠不會大于length的值,也永遠不會小于0。The returned value will move back and forthbetween 0 and length.返回值將在0和length之間來回移動。33.Mathf.PI圓周率static var PI : floatPI(讀pai)的值,也就是圓周率(π)的值3.14159265358979323846...(只讀) 34.Mathf.Pow次方static function Pow (f : float, p : float): float計算并返回 f 的 p 次方。35.Mathf.Repeat重復static function Repeat (t : float, length :float) : float循環(huán)數(shù)值t,0到length之間。t值永遠不會大于length的值,也永遠不會小于0。這是類似于模運算符,但可以使用浮點數(shù)。public class example : MonoBehaviour { voidUpdate() { transform.position= new Vector3(Mathf.Repeat(Time.time, 3), transform.position.y,transform.position.z); }}36.Mathf.RoundToInt四舍五入到整數(shù)static function RoundToInt (f : float) :int返回 f 指定的值四舍五入到最近的整數(shù)。如果數(shù)字末尾是.5,因此它是在兩個整數(shù)中間,不管是偶數(shù)或是奇數(shù),將返回偶數(shù)。37.Mathf.Round四舍五入static function Round (f : float) : float返回浮點數(shù) f 進行四舍五入最接近的整數(shù)。如果數(shù)字末尾是.5,因此它是在兩個整數(shù)中間,不管是偶數(shù)或是奇數(shù),將返回偶數(shù)。38.Mathf.Sign符號static function Sign (f : float) : float返回 f 的符號。當 f 為正或為0返回1,為負返回-1。39.Mathf.Sin正弦static function Sin (f : float) : float計算并返回以弧度為單位指定的角 f 的正弦值。40.Mathf.SmoothDampAngle平滑阻尼角度static function SmoothDampAngle (current :float, target : float, ref currentVelocity : float, smoothTime : float,maxSpeed : float = Mathf.Infinity, deltaTime : float = Time.deltaTime) : float 參數(shù)current 當前的位置。target 我們試圖達到的位置。currentVelocity 當前速度,這個值在你訪問這個函數(shù)的時候會被隨時修改。smoothTime the target faster. 要到達目標位置的近似時間,實際到達目標時要快一些。maxSpeed 可選參數(shù),允許你限制的最大速度。deltaTime 上次調用該函數(shù)到現(xiàn)在的時間。缺省為Time.deltaTime。隨著時間的推移逐漸改變一個給定的角度到期望的角度。這個值通過一些彈簧減震器類似的功能被平滑。這個函數(shù)可以用來平滑任何一種值,位置,顏色,標量。最常見的是平滑一個跟隨攝像機。//一個簡單的平滑跟隨攝像機//跟隨目標的朝向public class 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); }}41.Mathf.SmoothDamp平滑阻尼static function SmoothDamp (current :float, target : float, ref currentVelocity : float, smoothTime : float,maxSpeed : float = Mathf.Infinity, deltaTime : float = Time.deltaTime) : float參數(shù)current 當前的位置。target 我們試圖達到的位置。currentVelocity 當前速度,這個值在你訪問這個函數(shù)的時候會被隨時修改。smoothTime 要到達目標位置的近似時間,實際到達目標時要快一些。maxSpeed 可選參數(shù),允許你限制的最大速度。deltaTime 上次調用該函數(shù)到現(xiàn)在的時間。缺省為Time.deltaTime。描述隨著時間的推移逐漸改變一個值到期望值。這個值就像被一個不會崩潰的彈簧減振器一樣被平滑。這個函數(shù)可以用來平滑任何類型的值,位置,顏色,標量。public class 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= new Vector3(transform.position.x, newPosition, transform.position.z); }}42.Mathf.SmoothStep平滑插值static function SmoothStep (from : float,to : float, t : float) : float和lerp類似,在最小和最大值之間的插值,并在限制處漸入漸出。public class example : MonoBehaviour { publicfloat minimum = 10.0F; publicfloat maximum = 20.0F; voidUpdate() { transform.position= new Vector3(Mathf.SmoothStep(minimum, maximum, Time.time), 0, 0); }}43.Mathf.Sqrt平方根static function Sqrt (f : float) : float計算并返回 f 的平方根。44.Mathf.Tan正切static function Tan (f : float) : float計算并返回以弧度為單位 f 指定角度的正切值。
新聞熱點
疑難解答