# Unity Mathf 数学运算(C#)

smoothdamp，我的理解是平滑缓冲，东西不是僵硬的移动而是做减速缓冲运动到指定位置。我们看看代码：

 1 2 3 4 5 6 7 8 public Transform target;        //The player     public float smoothTime= 0.3f;  //Smooth Time     private Vector2 velocity;       //Velocity     void  Update ()     {         //Set the position         transform.position = new Vector3(Mathf.SmoothDamp(transform.position.x, target.position.x, ref velocity.x, smoothTime),Mathf.SmoothDamp( transform.position.y, target.position.y, ref velocity.y, smoothTime),transform.position.z);     }

Mathf.Abs绝对值

Mathf.Acos反余弦

static function Acos (f : float) : float

Mathf.Approximately近似

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

public class example : MonoBehaviour {

publicvoid Awake() {

if(Mathf.Approximately(1.0F, 10.0F / 10.0F))

print("same");

}

}

Mathf.Asin反正弦

static function Asin (f : float) : float

Mathf.Atan2反正切

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

public class example : MonoBehaviour {

publicTransform target;

voidUpdate() {

Vector3relative = transform.InverseTransformPoint(target.position);

floatangle = Mathf.Atan2(relative.x, relative.z) * Mathf.Rad2Deg;

transform.Rotate(0,angle, 0);

}

}

Mathf.Atan反正切

static function Atan (f : float) :float

Mathf.CeilToInt最小整数

static function CeilToInt (f : float) : int

Mathf.Ceil上限值

static function Ceil (f : float) : float

Mathf.Clamp01限制0~1

static function Clamp01 (value : float) :float

Mathf.Clamp限制

static function Clamp (value : float, min :float, max : float) : float

static function Clamp (value : int, min :int, max : int) : int

Mathf.ClosestPowerOfTwo最近的二次方

static function ClosestPowerOfTwo (value :int) : int

Mathf.Cos余弦

static function Cos (f : float) : float

Mathf.DeltaAngle增量角

static function DeltaAngle (current :float, target : float) : float

// Prints 90

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

Mathf.Epsilon小正数

static var Epsilon : float

-    anyValue + Epsilon = anyValue

-    anyValue - Epsilon = anyValue

-    0 + Epsilon = Epsilon

-    0 - 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指数

static function Exp (power : float) : float

Mathf.FloorToInt最大整数

static function FloorToInt (f : float) :int

Mathf.Floor下限值

static function Floor (f : float) : float

Mathf.Infinity正无穷

static var Infinity : float

Mathf.InverseLerp反插值

//现在参数是3/5

float parameter =Mathf.InverseLerp(walkSpeed, runSpeed, speed);

Mathf.IsPowerOfTwo是否2的幂

static function IsPowerOfTwo (value : int): bool

// 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

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

Mathf.Log10基数10的对数

static function Log10 (f : float) : float

Mathf.Log对数

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

// logarithm of 6 in base 2

//以2为底6的对数

// prints 2.584963

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

Mathf.Max最大值

static function Max (a : float, b : float): float

static function Max (params values :float[]) : float

Mathf.Min最小值

static function Min (a : float, b : float): float

static function Min (params values :float[]) : float

Mathf.MoveTowardsAngle移动角

static function MoveTowardsAngle (current :float, target : float, maxDelta : float) : float

Mathf.MoveTowards移向

static function MoveTowards (current :float, target : float, maxDelta : float) : float

Mathf.NegativeInfinity负无穷

static var NegativeInfinity : float

Mathf.NextPowerOfTwo下个2的幂

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.

Mathf.PI圆周率

static var PI : float

PI（读pai）的值，也就是圆周率（π）的值3.14159265358979323846...（只读）

Mathf.Pow次方

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

Mathf.Repeat重复

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

public class example : MonoBehaviour {

voidUpdate() {

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

}

}

Mathf.RoundToInt四舍五入到整数

static function RoundToInt (f : float) :int

Mathf.Round四舍五入

static function Round (f : float) : float

Mathf.Sign符号

static function Sign (f : float) : float

Mathf.Sin正弦

static function Sin (f : float) : float

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

//一个简单的平滑跟随摄像机

//跟随目标的朝向

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

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

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平方根

static function Sqrt (f : float) : float

Mathf.Tan正切

static function Tan (f : float) : float