Unity 基础函数

Mathf:

        //1.π-PI
        print(Mathf.PI);
        //2.取绝对值-Abs
        print(Mathf.Abs(-10));
        print(Mathf.Abs(-20));
        print(Mathf.Abs(1));
        //3.向上取整-Ce il To In t
        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));//10
        print(Mathf.Clamp(22, 11, 20));//20
        print(Mathf.Clamp(15, 11, 20));//15
        //6.获取最大值-Max
        print(Mathf.Max(10, 11, 20));//取最大数
        //7.获取最小值-Min
        print(Mathf.Pow(10, 2));//10的2次方
        //8.一个数的n次幕-Pow
        print(Mathf.FloorToInt(9.6f));
        //9.四舍五入-RoundToInt
        print(Mathf.RoundToInt(9.6f));
        //10.返回一个数的平方根-Sqrt
        print(Mathf.Sqrt(4f));//2
        //11.判断一个数是否是2的n次方-IsPowerofTwo
        print(Mathf.IsPowerOfTwo(9));//false
        //12.判断正负数-Sign
        print(Mathf.Sign(9.6f));//正数返回1

三角函数:

         // 弧度转角度
        float rad = 1;
        float anger = rad * Mathf.Rad2Deg;
        print(anger);
        // 角度转弧度
        anger = 1;
        rad = anger * Mathf.Deg2Rad;
        print(rad);

        //注意:Mathf中的三角函数相关函数,传入的参数需要时弧度值
        print(Mathf.Sin(30 * Mathf.Deg2Rad));
        print(Mathf.Cos(60 * Mathf.Deg2Rad));

        //注意:反三角函数得到的结果是正弦或者余弦值对应的弧度
        rad = Mathf.Asin(0.5f);
        print(rad * Mathf.Rad2Deg);
        rad = Mathf.Acos(0.5f);
        print(rad * Mathf.Rad2Deg);

坐标系转换:

        //世界坐标系
        //目前学习的和世界坐标系相关的
        //this.transform.position;
        //this.transform.rotation;
        //this.transform.eulerAngles;
        //this.transform.lossyScale;
        //修改他们会是相对世界坐标系的变化

        //相对坐标系
        //相对父对象的物体坐标系的位置本地坐标相对坐标
        //this.transform.localPosition;
        //this.transform.localEulerAngles;
        //this.transform.localRotation;
        //this.transform.localscale;
        //修改他们会是相对父对象物体坐标系的变化

        //三屏幕坐标系
        //Input.mouse Position
        //screen.width;
        //screen.height;

        //坐标转换相关
        //世界转本地
        //this.transform.InverseTransformDirection
        //this.transform.InverseTransformPoint
        //this.transform.InverseTransformVector
        //本地转世界
        //this.transform.TransformDirection
        //this.transform.TransformPoint
        //this.transform.TransformVector
        //世界转屏幕
        //Camera.main.WorldToscreenPoint
        //屏幕转世界
        //Camera.main.ScreenToworldPoint;

        //世界转视口
        //Camera.main.WorldToViewportPoint
        //视口转世界
        //Camera.main.ViewportToworldPoint
        //视口转屏幕
        //Camera.main.ViewportToScreenPoint
        //屏幕转视口
        //Camera.main.ScreenToViewportPoint;

向量:
//知识点一向量
        //三维向量-Vector3
        //Vector3有两种几何意义
        //1.位置一代表一个点
        print(this.transform.position);
        //2.方向一代表一个方向
        print(this.transform.forward);
        print(this.transform.up);

        //知识点二两点决定一向量
        //A和B此时几何意义是两个点
        Vector3 A = new Vector3(1, 2, 3);
        Vector3 B = new Vector3(5, 1, 5);
        //求向量
        //此时AB和BA他们的几何意义是两个向量
        Vector3 AB = B - A;
        Vector3 BA = A - B;

        //知识点三零向量和负向量
        print(Vector3.zero);
        print(Vector3.forward);
        print(Vector3.forward);

        //知识点四向量的模长
        //Vector3中提供了获取向量模长的成员属性
        //magnitude
        print(AB.magnitude);
        Vector3 c = new Vector3(5, 6, 7);
        print(c.magnitude);

        //知识点五单位向量
        print(AB.normalized);

        //向量加法
        //this.transform.position += new Vector3(1, 2, 3);
        this.transform.Translate(Vector3.forward * 5);      
        //向量减法
        //this.transform.position -= new Vector3(1, 2, 3);
        this.transform.Translate(-Vector3.forward * 5);
        //向量乘除标量
        this.transform.localScale *= 2;
        this.transform.localScale /= 2;

        //补充知识调试画线
        //画线段
        //前两个参数分别是起点终点
        Debug.DrawLine(this.transform.position, this.transform.position + this.transform.forward, Color.red);
        //画射线
        // 前两个参数分别是起点方向
        Debug.DrawRay(this.transform.position,transform.right, Color.green);

        //通过点乘判断对象方位
        //Vector3提供了计算点乘的方法
        Debug.DrawRay(this.transform.position, this.transform.forward, Color.red);
        //得到两个向量的点乘结果
        //向量a点乘AB的结果
        float dotResult = Vector3.Dot(this.transform.forward, target.position - this.transform.position);
        if (dotResult >= 0)
            print("它在我前方");
        else
            print("它在我后方");
        

 

//通过点乘推导公式算出夹角
        //步骤
        //1.用单位向量算出点乘结果
        //dot Result = Vector3.Dot(this.transform.forward, (target.position - this.transform.position).normalized);
        //2.用反三角函数得出角度
        print("角度" + Mathf.Acos(dotResult) * Mathf.Rad2Deg);
        //Vector3中提供了得到两个向量之间夹角的方法
        print("角度2" + Vector3.Angle(this.transform.forward, target.position - this.transform.position));

//叉乘计算
        print(Vector3.Cross(AA.position, BB.position));
        //叉乘几何意义
        //假设向量A和B都在X Z平面上
        //向量A叉乘向量B
        //y大于0证明B在A右侧
        //y小于0证明B在A左侧
        Vector3 vec = Vector3.Cross(BB.position, AA.position);
        if (vec.y > 0)
            print("AA在BB的右侧");
        else
            print("AA在BB的左侧");

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Unity数学函数库是一组用于处理数学运算的函数。其中包含了一些常用的数学函数,如绝对值、三角函数、指数函数、对数函数取整函数等。其中,Mathf类是Unity中提供的一个数学类,包含了许多常用的数学函数。比如,Mathf.Abs函数用于计算一个数的绝对值,Mathf.Sin函数用于计算正弦值,Mathf.Exp函数用于计算指数值,Mathf.Log函数用于计算对数值等等。 在Unity的数学函数库中,还有一些特殊的取整函数。除了常见的四舍五入函数Mathf.Round外,还有向上取整函数Mathf.Ceil和向下取整函数Mathf.Floor。Mathf.Ceil函数返回一个比给定参数大的最小整数,而Mathf.Floor函数返回一个比给定参数小的最大整数。 总而言之,Unity数学函数库提供了丰富的数学函数,方便开发者进行各种数学计算。无论是简单的四则运算,还是更复杂的三角函数、指数函数等,都能在该库中找到对应的函数来实现。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [Unity3D 实用技巧 - 基础数学库函数学习](https://blog.csdn.net/CSDN_ONION/article/details/103924119)[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^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* *3* [unity 中的 数学函数库(完善中)](https://blog.csdn.net/yy763496668/article/details/50833311)[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^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值