本文轉(zhuǎn)自Unity Connect博主?北京琳云信息科技有限責(zé)任公司
Mathf 數(shù)學(xué)函數(shù)庫(kù)淺析
1、Mathf.Abs 絕對(duì)值
計(jì)算并返回指定參數(shù) f 絕對(duì)值促王。
2、Mathf.Acos 反余弦
static function Acos (f : float) : float
以弧度為單位計(jì)算并返回參數(shù) f 中指定的數(shù)字的反余弦值。
3劝贸、Mathf.Approximately 近似
static function Approximately (a : float, b: float) : bool
比較兩個(gè)浮點(diǎn)數(shù)值,看它們是否非常接近, 由于浮點(diǎn)數(shù)值不精確逗宁,不建議使用等于來(lái)比較它們映九。例如,1.0==10.0/10.0也許不會(huì)返回true瞎颗。
public class example : MonoBehaviour {
? ? ? ? ? ? public void Awake() {
? ? ? ? ? ? ? if(Mathf.Approximately(5.0F, 50.0F / 50.0F))
? ? ? ? ? ? ? ? print("近似");
? ? ? ? ? ? }
}
4件甥、Mathf.Asin 反正弦
static function Asin (f : float) : float
以弧度為單位計(jì)算并返回參數(shù) f 中指定的數(shù)字的反正弦值。
5哼拔、Mathf.Atan2 反正切
static function Atan2 (y : float, x :float) : float
以弧度為單位計(jì)算并返回 y/x 的反正切值引有。返回值表示相對(duì)直角三角形對(duì)角的角,其中 x 是臨邊邊長(zhǎng)倦逐,而 y 是對(duì)邊邊長(zhǎng)譬正。
返回值是在 x 軸和一個(gè)二維向量開始于 0 個(gè)結(jié)束在 (x,y) 處之間的角。
public class example : MonoBehaviour {
? ? ? ? ? ? public Transform target;
? ? ? ? ? ? void Update() {
? ? ? ? ? ? ? ? ? Vector3 relative = transform.InverseTransformPoint(target.position);
? ? ? ? ? ? ? ? ? float angle = Mathf.Atan2(relative.x, relative.z) * Mathf.Rad2Deg;
? ? ? ? ? ? ? ? ? transform.Rotate(0,angle, 0);
? ? ? ? ? ? }
}
6、Mathf.Atan 反正切
static function Atan (f : float) :float
計(jì)算并返回參數(shù) f 中指定的數(shù)字的反正切值导帝。返回值介于負(fù)二分之 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ù)字或表達(dá)式的上限值。數(shù)字的上限值是大于等于該數(shù)字的最接近的整數(shù)虐秦。
9平酿、Mathf.Clamp01 限制 0~1
static 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 栖袋,否則返回 value 拍顷。
static 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 度轉(zhuǎn)弧度
static var Deg2Rad : float
度到弧度的轉(zhuǎn)化常量匆背。(只讀)
這等于(PI * 2) / 360呼伸。
14、Mathf.Mathf.Rad2Deg 弧度轉(zhuǎn)度
static var Rad2Deg : float
弧度到度的轉(zhuǎn)化常量钝尸。(只讀)
這等于 360 / (PI * 2)括享。
15、Mathf.DeltaAngle 增量角
static function DeltaAngle (current :float, target : float) : float
計(jì)算給定的兩個(gè)角之間最短的差異珍促。
// Prints 90?
Debug.Log(Mathf.DeltaAngle(1080,90));
16铃辖、Mathf.Epsilon 小正數(shù)
static var Epsilon : float
一個(gè)很小的浮點(diǎn)數(shù)值。(只讀)
最小的浮點(diǎn)值猪叙,不同于0娇斩。
以下規(guī)則:
-? ? anyValue + Epsilon = anyValue
-? ? anyValue - Epsilon = anyValue
-? ? 0 + Epsilon = Epsilon
-? ? 0 - Epsilon = -Epsilon
一個(gè)在任意數(shù)和Epsilon的之間值將導(dǎo)致在任意數(shù)發(fā)生截?cái)嗾`差仁卷。
public class example : MonoBehaviour {
bool isEqual(float a, float b) {
? if(a >= b - Mathf.Epsilon && a <= b + Mathf.Epsilon)
? ? return true;
? ? else
? ? return false;
? ? ? }
? }? ? ?
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ù)字或表達(dá)式的下限值。下限值是小于等于指定數(shù)字或表達(dá)式的最接近的整數(shù)鉴分。
20哮幢、Mathf.Infinity 正無(wú)窮
static var Infinity : float
表示正無(wú)窮,也就是無(wú)窮大志珍,∞ (只讀)
21橙垢、Mathf.InverseLerp 反插值
計(jì)算兩個(gè)值之間的Lerp參數(shù)。也就是value在from和to之間的比例值伦糯。
//現(xiàn)在參數(shù)是3/5?
float parameter =Mathf.InverseLerp(walkSpeed, runSpeed, speed);
22柜某、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));
23莺琳、Mathf.LerpAngle 插值角度
static function LerpAngle (a : float, b :float, t : float) : float
和 Lerp 的原理一樣,當(dāng)他們環(huán)繞 360 度確保插值正確载慈。
a和b是代表度數(shù)。
public class example : MonoBehaviour {
public float minAngle = 0.0F;
public float maxAngle = 90.0F;
void Update() {
? ? ? float angle = Mathf.LerpAngle(minAngle, maxAngle, Time.time);
? ? ? transform.eulerAngles= new Vector3(0, angle, 0);
? ? ? ? }
? ? }
}
24珍手、Mathf.Lerp 插值
static function Lerp (from : float, to :float, t : float) : float
基于浮點(diǎn)數(shù)t返回 a 到 b 之間的插值办铡,t限制在 0~1 之間。
當(dāng) t = 0 返回 from 琳要,當(dāng) t = 1 返回 to 寡具。當(dāng) t = 0.5 返回 from 和 to 的平均值。
25稚补、Mathf.Log10 基數(shù)10的對(duì)數(shù)
static function Log10 (f : float) : float
返回f的對(duì)數(shù)童叠,基數(shù)為10。
26课幕、Mathf.Log 對(duì)數(shù)
static function Log (f : float, p : float): float
返回參數(shù) f 的對(duì)數(shù)厦坛。
// logarithm of 6 in base 2?
//以2為底6的對(duì)數(shù)?
// prints 2.584963?
print(Mathf.Log(6, 2));
27、Mathf.Max 最大值
static function Max (a : float, b : float): float
static function Max (params values :float[]) : float
返回兩個(gè)或更多值中最大的值乍惊。
28杜秸、Mathf.Min 最小值
static function Min (a : float, b : float): float
static function Min (params values :float[]) : float
返回兩個(gè)或更多值中最小的值。
29润绎、Mathf.MoveTowardsAngle 移動(dòng)角
static function MoveTowardsAngle (current :float, target : float, maxDelta : float) : float
像 MoveTowards 撬碟,但是當(dāng)它們環(huán)繞 360 度確保插值正確诞挨。
變量 current 和 target 是作為度數(shù)。為優(yōu)化原因呢蛤,maxDelta 負(fù)值的不被支持惶傻,可能引起振蕩。從target 角推開 current 其障,添加 180 度角代替银室。
30、Mathf.MoveTowards 移向
static function MoveTowards (current :float, target : float, maxDelta : float) : float
改變一個(gè)當(dāng)前值向目標(biāo)值靠近静秆。
這實(shí)際上和 Mathf.Lerp 相同粮揉,而是該函數(shù)將確保我們的速度不會(huì)超過(guò) maxDelta 。maxDelta 為負(fù)值將目標(biāo)從推離抚笔。
31扶认、Mathf.NegativeInfinity 負(fù)無(wú)窮
static var NegativeInfinity : float
表示負(fù)無(wú)窮,也就是無(wú)窮小殊橙,-∞(只讀)
32辐宾、Mathf.NextPowerOfTwo? 下個(gè)2的冪
//Prints 8 to the console?
Debug.Log(Mathf.NextPowerOfTwo(7));
33、Mathf.PingPong 乒乓
static function PingPong (t : float, length: float) : float
0 到 length 之間往返膨蛮。t 值永遠(yuǎn)不會(huì)大于 length 的值叠纹,也永遠(yuǎn)不會(huì)小于 0 。
The returned value will move back and forthbetween 0 and length.
返回值將在 0 和 length 之間來(lái)回移動(dòng)敞葛。
34誉察、Mathf.PI 圓周率
static var PI : float
PI(讀pai)的值,也就是圓周率(π)的值3.14159265358979323846...(只讀)
35惹谐、Mathf.Pow 次方
static function Pow (f : float, p : float): float
計(jì)算并返回 f 的 p 次方持偏。
36、Mathf.Repeat 重復(fù)
static function Repeat (t : float, length :float) : float
循環(huán)數(shù)值 t 氨肌, 0 到 length 之間鸿秆。 t 值永遠(yuǎn)不會(huì)大于 length 的值,也永遠(yuǎn)不會(huì)小于 0 怎囚。
這是類似于模運(yùn)算符卿叽,但可以使用浮點(diǎn)數(shù)。
public class example : MonoBehaviour {
void Update() {
? transform.position= new Vector3(Mathf.Repeat(Time.time, 3), transform.position.y,transform.position.z);
? }
}
37恳守、Mathf.RoundToInt 四舍五入到整數(shù)
static function RoundToInt (f : float) :int
返回 f 指定的值四舍五入到最近的整數(shù)考婴。
如果數(shù)字末尾 0.5 ,因此它是在兩個(gè)整數(shù)中間井誉,不管是偶數(shù)或是奇數(shù)蕉扮,將返回偶數(shù)。
38颗圣、Mathf.Round 四舍五入
static function Round (f : float) : float
返回浮點(diǎn)數(shù) f 進(jìn)行四舍五入最接近的整數(shù)喳钟。
如果數(shù)字末尾是 0.5 屁使,因此它是在兩個(gè)整數(shù)中間,不管是偶數(shù)或是奇數(shù)奔则,將返回偶數(shù)蛮寂。
39、Mathf.Sign 符號(hào)
static function Sign (f : float) : float
返回 f 的符號(hào)易茬。
當(dāng) f 為正或?yàn)?返回1酬蹋,為負(fù)返回-1。
40抽莱、Mathf.Sin 正弦
static function Sin (f : float) : float
計(jì)算并返回以弧度為單位指定的角 f 的正弦值范抓。
41、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 :當(dāng)前的位置食铐。
target :我們?cè)噲D達(dá)到的位置匕垫。
currentVelocity :當(dāng)前速度,這個(gè)值在你訪問(wèn)這個(gè)函數(shù)的時(shí)候會(huì)被隨時(shí)修改虐呻。
smoothTime( the target faster) :要到達(dá)目標(biāo)位置的近似時(shí)間象泵,實(shí)際到達(dá)目標(biāo)時(shí)要快一些。
maxSpeed :可選參數(shù)斟叼,允許你限制的最大速度萧落。
deltaTime :上次調(diào)用該函數(shù)到現(xiàn)在的時(shí)間肯腕。缺省為Time.deltaTime肴熏。
隨著時(shí)間的推移逐漸改變一個(gè)給定的角度到期望的角度甥材。 這個(gè)值通過(guò)一些彈簧減震器類似的功能被平滑。這個(gè)函數(shù)可以用來(lái)平滑任何一種值谢床,位置扒腕,顏色,標(biāo)量萤悴。最常見(jiàn)的是平滑一個(gè)跟隨攝像機(jī)。
//一個(gè)簡(jiǎn)單的平滑跟隨攝像機(jī) //跟隨目標(biāo)的朝向 public class example : MonoBehaviour {
? public Transform target;
? public float smooth = 0.3F;
? public float distance = 5.0F;
? private float yVelocity = 0.0F;
? void Update() {?
? //從目前的y角度變換到目標(biāo)y角度? ? float yAngle = Mathf.SmoothDampAngle(transform.eulerAngles.y, target.eulerAngles.y,ref yVelocity, smooth);
? //target的位置? ? Vector3 position = target.position;
? //然后皆的,新角度之后的距離偏移? position += Quaternion.Euler(0, yAngle, 0) * new Vector3(0, 0, -distance);
? //應(yīng)用位置? ? transform.position= position;
? //看向目標(biāo)? ? transform.LookAt(target);
? ? }
}? ? ? ? ? ?
42覆履、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 :當(dāng)前的位置。
target :我們?cè)噲D達(dá)到的位置费薄。
currentVelocity :當(dāng)前速度硝全,這個(gè)值在你訪問(wèn)這個(gè)函數(shù)的時(shí)候會(huì)被隨時(shí)修改。
smoothTime :要到達(dá)目標(biāo)位置的近似時(shí)間楞抡,實(shí)際到達(dá)目標(biāo)時(shí)要快一些伟众。
maxSpeed :可選參數(shù),允許你限制的最大速度召廷。
deltaTime :上次調(diào)用該函數(shù)到現(xiàn)在的時(shí)間凳厢。缺省為Time.deltaTime账胧。
描述
隨著時(shí)間的推移逐漸改變一個(gè)值到期望值。
這個(gè)值就像被一個(gè)不會(huì)崩潰的彈簧減振器一樣被平滑先紫。這個(gè)函數(shù)可以用來(lái)平滑任何類型的值治泥,位置,顏色遮精,標(biāo)量居夹。
public class example : MonoBehaviour {
? ? public Transform target;
? ? public float smoothTime = 0.3F;
? ? private float yVelocity = 0.0F;
? ? void Update() {
? ? ? ? float newPosition = Mathf.SmoothDamp(transform.position.y, target.position.y, refyVelocity, smoothTime);
? ? ? ? transform.position= new Vector3(transform.position.x, newPosition, transform.position.z);
? ? ? }
}
43、Mathf.SmoothStep 平滑插值
static function SmoothStep (from : float,to : float, t : float) : float
和lerp類似本冲,在最小和最大值之間的插值准脂,并在限制處漸入漸出。
public class example : MonoBehaviour {
? ? public float minimum = 10.0F;
? ? public float maximum = 20.0F;
? ? void Update() {
? ? ? transform.position= new Vector3(Mathf.SmoothStep(minimum, maximum, Time.time), 0, 0);
? }
}
44檬洞、Mathf.Sqrt 平方根
static function Sqrt (f : float) : float
計(jì)算并返回 f 的平方根狸膏。
45、Mathf.Tan 正切
static function Tan (f : float) : float 計(jì)算并返回以弧度為單位 f 指定角度的正切值疮胖。
Unity3D 官方數(shù)學(xué)庫(kù)學(xué)習(xí)教程:
https://docs.unity3d.com/ScriptReference/Mathf.html
C# 中常用的 Math 函數(shù)圖表
原文鏈接:https://connect.unity.com/p/unity3d-shi-yong-ji-qiao-ji-chu-shu-xue-ku-han-shu-xue-xi?app=true
歡迎大家戳上方鏈接环戈,下載Unity官方app,和博主一起探討交流澎灸,在這里可以認(rèn)識(shí)很多有趣的小伙伴院塞,還有在線技術(shù)答疑,更多實(shí)用干貨等你來(lái)發(fā)現(xiàn)性昭。