Mathf

public class L1 : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        //Mathf和Math
        //Math是C#中封装好的用于数学计算的工具类,在System命名空间中
        //Mathf是Unity中封装好的用于数学计算的工具结构体,在UnityEngine中
        //都用于数学计算
        //Mathf中的常用方法

        //Mathf中的常用方法 - 一般之计算一次
        //PI
        print(Mathf.PI);
        //取绝对值 - Mathf.Abs

        //向上取整
        print(Mathf.CeilToInt(1.3f));

        //向下取整
        //C#本身的强转
        float f = 1.3f;
        int i = (int)f;
        //Unity向下取整
        print(Mathf.FloorToInt(9.6f));

        //夹紧函数 -Clamp
        //比小的还小就取最小,比大的还大就取最大,两者之间就取本身
        print(Mathf.Clamp(10,11,20));

        //获取最大值- Max
        print(Mathf.Max(1, 2, 3, 4));

        //获取最小值 - Min,同Max

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

        //四舍五入 - RoundToint
        print(Mathf.RoundToInt(1.3f));

        //一个数的平方根 - Sqrt
        print(Mathf.Sqrt(4));

        //判断一个数是否是2的n次方 - IsPowerOfTwo
        print(Mathf.IsPowerOfTwo(4));

        //判断正负数 - Sign
        //正数返回1 , 负数返回-1
        print(Mathf.Sign(4));

    }
    float start=0;
    float end=0;
    float time = 0;
    // Update is called once per frame
    void Update()
    {
        //Mathf中常用方法 一般不停计算
        //插值运算 - Lerp

        //Lerp函数公式
        //Mathf.Lerp(start,end,t)

        //t为插值系数,取值范围为0~1;
        //result = start +(end - start)*t

        //插值运算用法一
        //每帧改变start的值 - 变化速度先快后慢,位置无限接近但是不会得到end
        start = Mathf.Lerp(start, 10, Time.deltaTime);

        //插值运算用法二
        //每帧改变t的位置 - 变化速度匀速,位置每帧接近,当t>=1时得到结果
        time += Time.deltaTime;
        end = Mathf.Lerp (start,10,time);
    }
}
posted @ 2025-03-19 19:52  cannedmint  阅读(34)  评论(0)    收藏  举报