C#(Unity)_Math函数总结

C#(Unity)_Math函数总结

博主第一篇,编写的不太好,还望多多见谅,多多支持。

Mathf.Abs() 计算绝对值

正数的绝对值是它本身;负数的绝对值是它的相反数 。/1的绝对值为1,-1的绝对值为1/。


Mathf.Acos() 计算反余弦值

以弧度为单位计算并返回参数 f 中指定的数字的反余弦值。print(Mathf.Acos(0.5))。


Mathf.Approximately近似

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

public class example : MonoBehaviour { 
            publicvoid Awake() { 
                        if(Mathf.Approximately(1.0F, 10.0F / 10.0F)) 
                                    print("same"); 

            } 
}

Mathf.Asin() 计算反正弦值。

以弧度为单位计算并返回参数 f 中指定的数字的反正弦值。print (Mathf.Asin(0.5));


Mathf.Atan() 计算反正切值.

angel=Mathf.atan(slope) ,只接受一个参数angel为一个角度的弧度值,slope为直线的斜率,是一个数字,这个数字可以是负的无穷大到正无穷大之间的任何一个值.不过,利用他进行计算比较复杂.因为他的周期性,一个数字的反正切值不止一个.例如atan(-1)的值可能是45度,也可能是225度.这样就是他的周期性,对于正切函数来说,他的周期是180度,所以两个相差180度的角具有相同的正切和斜率:tanθ=tan(θ+180)然而,Mathf.atan()只能返回一个角度值,因此确定他的角度非常的复杂,而且,90度和270度的正切是无穷大,因为除数为零,我们也是比较难以处理的~!因此我们更多的会采用Mathf.atan2()。


Mathf.Atan2() 计算从x 坐标轴到点的角度。

Math.atan2()接受两个参数x和y,方法如下:angel=Math.atan2(y,x)
x 指定点的 x 坐标的数字。
y 指定点的 y 坐标的数字。
计算出来的结果angel是一个弧度值,也可以表示相对直角三角形对角的角,其中 x 是临边边长,而 y 是对边边长。
x=Mathf.atan2(7,7)
print(x)输出0.785398163397448/输出一个弧度值/
x=180x/Mathf.PI/转换为角度值/
print(x)//输出45。
x=Mathf.atan2(7,-7)
print(x)2.35619449019234
x=180
x/Mathf.PI//转换为角度值
print(x)135
//从这些测试可以看出,通过坐标系的自动调整,我们可以很自由的计算出处于不同象限的位置相对应的角度.
计算两点间连线的倾斜角:
Mathf.atan2()函数返回点(x,y)和原点(0,0)之间直线的倾斜角.那么如何计算任意两点间直线的倾斜角呢?只需要将两点x,y坐标分别相减得到一个新的点(x2-x1,y2-y1).然后利用他求出角度就可以了.使用下面的一个转换可以实现计算出两点间连线的夹角.
Mathf.atan2(y2-y1,x2-x1)
不过这样我们得到的是一个弧度值,在一般情况下我们需要把它转换为一个角度.
下面我们用一段代码来测试一下这样的转换.
//测试,计算点(3,3)和(5,5)构成的连线的夹角
x=Mathf.atan2(5-3,5-3)
print(x)/输出0.785398163397448/
x=x*180/Mathf.PI//转换为角度
print(x)//输出45。


Mathf.Clamp()限制数值的最大与最小。

