在Unity社区分享经验,你也有机会获得官方推荐!发稿入口:unity.cn/articles

2020年度Unity价值博主、Unity价值专家招募中 更多Unity博主专属权益见文末

你是否也会为数学库函数而烦恼呢?小编看到许多大神都在发表相关的文章,忍不住把它们默默收集在经验目录中,今天就与大家一起分享。

Mathf 数学库函数浅析

1、Mathf.Abs 绝对值

计算并返回指定参数 f 绝对值。

2、Mathf.Acos 反余弦

static function Acos (f : float) : float

以弧度为单位计算并返回参数 f 中指定的数字的反余弦值。

3、Mathf.Approximately 近似

static function Approximately (a : float, b: float) : bool

比较两个浮点数值,看它们是否非常接近, 由于浮点数值不精确,不建议使用等于来比较它们。例如,1.0==10.0/10.0也许不会返回true。

public class example : MonoBehaviour {             public void Awake() {               if(Mathf.Approximately(5.0F, 50.0F / 50.0F))                  print("近似");             } }

右滑查看完整代码

4、Mathf.Asin 反正弦

static function Asin (f : float) : float

以弧度为单位计算并返回参数 f 中指定的数字的反正弦值。

5、Mathf.Atan2 反正切

static function Atan2 (y : float, x :float) : float

以弧度为单位计算并返回 y/x 的反正切值。返回值表示相对直角三角形对角的角,其中 x 是临边边长,而 y 是对边边长。

返回值是在 x 轴和一个二维向量开始于 0 个结束在 (x,y) 处之间的角。

public class example : MonoBehaviour {             public Transform target;             void Update() {                   Vector3 relative = transform.InverseTransformPoint(target.position);                   float angle = Mathf.Atan2(relative.x, relative.z) * Mathf.Rad2Deg;                   transform.Rotate(0,angle, 0);             } }

右滑查看完整代码6、Mathf.Atan 反正切static function Atan (f : float) :float计算并返回参数 f 中指定的数字的反正切值。返回值介于负二分之 pi 与正二分之 pi 之间。7、Mathf.CeilToInt 最小整数static function CeilToInt (f : float) : int返回最小的整数大于或等于f。8、Mathf.Ceil 上限值static function Ceil (f : float) : float返回 f 指定数字或表达式的上限值。数字的上限值是大于等于该数字的最接近的整数。9、Mathf.Clamp01 限制 0~1static function Clamp01 (value : float) :float限制 value 在 0,1 之间并返回 value 。如果 value 小于 0 ,返回 0。如果 value 大于 1 ,返回 1 ,否则返回 value  。10、Mathf.Clamp 限制static function Clamp (value : float, min :float, max : float) : float限制 value 的值在 min 和 max 之间, 如果 value 小于 min ,返回 min 。如果 value 大于 max ,返回 max ,否则返回 value 。static function Clamp (value : int, min :int, max : int) : int限制 value 的值在 min 和 max 之间,并返回 value 。11、Mathf.ClosestPowerOfTwo 最近的二次方static function ClosestPowerOfTwo (value :int) : int返回距离value最近的2的次方数。12、Mathf.Cos 余弦static function Cos (f : float) : float返回由参数 f 指定的角的余弦值(介于 -1.0 与 1.0 之间的值)。13、Mathf.Deg2Rad 度转弧度static var Deg2Rad : float度到弧度的转化常量。(只读)这等于(PI * 2) / 360。14、Mathf.Mathf.Rad2Deg 弧度转度static var Rad2Deg : float弧度到度的转化常量。(只读)这等于 360 / (PI * 2)。15、Mathf.DeltaAngle 增量角static function DeltaAngle (current :float, target : float) : float计算给定的两个角之间最短的差异。

// Prints 90 Debug.Log(Mathf.DeltaAngle(1080,90));

右滑查看完整代码

16、Mathf.Epsilon 小正数

static var Epsilon : float

一个很小的浮点数值。(只读)

最小的浮点值,不同于0。

以下规则:

-    anyValue + Epsilon = anyValue

-    anyValue - Epsilon = anyValue

-    0 + Epsilon = Epsilon

-    0 - Epsilon = -Epsilon

一个在任意数和Epsilon的之间值将导致在任意数发生截断误差。

public class example : MonoBehaviour {  bool isEqual(float a, float b) {    if(a >= b - Mathf.Epsilon && a <= b + Mathf.Epsilon)      return true;      else      return false;       }   }

右滑查看完整代码

17、Mathf.Exp 指数

static function Exp (power : float) : float

返回 e 的 power 次方的值。

18、Mathf.FloorToInt 最大整数

static function FloorToInt (f : float) :int

