# 使用常见的数学函数

> Use Unity's Mathf class to apply trigonometric, logarithmic, and other math functions commonly required in game development.

Unity 的 Mathf 类提供了一组常见的数学函数，包括三角函数、对数函数以及游戏和应用开发中常用的其他函数。

此页面概述了 Mathf 类以及使用该类编写脚本时的常见用法。有关 Mathf 类的每个成员的详尽参考，请参阅 [Mathf 脚本参考](/engine/6000.0/script-reference/unity-engine/mathf.md)。

## 三角函数

Unity 的所有三角函数都使用**弧度**。

* [`Sin`](/engine/6000.0/script-reference/unity-engine/mathf/sin.md)
* [`Cos`](/engine/6000.0/script-reference/unity-engine/mathf/cos.md)
* [`Tan`](/engine/6000.0/script-reference/unity-engine/mathf/tan.md)
* [`Asin`](/engine/6000.0/script-reference/unity-engine/mathf/asin.md)
* [`Acos`](/engine/6000.0/script-reference/unity-engine/mathf/acos.md)
* [`Atan`](/engine/6000.0/script-reference/unity-engine/mathf/atan.md)
* [`Atan2`](/engine/6000.0/script-reference/unity-engine/mathf/atan-2.md)

[`PI`](/engine/6000.0/script-reference/unity-engine/mathf/pi.md) 可用作常量，您可以乘以静态值 [`Rad2Deg`](/engine/6000.0/script-reference/unity-engine/mathf/rad-2-deg.md) 或 [`Deg2Rad`](/engine/6000.0/script-reference/unity-engine/mathf/deg-2-rad.md) 以在弧度和度之间进行转换。

## 幂和平方根

Unity 提供了您通常会用到的幂和平方根函数：

* [`Pow`](/engine/6000.0/script-reference/unity-engine/mathf/pow.md)
* [`Sqrt`](/engine/6000.0/script-reference/unity-engine/mathf/sqrt.md)
* [`Exp`](/engine/6000.0/script-reference/unity-engine/mathf/exp.md)

还有一些有用的 2 的幂相关函数。这在处理常见的二进制数据大小时很有用，这些数据大小通常被限制或优化为二的幂值（例如纹理尺寸）：

* [`ClosestPowerOfTwo`](/engine/6000.0/script-reference/unity-engine/mathf/closest-power-of-two.md)
* [`NextPowerOfTwo`](/engine/6000.0/script-reference/unity-engine/mathf/next-power-of-two.md)
* [`IsPowerOfTwo`](/engine/6000.0/script-reference/unity-engine/mathf/is-power-of-two.md)

## 插值

Unity 的插值函数允许您计算位于两个给定点之间某处的值。其中的函数行为各不相同，适用于不同的情况。有关更多信息，请参阅各自的示例：

* [`Lerp`](/engine/6000.0/script-reference/unity-engine/mathf/lerp.md)
* [`LerpAngle`](/engine/6000.0/script-reference/unity-engine/mathf/lerp-angle.md)
* [`LerpUnclamped`](/engine/6000.0/script-reference/unity-engine/mathf/lerp-unclamped.md)
* [`InverseLerp`](/engine/6000.0/script-reference/unity-engine/mathf/inverse-lerp.md)
* [`MoveTowards`](/engine/6000.0/script-reference/unity-engine/mathf/move-towards.md)
* [`MoveTowardsAngle`](/engine/6000.0/script-reference/unity-engine/mathf/move-towards-angle.md)
* [`SmoothDamp`](/engine/6000.0/script-reference/unity-engine/mathf/smooth-damp.md)
* [`SmoothDampAngle`](/engine/6000.0/script-reference/unity-engine/mathf/smooth-damp-angle.md)
* [`SmoothStep`](/engine/6000.0/script-reference/unity-engine/mathf/smooth-step.md)

注意，[Vector 类](/engine/6000.0/manual/scripting/object-oriented-development/vectors.md)和 [`Quaternion`](/engine/6000.0/manual/scripting/object-oriented-development/class-quaternion.md) 类都有自己的插值函数（如 Quaternion.Lerp），允许您在多个维度上插入位置、方向和旋转。

## 限制和重复值

这些简单的辅助函数在游戏或应用中通常很有用，当您需要将值限制在某个范围内或在某个范围内重复时，可以为您节省时间。

* [`Max`](/engine/6000.0/script-reference/unity-engine/mathf/max.md) 和 [`Min`](/engine/6000.0/script-reference/unity-engine/mathf/min.md)
* [`Repeat`](/engine/6000.0/script-reference/unity-engine/mathf/repeat.md) 和 [`PingPong`](/engine/6000.0/script-reference/unity-engine/mathf/ping-pong.md)
* [`Clamp`](/engine/6000.0/script-reference/unity-engine/mathf/clamp.md) 和 [`Clamp01`](/engine/6000.0/script-reference/unity-engine/mathf/clamp-01.md)
* [`Ceil`](/engine/6000.0/script-reference/unity-engine/mathf/ceil.md) 和 [`Floor`](/engine/6000.0/script-reference/unity-engine/mathf/floor.md)

## 对数

[`Log`](/engine/6000.0/script-reference/unity-engine/mathf/log.md) 函数用于计算指定数字的对数，可以是自然对数，也可以是指定底数的对数。此外，[`Log10`](/engine/6000.0/script-reference/unity-engine/mathf/log-10.md) 函数返回指定数值的以 10 为底的对数。

## 其他函数

有关 Mathf 类中函数的完整列表，请参阅 [Mathf 脚本参考](/engine/6000.0/script-reference/unity-engine/mathf.md)。
