C# Mathf

Mathf 数学函数

常见的数学函数集合

Mathf.Abs 绝对值

返回f的绝对值

print(Mathf.Abs(-10.5F));  //10.5

Mathf.Acos 反余弦

返回参数 f 的反余弦值 - 弧度角的余弦是f

print(Mathf.Acos(0.5F));

Mathf.Approximately 近视值

比较两个浮点数值,看它们是否非常接近

if (Mathf.Approximately(1.0F, 10.0F / 10.0F))
print("same");

Mathf.Asin 反正弦

返回参数 f 的反正弦值 - 弧度角的正弦是f

print(Mathf.Asin(0.5F));

Mathf.Atan 反正切

返回参数 f 的反正切值 - 弧度角的正切是f。返回值介于负二分之 pi 与正二分之 pi 之间

print(Mathf.Atan(0.5F));

Mathf.Atan2 反正切2

返回弧度角的正切是y/x

var relative : Vector3 = transform.InverseTransformPoint(target.position); 
var angle : float = Mathf.Atan2(relative.x, relative.z) * Mathf.Rad2Deg; 
transform.Rotate (0, angle, 0);

Mathf.Ceil 向上进位取整

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

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

Mathf.CeilToInt 向上进位取整

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

Debug.Log(Mathf.CeilToInt(10.2F));   //11
Debug.Log(Mathf.CeilToInt(-10.2F));  //-10

Mathf.Clamp 钳制

限制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 钳制01

限制value在0,1之间并返回value。如果value小于0,返回0。如果value大于1,返回1,否则返回value

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

Mathf.ClosestPowerOfTwo 最接近二次方

返回距离value最近的2的次方数

Debug.Log(Mathf.ClosestPowerOfTwo(7));  //8
Debug.Log(Mathf.ClosestPowerOfTwo(19));  //16

Mathf.Cos 余弦

返回由参数 f 指定的角的余弦值(介于 -1.0 与 1.0 之间的值)

print(Mathf.Cos(3));

Mathf.Deg2Rad 度转弧度

度到弧度的转化常量

public float deg = 30.0F;
void Start() {
        float rad = deg * Mathf.Deg2Rad;
        Debug.Log(deg + "degrees are equal to " + rad + " radians.");
    }

Mathf.DeltaAngle 增量角

计算给定的两个角之间最短的差异

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

Mathf.Epsilon 最小正数

一个很小的浮点数值

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

Mathf.Exp 指数

返回 e 的 power 次方的值

print(Mathf.Exp(6));

Mathf.Floor 向下舍位取整

返回小于或等于该数的最大整数

Debug.Log(Mathf.Floor(10.2F));  //10
Debug.Log(Mathf.Floor(-10.2F));  //-10

Mathf.FloorToInt 向下舍位取整

返回小于或等于该数的最大整数

 Debug.Log(Mathf.FloorToInt(10.2F));  //10
Debug.Log(Mathf.FloorToInt(-10.2F));  //-10

Mathf.Infinity 正无穷

表示正无穷,也就是无穷大,∞

void Update() {
     Debug.DrawLine(Vector3.zero, Vector3.forward * 100);
     if (Physics.Raycast(Vector3.zero, Vector3.forward, Mathf.Infinity))
        print("There is something in front of the object!");
}

Mathf.InverseLerp 反插值

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

public float walkSpeed = 5.0F;
public float runSpeed = 10.0F;
public float speed = 8.0F;
void Start() {
    float parameter = Mathf.InverseLerp(walkSpeed, runSpeed, speed);
}

Mathf.IsPowerOfTwo 是否是二次方

如果该值是2的次方,返回true

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

Mathf.Lerp 插值

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

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

Mathf.LerpAngle 插值角度

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

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);
}

Mathf.Log 对数

返回指定基数的指定对数

print(Mathf.Log(6, 2));  //2.584963

Mathf.Log10 对数10

返回指定值的对数,基数为10

print(Mathf.Log10(100));  //2

Mathf.Max 最大值

返回两个或更多值中最大的值

print(Mathf.Max(1.2F, 2.4F));  //2.4

Mathf.Min 最小值

返回两个或更多值中最小的值

print(Mathf.Min(1.2F, 2.4F));  //1.2

Mathf.MoveTowards 移向

改变一个当前值向目标值靠近

public float currStrength;
public float maxStrength;
public float recoveryRate;
void Update() {
   currStrength = Mathf.MoveTowards(currStrength, maxStrength, recoveryRate * Time.deltaTime);
}

Mathf.MoveTowardsAngle 移向角度

像MoveTowards,但是当它们环绕360度时确保插值正确

public float target = 270.0F;
public float speed = 45.0F;
void Update() {
    float angle = Mathf.MoveTowardsAngle(transform.eulerAngles.y, target, speed * Time.deltaTime);
    ransform.eulerAngles = new Vector3(0, angle, 0);
}

Mathf.NegativeInfinity 负无穷

表示负无穷,也就是无穷小,-∞

Debug.Log(Mathf.NegativeInfinity);

Mathf.NextPowerOfTwo 最接近的二次方

返回最接近的二次方的值

Debug.Log(Mathf.NextPowerOfTwo(7));  // prints 8
Debug.Log(Mathf.NextPowerOfTwo(139));  // prints 256

Mathf.PI 圆周率

public float radius = 5;
void Start() {
    float perimeter = 2 * Mathf.PI * radius;
    Debug.Log("The perimeter of the circle is: " + perimeter);
}

Mathf.PingPong 乒乓

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

transform.position = new Vector3(Mathf.PingPong(Time.time, 3), transform.position.y, transform.position.z);

Mathf.Pow 幂

计算并返回 f 的 p 次幂

print(Mathf.Pow(6, 1.8F));

Mathf.Rad2Deg 弧度转度

弧度到度的转化常量

public float rad = 10.0F;
void Start() {
    float deg = rad * Mathf.Rad2Deg;
    Debug.Log(rad + " radians are equal to " + deg + " degrees.");
}

Mathf.Repeat 重复

循环t值,从来不会比length大,并且从不会小于0,取值在0~length之间

transform.position = new Vector3(Mathf.Repeat(Time.time, 3), transform.position.y, transform.position.z);

Mathf.Round 四舍五入

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

Debug.Log(Mathf.Round(10.2F));  //10
Debug.Log(Mathf.Round(10.7F));  //11
Debug.Log(Mathf.Round(-10.2F));  //-10
Debug.Log(Mathf.Round(-10.7F));  //-11

Mathf.RoundToInt 四舍五入到整数

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

Debug.Log(Mathf.RoundToInt(10.2F));  //10
Debug.Log(Mathf.RoundToInt(10.7F));  //11
Debug.Log(Mathf.RoundToInt(-10.2F));  //-10
Debug.Log(Mathf.RoundToInt(-10.7F));  //-11

Mathf.Sign 数字符号

返回 f 的数字符号

Debug.Log(Mathf.Sign(-10));
Debug.Log(Mathf.Sign(10));

Mathf.Sin 正弦

返回弧度角 f 的正弦值

print(Mathf.Sin(3));

Mathf.Sqrt 平方根

计算并返回 f 的平方根

print(Mathf.Sqrt(10));

Mathf.Tan 正切

返回互动f角的正切值

print(Mathf.Tan(0.5F));

 

 

  • 1
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值