math是C#的,mathf是unity这边又加了一些游戏需要的api的

math是类,mathf是结构体

Mathf ApI

		 //1.π - PI
        print(Mathf.PI);

        //2.取绝对值 - Abs
        print(Mathf.Abs(-10));
        print(Mathf.Abs(-20));
        print(Mathf.Abs(1));
        //3.向上取整 - CeilToInt
        float f = 1.3f;
        int i = (int)f;
        print(i);
        print(Mathf.CeilToInt(f));
        print(Mathf.CeilToInt(1.00001f));

        //4.向下取整 - FloorToInt
        print(Mathf.FloorToInt(9.6f));

        //5.钳制函数 - Clamp 第一个在后面界限内,小于最小取最小,大于最大取最大,在里面取自己
        print(Mathf.Clamp(10, 11, 20));  
        print(Mathf.Clamp(21, 11, 20));
        print(Mathf.Clamp(15, 11, 20));
		10
		21
		15
        //6.获取最大值 - Max
        print(Mathf.Max(1, 2, 3, 4, 5, 6, 7, 8));
        print(Mathf.Max(1, 2));

        //7.获取最小值 - Min
        print(Mathf.Min(1, 2, 3, 4, 545, 6, 1123, 123));
        print(Mathf.Min(1.1f, 0.4f));

        //8.一个数的n次幂 - Pow
        print("一个数的n次方" + Mathf.Pow(4, 2)); // 4的二次方
        print("一个数的n次方" + Mathf.Pow(2, 3));

        //9.四舍五入 - RoundToInt
        print("四舍五入" + Mathf.RoundToInt(1.3f));
        print("四舍五入" + Mathf.RoundToInt(1.5f));

        //10.返回一个数的平方根 - Sqrt
        print("返回一个数的平方根" + Mathf.Sqrt(4));
        print("返回一个数的平方根" + Mathf.Sqrt(16));
        print("返回一个数的平方根" + Mathf.Sqrt(64));

        //11.判断一个数是否是2的n次方 - IsPowerOfTwo return bool
        print("判断一个数是否是2的n次方" + Mathf.IsPowerOfTwo(4));
        print("判断一个数是否是2的n次方" + Mathf.IsPowerOfTwo(8));
        print("判断一个数是否是2的n次方" + Mathf.IsPowerOfTwo(3));
        print("判断一个数是否是2的n次方" + Mathf.IsPowerOfTwo(1));

        //12.判断正负数 - Sign
        print("判断正负数" + Mathf.Sign(0));   //正数
        print("判断正负数" + Mathf.Sign(10));
        print("判断正负数" + Mathf.Sign(-10));
        print("判断正负数" + Mathf.Sign(3));
        print("判断正负数" + Mathf.Sign(-2));

插值函数 Mathf中的常用方法——一般不停计算


    //插值运算 - Lerp
        //Lerp函数公式
        //result = Mathf.Lerp(start, end, t);

        //t为插值系数,取值范围为 0~1,内部实现公式
        //result = start + (end - start)*t


 //开始值
    float start = 0;
    float result = 0;
    float time = 0;
    // Update is called once per frame
    void Update()
    {
    
        //插值运算用法一
        //每帧改变start的值——变化速度先快后慢,位置无限接近,但是不会得到end位置
        //start = start + (end - start)*t //start每一帧都在变大,导致,(end - start)*t 越来越小,越来越接近目标值而不等于
        start = Mathf.Lerp(start, 10, Time.deltaTime);
	
        //插值运算用法二
        //每帧改变t的值——变化速度匀速,位置每帧接近,当t>=1时,得到结果,这个是可以得到end pos
         //result = start + (end - start)*t  // (end - start)每一帧都不变化,t越来越大,导致,(end - start)*t 越来越大,result在线性变大,可以看成是匀速
        time += Time.deltaTime;
        result = Mathf.Lerp(start, 10, time);
    }

在这里插入图片描述
在这里插入图片描述

Logo

北京人形旗下天工造物具身智能开源社区,聚焦具身天工与慧思开物两大平台

更多推荐