【Unity】入门学习笔记180518——API(6)——Mathf类

Mathf类

数学类,属于结构体类型,只有静态属性和静态方法


A、Mathf类静态属性

1、Deg2Rad

public const float Deg2Rad=0.0174533f;

从角度到弧度常量,其值为(2*Mathf.PI)/360=0.01745329


Rad2Deg属性相反,从弧度到角度的转换常量


2、Infinity

public const float Infinity=1.0f / 0.0f

表示数学计算中的正无穷大,只读

Mathf.Infinity/x=Mathf.Infinity

Mathf.Infinity/Mathf.Infinity=NaN


B、Mathf类静态方法

1、Clamp

public static float Clamp(float value, float min, float max);

public static int Clamp(int value, int min, int max);

用来返回有范围限制的value值,即落在区间内返回原值,区间外返回最大或最小值

相似的还有Clamp01


2、ClosestPowerOfTwo方法

public static int ClosestPowerOfTwo(int value);

返回最接近参数值value的2的某次幂值,向上取整

即2的次幂有2,4,8,16,32,64,128...

value参数为12,则向上取16返回;value参数为11,则返回8


3、DeltaAngle

public static float DeltaAngle(float current, float target);

current为当前角度,target为目标角度

此方法用于返回current到target最小增量角度值,顺时针为正,逆时针为负,返回(0<=e<=180)


4、InverseLerp

public static float InverseLerp(float from, float to, float value);

from为起始值,to为终点值,value为参考值

此方法用来返回value值在从参数from到to中的比例值

即f’=(v-from) / (to-from)

若f'∈[0.0,1.0],f=f';f'>1,则f=1;f'<0,则f=0


5、Lerp

public static float lerp(float from, float to, float t)

from为线性插值的起始值,to为线性插值的结束值,t为插值系数

返回一个从from到to范围的线性插值,计算方法为(to-from)*t'+from

t的有效范围为[0,1],当t<0时t'=0,t>1时t'=1


6、LerpAngle

public static float LerpAngle(float a, float b, float t);

a为起始角度,b为结束角度,t为插值系数

角度之间的插值,a'=360*K+a,类似于Lerp


7、MoveTowards

public static float MoveToWards(float current, float target, float maxDelta);

current为当前值,target为目标值,maxDelta为最大约束值

此方法时返回一个从current到target之间的插值

返回值计算方法如下:

①current<target:current+maxDelta与target返回较小值

②current>target:current-maxDelta与target返回较大值

简单的说就是返回中间值


8、MoveTowardsAngle

public static float MoveTowardAngle(float current, float target, float maxDelta);

角度的选择性插值,类似于MoveTowards


9、PingPong

public static float PingPong(float t, float length);

模拟乒乓球的往复运动

我的理解是f,length之间来回取值


10、Repeat

public static float Repeat(float t, float length);

类似于浮点数的取模运算


11、Round

public static float Round(float f);

返回离f最近的整型浮点值


12、SmoothDamp

public static float SmoothDamp(float current, float target, ref float currentVelocity, float smoothTime);

~(float current, float target, ref float currentVelocity, float smoothTime, float maxSpeed);

~(float current, float target, ref float currentVelocity, float smoothTime, float maxSpeed, float deltaTime);

current:起始值

target :目标值

currentVelocity:ref类型,当前帧速度

smoothTime:预计平滑时间

maxSpeed:当前帧最大速度,默认值为Mathf.Infinity

deltaTime:平滑时间,值越大返回值也相对越大,一般用Time.deltaTime计算

模拟平滑阻尼运动,并返回模拟插值


13、SmoothDampAngle

public static float SmoothDampAngle(float current, float target, ref float currentVelocity, float smoothTime);

~

~

模拟角度的平滑阻尼旋转,并返回模拟插值,与SmoothDamp方法功能类似


14、SmoothStep

public static float SmoothStep(float from, float to, float t);

返回一个从from到to的平滑插值

t的有效范围为[0.0f, 1.0f]

猜你喜欢

转载自blog.csdn.net/dylan_day/article/details/80362986
今日推荐