引入:
看到一個(gè)案例注意到函數(shù)Mathf.SmoothDamp的使用,游戲中用于做相機(jī)的緩沖跟蹤和boss直升機(jī)跟蹤士兵绎橘。該函數(shù)是Unity3D中Mathf數(shù)學(xué)運(yùn)算函數(shù)中的一個(gè)神妹。一些游戲使用了smoothmove的功能酵使,其實(shí)就是類似的效果,只是發(fā)現(xiàn)這個(gè)函數(shù)很容易的已經(jīng)封裝好了钠龙,查了官網(wǎng)文檔發(fā)現(xiàn)使用起來真的非常簡單。
smoothdamp御铃,我的理解是平滑緩沖碴里,東西不是僵硬的移動(dòng)而是做減速緩沖運(yùn)動(dòng)到指定位置。我們看看代碼:
publicTransform target;//The playerpublicfloatsmoothTime=0.3f;//Smooth TimeprivateVector2 velocity;//Velocityvoid? Update ()
? ? {
? ? ? ? //Set the positiontransform.position =newVector3(Mathf.SmoothDamp(transform.position.x, target.position.x,refvelocity.x, smoothTime),Mathf.SmoothDamp( transform.position.y, target.position.y,ref velocity.y, smoothTime),transform.position.z);
? ? }
上述代碼中:
target(跟蹤的目標(biāo))上真、smoothtime(緩沖時(shí)間咬腋,時(shí)間越大緩沖速度越慢,移動(dòng)也越慢)睡互、velocity(相對緩沖減速根竿,看起來很屌很復(fù)雜,事實(shí)上我們根本不用理睬他就珠,它只是用作一個(gè)承載變量寇壳,我們只要默認(rèn)賦值其實(shí)就是0就可以了)。
在update中妻怎,本物體(其實(shí)就是camera)的移動(dòng)至做x軸和y軸移動(dòng)壳炎,因?yàn)槭菣M版游戲嘛~,大家其實(shí)可以吧velocity直接指定成一個(gè)float值逼侦,直接使用就是了匿辩。
?干貨:
Mathf.Abs絕對值
計(jì)算并返回指定參數(shù) f 絕對值疏日。
Mathf.Acos反余弦
static function Acos (f : float) : float
以弧度為單位計(jì)算并返回參數(shù) f 中指定的數(shù)字的反余弦值。
?Mathf.Approximately近似
static function Approximately (a : float, b: float) : bool
比較兩個(gè)浮點(diǎn)數(shù)值撒汉,看它們是否非常接近, 由于浮點(diǎn)數(shù)值不精確沟优,不建議使用等于來比較它們。例如睬辐,1.0==10.0/10.0也許不會(huì)返回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
以弧度為單位計(jì)算并返回參數(shù) f 中指定的數(shù)字的反正弦值。
Mathf.Atan2反正切
static function Atan2 (y : float, x :float) : float
以弧度為單位計(jì)算并返回 y/x 的反正切值溯饵。返回值表示相對直角三角形對角的角侵俗,其中 x 是臨邊邊長,而 y 是對邊邊長丰刊。
返回值是在x軸和一個(gè)二維向量開始于0個(gè)結(jié)束在(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);
??????????? }
}
Mathf.Atan反正切
static function?Atan?(f?: float) :float
計(jì)算并返回參數(shù) f 中指定的數(shù)字的反正切值。返回值介于負(fù)二分之 pi 與正二分之 pi 之間啄巧。
Mathf.CeilToInt最小整數(shù)
static function CeilToInt (f : float) : int
返回最小的整數(shù)大于或等于f寻歧。
?Mathf.Ceil上限值
static function Ceil (f : float) : float
返回 f 指定數(shù)字或表達(dá)式的上限值。數(shù)字的上限值是大于等于該數(shù)字的最接近的整數(shù)秩仆。
?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的次方數(shù)多望。
Mathf.Cos余弦
static function Cos (f : float) : float
返回由參數(shù) f 指定的角的余弦值(介于 -1.0 與 1.0 之間的值)。
Mathf.Deg2Rad度轉(zhuǎn)弧度
static var Deg2Rad : float
度到弧度的轉(zhuǎn)化常量氢烘。(只讀)
這等于(PI * 2) / 360怀偷。
?Mathf.Mathf.Rad2Deg 弧度轉(zhuǎn)度
static var Rad2Deg : float
弧度到度的轉(zhuǎn)化常量。(只讀)
這等于 360 / (PI * 2)播玖。
Mathf.DeltaAngle增量角
static function DeltaAngle (current :float, target : float) : float
計(jì)算給定的兩個(gè)角之間最短的差異椎工。
// Prints 90
Debug.Log(Mathf.DeltaAngle(1080,90));
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
{
??????????? boolisEqual(float a, float b)
{
??????????????????????? if(a >= b - Mathf.Epsilon && a <= b + Mathf.Epsilon)
??????????????????????????????????? returntrue;
??????????????????????? else
?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?returnfalse;
??????????? }
}
Mathf.Exp指數(shù)
static function Exp (power : float) : float
返回 e 的 power 次方的值。
Mathf.FloorToInt最大整數(shù)
static function FloorToInt (f : float) :int
返回最大的整數(shù)颅痊,小于或等于f殖熟。
?Mathf.Floor下限值
static function Floor (f : float) : float
返回參數(shù) f 中指定的數(shù)字或表達(dá)式的下限值。下限值是小于等于指定數(shù)字或表達(dá)式的最接近的整數(shù)斑响。
?Mathf.Infinity正無窮
static var Infinity : float
表示正無窮菱属,也就是無窮大,∞ (只讀)
?Mathf.InverseLerp反插值
計(jì)算兩個(gè)值之間的Lerp參數(shù)舰罚。也就是value在from和to之間的比例值纽门。
//現(xiàn)在參數(shù)是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的原理一樣赏陵,當(dāng)他們環(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);
??????????? }
}
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的平均值。
?Mathf.Log10基數(shù)10的對數(shù)
static function Log10 (f : float) : float
返回f的對數(shù)伙菊,基數(shù)為10。
?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.584963
print(Mathf.Log(6, 2));
?Mathf.Max最大值
static function Max (a : float, b : float): float
static function Max (params values :float[]) : float
返回兩個(gè)或更多值中最大的值镜硕。
?Mathf.Min最小值
static function Min (a : float, b : float): float
static function Min (params values :float[]) : float
返回兩個(gè)或更多值中最小的值。
?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度角代替癌淮。
?Mathf.MoveTowards移向
static function MoveTowards (current :float, target : float, maxDelta : float) : float
改變一個(gè)當(dāng)前值向目標(biāo)值靠近躺坟。
這實(shí)際上和 Mathf.Lerp相同,而是該函數(shù)將確保我們的速度不會(huì)超過maxDelta乳蓄。maxDelta為負(fù)值將目標(biāo)從推離咪橙。
?Mathf.NegativeInfinity負(fù)無窮
static var NegativeInfinity : float
表示負(fù)無窮,也就是無窮小,-∞(只讀)
?Mathf.NextPowerOfTwo 下個(gè)2的冪
?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之間來回移動(dòng)菠剩。
Mathf.PI圓周率
static var PI : float
PI(讀pai)的值易猫,也就是圓周率(π)的值3.14159265358979323846...(只讀)?
?Mathf.Pow次方
static function Pow (f : float, p : float): float
計(jì)算并返回 f 的 p 次方。
?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
{
??????????? voidUpdate()
{
??????????????????????? transform.position= new Vector3(Mathf.Repeat(Time.time, 3), transform.position.y,transform.position.z);
??????????? }
}
?Mathf.RoundToInt四舍五入到整數(shù)
static function RoundToInt (f : float) :int
返回 f 指定的值四舍五入到最近的整數(shù)涧郊。
如果數(shù)字末尾是.5贯被,因此它是在兩個(gè)整數(shù)中間,不管是偶數(shù)或是奇數(shù)妆艘,將返回偶數(shù)彤灶。
?Mathf.Round四舍五入
static function Round (f : float) : float
返回浮點(diǎn)數(shù) f 進(jìn)行四舍五入最接近的整數(shù)。
如果數(shù)字末尾是.5批旺,因此它是在兩個(gè)整數(shù)中間幌陕,不管是偶數(shù)或是奇數(shù),將返回偶數(shù)汽煮。
?Mathf.Sign符號
static function Sign (f : float) : float
返回 f 的符號搏熄。
當(dāng) f 為正或?yàn)?返回1,為負(fù)返回-1暇赤。
?Mathf.Sin正弦
static function Sin (f : float) : float
計(jì)算并返回以弧度為單位指定的角 f 的正弦值心例。
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
我們試圖達(dá)到的位置鞋囊。
currentVelocity
當(dāng)前速度止后,這個(gè)值在你訪問這個(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è)值通過一些彈簧減震器類似的功能被平滑现恼。這個(gè)函數(shù)可以用來平滑任何一種值肃续,位置,顏色叉袍,標(biāo)量始锚。最常見的是平滑一個(gè)跟隨攝像機(jī)。
//一個(gè)簡單的平滑跟隨攝像機(jī)
//跟隨目標(biāo)的朝向
public class example : MonoBehaviour
{
??????????? publicTransform target;
??????????? publicfloat smooth = 0.3F;
??????????? publicfloat distance = 5.0F;
??????????? privatefloat yVelocity = 0.0F;
??????????? voidUpdate()
{
//從目前的y角度變換到目標(biāo)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);
//應(yīng)用位置
??????????????????????? transform.position= position;
//看向目標(biāo)
??????????????????????? 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
參數(shù)
current
當(dāng)前的位置瞧捌。
target
我們試圖達(dá)到的位置。
currentVelocity
當(dāng)前速度润文,這個(gè)值在你訪問這個(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ù)可以用來平滑任何類型的值笑陈,位置,顏色葵袭,標(biāo)量涵妥。
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);
??????????? }
}
?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);
??????????? }
}
Mathf.Sqrt平方根
static function Sqrt (f : float) : float
計(jì)算并返回 f 的平方根。