返回最大的整数,小于或等于f。

19、Mathf.Floor 下限值

static function Floor (f : float) : float

返回参数 f 中指定的数字或表达式的下限值。下限值是小于等于指定数字或表达式的最接近的整数。

20、Mathf.Infinity 正无穷

static var Infinity : float

表示正无穷,也就是无穷大,∞ (只读)

21、Mathf.InverseLerp 反插值

计算两个值之间的Lerp参数。也就是value在from和to之间的比例值。

//现在参数是3/5 float parameter =Mathf.InverseLerp(walkSpeed, runSpeed, speed);

右滑查看完整代码22、Mathf.IsPowerOfTwo 是否 2 的幂static function IsPowerOfTwo (value : int): bool如果该值是 2 的幂,返回 true 。

// prints false Debug.Log(Mathf.IsPowerOfTwo(7));// prints true  Debug.Log(Mathf.IsPowerOfTwo(32));

23、Mathf.LerpAngle 插值角度

static function LerpAngle (a : float, b :float, t : float) : float

和 Lerp 的原理一样,当他们环绕 360 度确保插值正确。

a和b是代表度数。

public class example : MonoBehaviour {  public float minAngle = 0.0F;  public float maxAngle = 90.0F;  void Update() {       float angle = Mathf.LerpAngle(minAngle, maxAngle, Time.time);       transform.eulerAngles= new Vector3(0, angle, 0);          }     } }

右滑查看完整代码

24、Mathf.Lerp 插值

static function Lerp (from : float, to :float, t : float) : float

基于浮点数t返回 a 到 b 之间的插值,t限制在 0~1 之间。

当 t = 0 返回 from ,当 t = 1 返回 to 。当 t = 0.5 返回 from 和 to 的平均值。

25、Mathf.Log10 基数10的对数

static function Log10 (f : float) : float

返回f的对数,基数为10。

26、Mathf.Log 对数

static function Log (f : float, p : float): float

返回参数 f 的对数。

// logarithm of 6 in base 2 //以2为底6的对数 // prints 2.584963 print(Mathf.Log(6, 2));

27、Mathf.Max 最大值static function Max (a : float, b : float): floatstatic function Max (params values :float[]) : float返回两个或更多值中最大的值。28、Mathf.Min 最小值static function Min (a : float, b : float): floatstatic function Min (params values :float[]) : float返回两个或更多值中最小的值。29、Mathf.MoveTowardsAngle 移动角static function MoveTowardsAngle (current :float, target : float, maxDelta : float) : float像 MoveTowards ,但是当它们环绕 360 度确保插值正确。变量 current 和 target 是作为度数。为优化原因,maxDelta 负值的不被支持,可能引起振荡。从target 角推开 current ,添加 180 度角代替。30、Mathf.MoveTowards 移向static function MoveTowards (current :float, target : float, maxDelta : float) : float改变一个当前值向目标值靠近。这实际上和 Mathf.Lerp 相同,而是该函数将确保我们的速度不会超过 maxDelta 。maxDelta 为负值将目标从推离。31、Mathf.NegativeInfinity 负无穷static var NegativeInfinity : float表示负无穷,也就是无穷小,-∞(只读)32、Mathf.NextPowerOfTwo  下个2的幂

//Prints 8 to the consoleDebug.Log(Mathf.NextPowerOfTwo(7));

33、Mathf.PingPong 乒乓

static function PingPong (t : float, length: float) : float

0 到 length 之间往返。t 值永远不会大于 length 的值,也永远不会小于 0 。

The returned value will move back and forthbetween 0 and length.

返回值将在 0 和 length 之间来回移动。

34、Mathf.PI 圆周率

static var PI : float

PI(读pai)的值,也就是圆周率(π)的值3.14159265358979323846...(只读)

35、Mathf.Pow 次方

static function Pow (f : float, p : float): float

计算并返回 f 的 p 次方。

36、Mathf.Repeat 重复

static function Repeat (t : float, length :float) : float

循环数值 t , 0 到 length 之间。t 值永远不会大于 length 的值,也永远不会小于 0 。

这是类似于模运算符,但可以使用浮点数。

public class example : MonoBehaviour {  void Update() {   transform.position= new Vector3(Mathf.Repeat(Time.time, 3), transform.position.y,transform.position.z);    }  }

右滑查看完整代码

37、Mathf.RoundToInt 四舍五入到整数

static function RoundToInt (f : float) :int

返回 f 指定的值四舍五入到最近的整数。

如果数字末尾 0.5 ,因此它是在两个整数中间,不管是偶数或是奇数,将返回偶数。

38、Mathf.Round 四舍五入

static function Round (f : float) : float

返回浮点数 f 进行四舍五入最接近的整数。

