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

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);
    }

 

  • 8
    点赞
  • 31
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Mathf.CeilToInt()是一个Unity的数学函数,用于向上取整并返回一个整数值。它可以将一个浮点数值向上取整到比它大的最近的整数。例如,Mathf.CeilToInt(10.7f)将返回11,Mathf.CeilToInt(-10.2f)将返回-10。\[1\] 需要注意的是,Mathf.CeilToInt()函数是根据四舍五入规则进行取整的,而不是简单地将小数部分舍去。这意味着当小数部分大于等于0.5时,取整结果会向上取整;当小数部分小于0.5时,取整结果会向下取整。因此,Mathf.CeilToInt(10.5f)将返回10,而不是11。\[3\] 除了Mathf.CeilToInt()函数,Unity还提供了其他一些数学函数,如Mathf.Abs()用于返回一个数的绝对值,Mathf.Clamp()用于将一个值限定在指定的范围内,Mathf.ClosestPowerOfTwo()用于返回离指定数最近的2的n次幂,Mathf.Max()用于返回一组数的最大值,Mathf.Min()用于返回一组数的最小值,Mathf.Sqrt()用于返回一个数的平方根等等。这些函数可以帮助开发者在游戏开发进行数学计算和数值处理。\[2\] #### 引用[.reference_title] - *1* *3* [Mathf 常用方法](https://blog.csdn.net/yf391005/article/details/93633977)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [【Unity】Mathf的常用函数Abs,Ceil,Floor,Lerp等](https://blog.csdn.net/weixin_38211198/article/details/90489629)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值