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

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章