如果数字末尾是 0.5 ,因此它是在两个整数中间,不管是偶数或是奇数,将返回偶数。

39、Mathf.Sign 符号

static function Sign (f : float) : float

返回 f 的符号。

当 f 为正或为0返回1,为负返回-1。

40、Mathf.Sin 正弦

static function Sin (f : float) : float

计算并返回以弧度为单位指定的角 f 的正弦值。

41、Mathf.SmoothDampAngle 平滑阻尼角度

static function SmoothDampAngle (current :float, target : float, ref currentVelocity : float, smoothTime : float,maxSpeed : float = Mathf.Infinity, deltaTime : float = Time.deltaTime) : float

参数

current :当前的位置。

target :我们试图达到的位置。

currentVelocity :当前速度,这个值在你访问这个函数的时候会被随时修改。

smoothTime( the target faster) :要到达目标位置的近似时间,实际到达目标时要快一些。

maxSpeed :可选参数,允许你限制的最大速度。

deltaTime :上次调用该函数到现在的时间。缺省为Time.deltaTime。

随着时间的推移逐渐改变一个给定的角度到期望的角度。这个值通过一些弹簧减震器类似的功能被平滑。这个函数可以用来平滑任何一种值,位置,颜色,标量。最常见的是平滑一个跟随摄像机。

//一个简单的平滑跟随摄像机 //跟随目标的朝向 public class example : MonoBehaviour {    public Transform target;   public float smooth = 0.3F;   public float distance = 5.0F;    private float yVelocity = 0.0F;  void Update() {    //从目前的y角度变换到目标y角度    float yAngle = Mathf.SmoothDampAngle(transform.eulerAngles.y, target.eulerAngles.y,ref yVelocity, smooth);    //target的位置    Vector3 position = target.position;    //然后,新角度之后的距离偏移   position += Quaternion.Euler(0, yAngle, 0) * new Vector3(0, 0, -distance);    //应用位置    transform.position= position;    //看向目标    transform.LookAt(target);     }}

右滑查看完整代码

42、Mathf.SmoothDamp 平滑阻尼

static function SmoothDamp (current :float, target : float, ref currentVelocity : float, smoothTime : float,maxSpeed : float = Mathf.Infinity, deltaTime : float = Time.deltaTime) : float

参数

current :当前的位置。

target :我们试图达到的位置。

currentVelocity :当前速度,这个值在你访问这个函数的时候会被随时修改。

smoothTime :要到达目标位置的近似时间,实际到达目标时要快一些。

maxSpeed :可选参数,允许你限制的最大速度。

deltaTime :上次调用该函数到现在的时间。缺省为Time.deltaTime。

描述

随着时间的推移逐渐改变一个值到期望值。

这个值就像被一个不会崩溃的弹簧减振器一样被平滑。这个函数可以用来平滑任何类型的值,位置,颜色,标量。

public class example : MonoBehaviour {      public Transform target;      public float smoothTime = 0.3F;      private float yVelocity = 0.0F;      void Update() {          float newPosition = Mathf.SmoothDamp(transform.position.y, target.position.y, refyVelocity, smoothTime);          transform.position= new Vector3(transform.position.x, newPosition, transform.position.z);        } }

右滑查看完整代码

43、Mathf.SmoothStep 平滑插值

static function SmoothStep (from : float,to : float, t : float) : float

和lerp类似,在最小和最大值之间的插值,并在限制处渐入渐出。

public class example : MonoBehaviour {      public float minimum = 10.0F;      public float maximum = 20.0F;      void Update() {        transform.position= new Vector3(Mathf.SmoothStep(minimum, maximum, Time.time), 0, 0);    } }

右滑查看完整代码

44、Mathf.Sqrt 平方根

static function Sqrt (f : float) : float

计算并返回 f 的平方根。

45、Mathf.Tan 正切

static function Tan (f : float) : float 计算并返回以弧度为单位 f 指定角度的正切值。

C# 中常用的 Math 函数图表C# Math 图表文章虽有些平常乏味,但还是希望大家能留言共同讨论学习。- 喜欢本文,关注博主 -如果你喜欢本文的话,在官网 unity.cn 或 Unity Connect App 搜索关键词“数学库” 就可以找到这篇文章,还可以收藏起来,慢慢学习哦。

想要你的创意被更多人看见吗?快快加入Unity社区,在社区内分享经验,你也有机会获得Unity官方推荐哦! 博客发布入口:unity.cn/articles - “写文章"你 可 能 感 兴 趣

