UnityAPI的学习——Mathf类

Mathf类是Unity中的数学类,属于结构体类型,只有静态属性和静态方法,即不可实例化。

Mathf类静态属性

在Mathf类中,涉及的静态属性有Deg2Rad、Rad2Deg和Infinity,其中属性Deg2Rad和Rad2Deg功能相似。

1、Deg2Rad属性:从角度到弧度常量
基本语法

public const float Deg2Rad=0.1f;

功能说明:此属性用来表示数学计算中从角度到弧度转变的常量值,其值为(2*Mathf.PI)/360=0.01745329,此属性只读

Rad2Deg属性与此属性功能相反,是从弧度到角度的转换常量值,其值为57.2958f

2、Infinity属性:正无穷大
基本语法

public const float Infinity=1.0f/0.0f

功能说明 :此属性用来表示在数学计算中的正无穷大,只读。

Mathf类静态方法

在Mathf类中,涉及的静态方法有Clamp方法、ClosestPowerOfTwo方法、DeltaAngle方法、InverseLerp方法、Lerp方法、LerpAngle方法、MoveToWard方法、MoveTowardsAngle方法、PingPong方法、Repeat方法、Round方法、SmoothDamp方法、SmoothDampAngle方法和SmoothStep方法

1、Clamp方法:返回有限范围值
基本语法

public static float Clamp(float value ,float min ,float max);
其中参数min为返回值的最小值,参数max为返回值的最大值。参数和返回值类型为浮点型

public static int Clamp(int value ,int min ,int max);
其中参数min为返回值的最小值,参数max为返回值的最大值,参数和返回值类型为整型

功能说明: 此方法用来返回有范围限制的value值,当value在[min , max]时返回value值;当value<min时返回min值;当value>max时返回值。

Clamp01的取值范围为[0,1],只有一个参数

2、ClosestPowerOfTwo方法:返回2的某次幂
基本语法

public static int ClosestPowerOfTwo(int value);

功能说明 此方法用于返回最接近参数值value的2的某次幂的值。当value属于中间值时取较大值
例如:
f=Mathf.ClosestPowerOfTwo(11) ,则f=8;
f=Mathf.ClosestPowerOfTwo(12),则f=16
当value值小于0时,返回值为0

3、DeltaAngle方法:最小赠量角度
基本语法

public static float DeltaAngle(float current ,float target);
其中参数current为当前角度,参数target为目标角度

功能说明 此方法用于返回从参数值current到target的最小增量角度值

4、InverseLerp方法:计算比例值
基本语法

public static float InverseLerp(float from,float to ,float value)
其中参数from 为起始值,参数to为终点值,参数value为参考值

功能说明:此方法用来返回value值在从参数from到to中的比例值。

5、Lerp方法:线性插值
基本语法

public static float Lerp(float from ,float to ,float t);
其中参数from为线性插值的起始值,参数to为线性插值的结束值,参数t为插值系数

功能说明
此方法的功能是用来返回一个从from到to范围的线性插值。返回值的计算方法为(to-from)*t+from,其中:
1、参数t的有效取值范围为[0,1],当t<0时有效值t=0,当t>1时有效值t=1;
2、参数from和to为任意的float数值,from和to之间没有任何约束关系,from的值可以大于to也可以小于to。

6、MoveTowards方法:选择性插值
基本语法

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

功能说明 此方法的功能是返回一个从current到target之间的插值,返回值受maxDelta值的约束。
此方法的功能是返回一个从current到target之间的插值,返回值受maxDelta值的约束。

返回值计算方式
1、若a<b:当a+d<b时 ,c=a+d:当a+d>b时 ,c=b
2、若a>b:当a-d>b时 , c=a-d: 当a-d<b时 ,c=b

7、MoveTowardsAngle方法:角度的选择性插值
基本语法

 public static float MoveTowardsAngle(float current ,float target ,float maxDelta)

功能说明 此方法的作用是返回一个从当角度current向目标角度target旋转的插值,每帧旋转角度不超过maxDelta度。

8、PingPong方法:往复运动
基本语法

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

功能说明
此方法用于模拟乒乓球的往复运动。设f=Mathf.PingPong( t ,1),其中f、t和1均为float类型数值。

9、Repeat方法:取模运算
基本语法

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

功能说明
此方法的作用类似于浮点数的取模运算

10、Round方法:浮点数的整型值
基本语法

public static float Round(float t)

功能说明
此方法作用是返回离f最近的整型浮点值

11、SmoothDamp方法:模拟阻尼运动

1public static float SmoothDamp(float current ,float target ,ref float current Velocity , float smoothTime);
2public static float SmoothDamp(float current ,float target ,ref float current Velocity , float smoothTime )
3public static float SmoothDamp(float current ,float target ,ref float current Velocity , float smoothTime ,float maxSpeed ,float deltaTime);

current:起始值
target:目标值
currentVelocity:当前帧速度
ref:参数smoothTime为预计平滑时间
maxSpeed:当前帧最大速度值,默认值为Mathf.Infinity
deltaTime:为平滑时间,值越大返回值也相对越大,一般用Time.deltaTime计算

功能说明:
此方法的功能是模拟平滑阻尼运动,并返回模拟插值。smoothTime:float ,预计平滑时间,物体越靠近目标,加速度的绝对值越小。实际到达目标的时间往往要比预计时间大很多,建议smoothTime的取值范围为(0.0f ,1.0f),若想控制物体到达目标的时间可以通过控制maxSpeed来达到目标。maxSpeed:float = Mathf.Infinity ,每帧返回值的最大值,默认值为Mathf.Infinity.

12、SmoothDampAngle方法:阻尼旋转
基本语法

1public static float SmoothDampAngle(float current ,float target ,ref float current Velocity , float smoothTime);
2public static float SmoothDampAngle(float current ,float target ,ref float current Velocity , float smoothTime )
3public static float SmoothDampAngle(float current ,float target ,ref float current Velocity , float smoothTime ,float maxSpeed ,float deltaTime);

功能说明:
此方法的功能是模拟角度的平滑阻尼旋转,并返回模拟插值

13、SmoothStep方法:平滑插值
基本语法

public static float SmoothStep(float from , float to ,float t);
其中参数from为起始值,参数to为结束值,参数t为插值系数

功能说明
此方法的功能是返回一个从from 到 to 的平滑插值
1、参数from和to是两个任意的float类型数值,它们之间没有任何大小约束关系,from可以大于to也可以小于to
2、插值系数t的有效范围为[0.0f ,1.0f],当t<0时其有效值 t 为0.0f ,当t>1时其有效值t为1.0f

  • 21
    点赞
  • 29
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值