当前位置: 首页 > news >正文

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中提供了获取向量模长的成员属性//magnitudeprint(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("它在我前方");elseprint("它在我后方");

 

//通过点乘推导公式算出夹角//步骤//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的右侧");elseprint("AA在BB的左侧");

http://www.lryc.cn/news/116879.html

相关文章:

  • 【学习】若依源码(前后端分离版)之 “ 上传图片功能实现”
  • vue3 excel 导出功能
  • python 相关框架事务开启方式
  • vue使用ElementUI
  • Python做一个绘图系统3:从文本文件导入数据并绘图
  • flutter开发实战-获取Widget的大小及位置
  • 软件测试工程师面试如何描述自动化测试是怎么实现的?
  • Qt5兼容使用之前Qt4接口 intersect接口
  • 【云原生】Kubernetes节点亲和性分配 Pod
  • 【Essential C++课后练习】纯代码(更新中)
  • C#仿热血江湖GClass
  • [SQL智慧航行者] - 用户购买商品推荐
  • Idea配置Scala开发环境
  • LT8711UXD 是一款高性能双通道 Type-C/DP1.4 至 HDMI2.0 转换器
  • Android APK体积优化(瘦身)
  • python技术栈 之 单元测试中mock的使用
  • python 提取冒号和逗号内的字符串
  • CentOS安装Postgresql
  • 云原生可观测框架 OpenTelemetry 基础知识(架构/分布式追踪/指标/日志/采样/收集器)...
  • 多用户跨境电商商品库系统快速搭建(全开源)
  • DataGrip 配置 HiveServer2 远程连接访问
  • 异常的使用
  • 软件安全测试包含哪些内容和方法?安全测试报告的必要性
  • 【代码随想录-leetcode第四题 20.有效的括号】
  • 造个轮子-任务调度执行小框架-IOC容器实现
  • npm发包中一些操作备忘
  • 15_基于Flink将pulsar数据写入到ClickHouse
  • Pycharm如何打断点进行调试?
  • 微服务02-docker
  • CSS:盒子模型 与 多种横向布局方法