log函数 oracle power_博主营地 | Unity3D 实用技巧 基础数学库函数学习相关推荐

  1. log函数 oracle power_数学函数

    abs(x) → [same as input] 返回 x 的绝对值. cbrt(x) → double 返回 x 的立方根. ceil(x) → [same as input] ceiling() ...

  2. log函数 oracle power_Excel之数学函数SQRT/MOD/EXP/LN/RAND

    本部分主要包括ABS函数.SQRT函数.SIGN函数.MOD函数.POWER.EXP函数.LN函数.LOG函数.LOG10函数.RAND函数.RANDBETWEEN函数.PI函数.SIN函数.COS函 ...

  3. Unity3D 实用技巧 - 快速学会模型合理导入 Unity3D 引擎

    本文转自Unity Connect博主 北京琳云信息科技有限责任公司 学习模型合理导入 Unity3D 引擎 相信在游戏中,模型相当于游戏必要的元素,例如游戏的一个人物角色,我们可以称它为一个模型,今 ...

  4. python使用复合语句def创建函数对象_【收藏】Python实用技巧-成为Pythoner必经之路...

    前言 本文主要记录 Python 中一些常用技巧,所描述的是告诉你怎么写才是更好?  如果你并不熟悉Python语法,希望你能在下面代码片段中看到Python的简单.优雅; 如果你象我这样,对 Pyt ...

  5. python log函数怎么打_Python的log日志功能及设置方法

    python log函数怎么打_Python的log日志功能及设置方法_Elaine要当律师的博客-CSDN博客

  6. row number函数 oracle,oracle函数 ROW_NUMBER()

    [语法]ROW_NUMBER() OVER (PARTITION BY COL1 ORDER BY COL2) [功能]表示根据COL1分组,在分组内部根据 COL2排序,而这个值就表示每组内部排序后 ...

  7. django权限系统实现步骤_博主营地 | Unity红点系统如何实现?超全步骤分享

    「Unity博主营地第一期」于2019年11月开启,现已收到数百篇原创投稿.每周根据Unity Connect社区反馈,帮助大家发现最优质.最干货.最受欢迎的博文作品. 在使用Unity开发游戏的时候 ...

  8. html实现log函数,math。h中的log函数的应用

    以10为底的log函数: 形式为 double  log10(double  x) 以e为底的log函数(即 ln)double log (double x) 如何表达log 以a为底b的对数: 用换 ...

  9. R语言使用log函数计算对数、自定义指定底数

    R语言使用log函数计算对数.自定义指定底数 目录 R语言使用log函数计算对数.自定义指定底数 R语言是解决什么问题的? R语言使用log函数计算对数.自定义指定底数 安利一个R语言的优秀博主及其C ...

最新文章

  1. Jackson 通过自定义注解来控制json key的格式
  2. Redis的常用命令——set的常用命令
  3. php代码练习,PHP模拟测试练习
  4. Centos7 升级稳定版 openssl
  5. java什么是自动类型转换_java自动类型转换
  6. 获取移动光猫(如HS8545M5等设备)pppoe密码明文
  7. MindMaster Pro 8.0.0 — 亿图思维导图
  8. URL中的#是什么意思
  9. 直通车点击率、点击率、创意图、关键词、出价卡位,提升直通车点击率的技巧和方法
  10. LabVIEW如何开发大型程序
  11. 每月两个小小项目——CSS3简易照片墙
  12. eg:输出1~100之间能被7整除但不能同时被5整除的所有整数
  13. 设计模式-简单总结(不涉及代码,有需要戳)
  14. 【MySQL进阶】MySQL事务隔离与锁机制底层原理万字总结(建议收藏!!)
  15. gerrit 删除废弃的提交(Abandoned commit change)
  16. UTF-8编码方式汉字和英文各占据的字节数
  17. Rokid Air AR 眼镜 Cocos Creator XR 开发笔记
  18. 链脉电子名片,轻松帮助企业建立信息数据库
  19. 《教父》,没有什么电影是非看不可的
  20. 一本关于ChatGPT的书《ChatGPT 革命:了解大型语言模型的力量》免费下载

热门文章

  1. datefromstring 转换不准确_免费的在线OCR工具,将图片内容转换为文本内容
  2. 2017.5.7 过河 失败总结
  3. 自创算法——暴力自动机
  4. 查询引擎: SQL反解析(json2sql)(附源码)
  5. java mvc 注解_Spring MVC注解开发入门
  6. python f.write 保存图片到路径_装逼篇 | 抖音超火的九宫格视频是如何生成的,Python 告诉你答案...
  7. jedis-2.4.1 中的JedisPoolConfig没MaxActive属性
  8. python安装pyqt4_Python-Mac 安装 PyQt4
  9. tf.layers.conv2d_transpose 反卷积
  10. 错误:The project was not built due to Unparsed aapt error(s)