unity 中 Mathf 的常用方法

本文介绍了 Unity 游戏引擎中 Mathf 常用的方法,包括取绝对值、各种取整函数、次方运算、平方根计算、角度计算、最近的2的幂次方、限制值范围以及线性插值等。特别提到了 Mathf.Round 在处理浮点数时的四舍五入规则。

Mathf 常用方法


Mathf.Abs()------取绝对值
Mathf.Ceil()------向上取整,返回值为 Float 类型
Mathf.CeilToInt()------向上取整,返回值为 Int 类型
void Start()
{
    //向上取整,返回比当前值大的一个整数
    Debug.Log(Mathf.Ceil(10.0f));//10
    Debug.Log(Mathf.Ceil(10.2f));//11
    Debug.Log(Mathf.CeilToInt(10.7f));//11

    Debug.Log(Mathf.Ceil(-10.0f));//-10
    Debug.Log(Mathf.Ceil(-10.2f));//-10
    Debug.Log(Mathf.CeilToInt(-10.7f));//-10
}
Mathf.Floor()--------向下取整,返回值为 Float 类型
Mathf.FloorToInt()--------向下取整,返回值为 Int 类型
void Start()
{
    //向下取整,返回比当前值小的一个整数
    Debug.Log(Mathf.Floor(10.0f));//10
    Debug.Log(Mathf.Floor(10.2f));//10
    Debug.Log(Mathf.FloorToInt(10.7f));//10

    Debug.Log(Mathf.Floor(-10.0f));//-10
    Debug.Log(Mathf.Floor(-10.2f));//-11
    Debug.Log(Mathf.FloorToInt(-10.7f));//-11
}
Mathf.Round()--------四舍五入,返回值为 Float 类型
Mathf.RoundToInt()--------四舍五入,返回值为 Int 类型

注意:如果数字小数部分以 5 结尾,那么它介于两个整数之间,若其中一个是偶数,另一个是奇数,则返回偶数。其他情况下就是正常的四舍五入

程序 != 数学 这里 Mathf.Round(10.5f)返回值为 10,而不是 11

void Start()
{
    Debug.Log(Mathf.Round(10.0f));//10
    Debug.Log(Mathf.Round(10.2f));//10
    Debug.Log(Mathf.Round(10.5f));//10
    Debug.Log(Mathf.Round(10.7f));//11

    Debug.Log(Mathf.Round(-10.0f));//-10
    Debug.Log(Mathf.Round(-10.2f));//-10
    Debug.Log(Mathf.Round(-10.5f));//-10
    Debug.Log(Mathf.Round(-10.7f));//-11
}
Mathf.Pow(float x,float y)--------返回 x 的 y 次方
Debug.Log(Mathf.Pow(2,3));//2 的 3 次方,返回 8
Mathf.Sqrt(float x)-----------返回 x 的平方根
Debug.Log(Mathf.Sqrt(64));//64 开根,结果为 8
Mathf.DeltaAngle(float current,float target)---------返回两个角夹角的最小值
Debug.Log(Mathf.DeltaAngle(30,60));//结果为 30
Debug.Log(Mathf.DeltaAngle(30, 360));//结果为-30
Mathf.ClosestPowerOfTwo(float x)------------返回 2 的 x 次方
Debug.Log(Mathf.ClosestPowerOfTwo(3));//2 的 3 次方
Mathf.Clamp(float current,float min,float max)

限制 current 的值在 min,max 之间,如果 current 大于 max,则返回 max,如果 current 小于 min,则返回 min,否者返回 current;

例如:

人物血量的计算

//血量只可能在[0,100]范围内,不可能小于 0,也不可能大于 100
int currentHealth = 100;

//控制血量在正常范围
currentHealth = Mathf.Clamp(currentHealth, 0, 100);

物体在某个方向上的移动

_rig.transform.position = new Vector3(transform.position.x, transform.position.y,
Mathf.Clamp(_rig.transform.position.z, -20.0f, 28.0f));

这里限制了刚体的 Z 轴方向的移动,刚体在-20.0 到 28.0 范围内移动。

Mathf.Lerp(float a, float b, float t)

线性插值,t 为一个比例,范围[0,1]
t=0 的时候返回 a,t=1 的时候返回 b,t=0.5 时返回(a+b)/2 先快后慢

public class Test : MonoBehaviour
{
    public float currStrength;
    public float maxStrength;
    public float recoveryRate;

    void Update()
    {
        currStrength = Mathf.Lerp(currStrength, maxStrength, recoveryRate * Time.deltaTime);
        transform.position = new Vector3(currStrength, 0, 0);
    }
}
Mathf.MoveTowards(float current, float target, float maxDelta)------与 Mathf.Lerp 本质上是一样的 匀速

如果 maxDelta 为负数,current 的值将远离 target

public class Test : MonoBehaviour
{
    public float currStrength;
    public float maxStrength;
    public float recoveryRate;

    void Update()
    {
        currStrength = Mathf.MoveTowards(currStrength, maxStrength, recoveryRate * Time.deltaTime);
        transform.position = new Vector3(currStrength, 0, 0);
    }
}
Mathf.PingPong(float t,float length)----返回值在 0 和 length 之间
void Update()
{
 float posX = Mathf.PingPong(Time.time, 3);//posX 永远在 0 和 3 之间
transform.position = new Vector3(posX, transform.position.y, transform.position.z);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值