Mathf.Clamp(value//,float min/最小值/,float max/最大值/)


Mathf.Clamp01限制0~1

print(Mathf.Clamp01(f/float类型/));
限制f在0,1之间并返回value。如果value小于0,返回0。如果value大于1,返回1,否则返回value 。


Mathf.Ceil() 将数字向上舍入为最接近的整数。

print(Mathf.celi(10));即为10。print(Mathf.ceil(10.2));即为11。print(Mathf.ceil(10.8));即为11。
print(Mathf.Ceil(f/float类型/));
返回 f 指定数字或表达式的上限值。数字的上限值是大于等于该数字的最接近的整数


Mathf.CeilToInt最小整数

print(Mathf.CeilToInt(f/float类型/));
返回最小的整数大于或等于f。


Mathf.ClosestPowerOfTwo最近的二次方

print(Mathf.ClosestPowerOfTwo(vule/int类型/));
返回距离value最近的2的次方数。


Mathf.Cos() 计算余弦值

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


Mathf.Deg2Rad度转弧度

static var Deg2Rad : float
度到弧度的转化常量。(只读)
这等于(PI * 2) / 360。


Mathf.Mathf.Rad2Deg 弧度转度

static var Rad2Deg : float
弧度到度的转化常量。(只读)
这等于 360 / (PI * 2)。


Mathf.DeltaAngle增量角

static function DeltaAngle (current :float, target : float) : float
计算给定的两个角之间最短的差异。
// Prints 90
Debug.Log(Mathf.DeltaAngle(1080,90));


Mathf.Epsilon小正数

static var Epsilon : float
一个很小的浮点数值。(只读)
最小的浮点值,不同于0。
以下规则:

  • anyValue + Epsilon = anyValue
  • anyValue - Epsilon = anyValue
  • 0 + Epsilon = Epsilon
  • 0 - Epsilon = -Epsilon
    一个在任意数和Epsilon的之间值将导致在任意数发生截断误差。
public class example : MonoBehaviour { 
            boolisEqual(float a, float b) { 
                        if(a >= b - Mathf.Epsilon && a <= b + Mathf.Epsilon) 
                                    returntrue; 
                        else 
                                    returnfalse; 
            } 
}

Mathf.Exp() 计算指数值

Mathf.Exp(Power/float/)返回e的Power次方。e值即为2.718282


Mathf.Floor() 将数字向下舍入为最接近的整数

print(Mathf.Floor(10));即为10。print(Mathf.Floor(10.2));即为10。print(Mathf.Floor(10.8));即为10。
print(Mathf.Floor(f/float类型/))
返回参数 f 中指定的数字或表达式的下限值。下限值是小于等于指定数字或表达式的最接近的整数。


Mathf.FloorToInt最大整数

print(Mathf.FloorToInt(f/float类型/))
返回最大的整数,小于或等于f。


Mathf.Infinity正无穷

static var Infinity : float
表示正无穷,也就是无穷大,∞ (只读)


Mathf.InverseLerp反插值

计算两个值之间的Lerp参数。也就是value在from和to之间的比例值。
//现在参数是3/5
float parameter =Mathf.InverseLerp(walkSpeed, runSpeed, speed);


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


Mathf.LerpAngle插值角度

static function LerpAngle (a : float, b :float, t : float) : float
和Lerp的原理一样,当他们环绕360度确保插值正确。
a和b是代表度数。

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

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的平均值。


Mathf.Log10基数10的对数

print(Mathf.Log10(f/float类型/))
返回f的对数,基数为10。


Mathf.log() 计算自然对数

print(Mathf.Log(6,2)),会返回以2为底6的对数,
print(Mathf.Log(6)),会返回以基数e为底的6的自然对数(e值看Mathf.Exp)


Mathf.max() 返回两个整数中较大的一个

print(Mathf.Max(6,2)),会返回最大值即为6,
print(Mathf.Max(params values :float[])),可填写数组的形式,返回数组中最大值。


Mathf.min() 返回两个整数中较小的一个。

print(Mathf.Max(6,2)),会返回最小值即为2,
print(Mathf.Max(params values :float[])),可填写数组的形式,返回数组中最小值。


Mathf.MoveTowardsAngle移动角

static function MoveTowardsAngle (current :float, target : float, maxDelta : float) : float
像MoveTowards,但是当它们环绕360度确保插值正确。
变量current和target是作为度数。为优化原因,maxDelta负值的不被支持,可能引起振荡。从target角推开current,添加180度角代替。


Mathf.MoveTowards移向

static function MoveTowards (current :float, target : float, maxDelta : float) : float
改变一个当前值向目标值靠近。
这实际上和 Mathf.Lerp相同,而是该函数将确保我们的速度不会超过maxDelta。maxDelta为负值将目标从推离


Mathf.NegativeInfinity负无穷

static var NegativeInfinity : float
表示负无穷,也就是无穷小,-∞(只读)


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之间来回移动


Mathf.PI 一个圆的周长与其直径的比值。

(即为π:大约为3.14159)。


Mathf.Pow() 计算x 的y 次方

print(Mathf.Pow(x:/float类型/,y:/float/类型)),
计算x值得y次方。


Mathf.Repeat重复

static function Repeat (t : float, length :float) : float
循环数值t,0到length之间。t值永远不会大于length的值,也永远不会小于0。
这是类似于模运算符,但可以使用浮点数。

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

Mathf.Round() 四舍五入为最接近的整数

print(Mathf.Round(f:/float类型/))四舍五入最接近的整数,
返回浮点数 f 进行四舍五入最接近的整数。
如果数字末尾是.5,因此它是在两个整数中间,不管是偶数或是奇数,将返回偶数。
例如2.5,将返回2,或3.5将返回4。


Mathf.RoundToInt四舍五入到整数

print(Mathf.Round(f:/float类型/))四舍五入最接近的整数,
返回 f 指定的值四舍五入到最近的整数。
如果数字末尾是.5,因此它是在两个整数中间,不管是偶数或是奇数,将返回偶数。


Mathf.Sign返回这个数的符号值

print(Mathf.Sign(f:/float/类型/))
当 f 为正或为0返回1,为负返回-1。


Mathf.Sin() 计算正弦值。

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


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 { 
            publicTransform target; 
            publicfloat smooth = 0.3F; 
            publicfloat distance = 5.0F; 
            privatefloat yVelocity = 0.0F; 
            voidUpdate() { 
//从目前的y角度变换到目标y角度 
                        floatyAngle = Mathf.SmoothDampAngle(transform.eulerAngles.y, target.eulerAngles.y,ref yVelocity, smooth); 
//target的位置 
                        Vector3position = target.position; 
//然后,新角度之后的距离偏移 
                        position+= Quaternion.Euler(0, yAngle, 0) * new Vector3(0, 0, -distance); 
//应用位置 
                        transform.position= position; 
//看向目标 
                        transform.LookAt(target); 
            } 
}

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 { 
            publicTransform target; 
            publicfloat smoothTime = 0.3F; 
            privatefloat yVelocity = 0.0F; 
            voidUpdate() { 
                        floatnewPosition = Mathf.SmoothDamp(transform.position.y, target.position.y, refyVelocity, smoothTime); 
                        transform.position= new Vector3(transform.position.x, newPosition, transform.position.z); 
            } 
} 

Mathf.SmoothStep平滑插值

static function SmoothStep (from : float,to : float, t : float) : float
和lerp类似,在最小和最大值之间的插值,并在限制处渐入渐出。

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

Mathf.Sqrt() 计算平方根。

print(X:/float类型/)计算X值得平方根。
(例如X为4,返回即为2。X为81,返回即为9)


Mathf.Tan() 计算正切值。

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


嗯,断断续续写了两三天也终于完成了,大致也了解了应该怎么编写博客,哈哈,原谅菜鸡的我从来没有编写过任何博客,本篇为第一篇。希望大家多多评论指出不足。

本篇献给爱学习的你。

最后特别要注意的是,我们发表的时候一定要选择【转载】,尊重原创!!

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值