unity日常——Mathf

unity日常—Mathf

这一次讲讲unity里面的Mathf函数
主要是加强我的记忆,有好多不常用的我会忘掉,就算是常用的我有时也会忘,在这里写下来哪一天忘了在来看看。

(一)Mathf.Abs绝对值

public static float Abs(float f);
计算并返回指定参数f绝对值
这里写图片描述
这里写图片描述


(二)Mathf.Acos反余弦

public static function Acos (f : float) : float
以弧度为单位计算并返回参数 f 中指定的数字的反余弦值。
这里写图片描述
这里写图片描述


(三)Mathf.Approximately近似

public static bool Approximately(float a, float b);
比较两个浮点值,如果它们相似,则返回true。
浮点不精确使得使用等号运算符的浮点数比较不准确。例如,(1 = 10 / 10)可能每次都不返回true。
这里写图片描述
这里写图片描述


(四)Mathf.Asin反正弦

public static float Asin(float f);
以弧度为单位计算并返回参数 f 中指定的数字的反正弦值。
这里写图片描述
这里写图片描述


(五)Mathf.Atan反正切

public static float Atan(float f);
计算并返回参数 f 中指定的数字的反正切值。返回值介于负二分之 pi 与正二分之 pi 之间。
这里写图片描述
这里写图片描述


(六)Mathf.Atan2反正切

public static float Atan2(float y, float x);
以弧度为单位计算并返回 y/x 的反正切值。返回值表示相对直角三角形对角的角,其中 x 是临边边长,而 y 是对边边长。
返回值是在x轴和一个二维向量开始于0个结束在(x,y)处之间的角。

using UnityEngine;
using System.Collections;

public class ExampleClass : 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);
    }
}

(七)Mathf.Ceil上限值

public static float Ceil(float f);
返回大于或等于f的最小整数。

        // Prints 10
        Debug.Log(Mathf.Ceil(10.0F));
        // Prints 11
        Debug.Log(Mathf.Ceil(10.2F));
        // Prints 11
        Debug.Log(Mathf.Ceil(10.7F));
        // Prints -10
        Debug.Log(Mathf.Ceil(-10.0F));
        // Prints -10
        Debug.Log(Mathf.Ceil(-10.2F));
        // Prints -10
        Debug.Log(Mathf.Ceil(-10.7F));

(八)Mathf.CeilToInt最小整数

public static int CeilToInt(float f);
返回大于或等于f的最小整数

        Debug.Log(Mathf.CeilToInt(10.0F));
        Debug.Log(Mathf.CeilToInt(10.2F));
        Debug.Log(Mathf.CeilToInt(10.7F));
        Debug.Log(Mathf.CeilToInt(-10.0F));
        Debug.Log(Mathf.CeilToInt(-10.2F));
        Debug.Log(Mathf.CeilToInt(-10.7F));

(九)Mathf.Clamp限制

public static float Clamp(float value, float min, float max);
public static int Clamp(int value, int min, int max);
限制value的值在min和max之间, 如果value小于min,返回min。 如果value大于max,返回max,否则返回value

transform.position = new Vector3(Mathf.Clamp(Time.time, 1.0F, 3.0F), 0, 0);

(十)Mathf.Clamp01限制0~1

public static float Clamp01(float value);
夹值在0到1之间,返回值。

transform.position = new Vector3(Mathf.Clamp01(Time.time), 0, 0);

(十一)Mathf.ClosestPowerOfTwo最近的二次方

public static int ClosestPowerOfTwo(int value);
返回距离value最近的2的次方数。

        Debug.Log(Mathf.ClosestPowerOfTwo(7));
        Debug.Log(Mathf.ClosestPowerOfTwo(19));

这里写图片描述


(十二)Mathf.CorrelatedColorTemperatureToRGB

public static Color CorrelatedColorTemperatureToRGB(float kelvin);
把温度(开氏度)变成颜色,kelvin范围在1000到40000。


(十三)Mathf.Cos余弦

public static float Cos(float f);
返回由参数 f 指定的角的余弦值(介于 -1.0 与 1.0 之间的值)。


(十四)Mathf.DeltaAngle增量角

public static float DeltaAngle(float current, float target);
计算给定角度的两个给定角度之间的最短差。

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

(十五)Mathf.Exp指数

public static float Exp(float power);
返回 e 的 power 次方的值。

 print(Mathf.Exp(6));

这里写图片描述


(十六)Mathf.Floor下限值

public static float Floor(float f);
返回小于或等于f的最大整数。

        Debug.Log(Mathf.Floor(10.0F));
        Debug.Log(Mathf.Floor(10.2F));
        Debug.Log(Mathf.Floor(10.7F));
        Debug.Log(Mathf.Floor(-10.0F));
        Debug.Log(Mathf.Floor(-10.2F));
        Debug.Log(Mathf.Floor(-10.7F));

这里写图片描述


(十七)Mathf.FloorToInt最大整数

public static int FloorToInt(float f);
返回小于或等于f的最大整数。

        Debug.Log(Mathf.FloorToInt(10.0F));
        Debug.Log(Mathf.FloorToInt(10.2F));
        Debug.Log(Mathf.FloorToInt(10.7F));
        Debug.Log(Mathf.FloorToInt(-10.0F));
        Debug.Log(Mathf.FloorToInt(-10.2F));
        Debug.Log(Mathf.FloorToInt(-10.7F));

这里写图片描述


(十八)Mathf.GammaToLinearSpace

public static float GammaToLinearSpace(float value);
转换gamma(sRGB)到linear color space。

没有写完,以后会完善的,有的我还没有用过所以有错误的请指出来,我会改正。

—— 这是官网 [ 传送门 ]

—— 上一篇 [ unity日常—函数执行顺序 ]
—— 下一篇 [ unity日常—游戏存档 ]

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值