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

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值