【Unity】Mathf常用数学函数

描述

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

一、静态变量

1.Deg2Rad:度到弧度换算常量

度到弧度换算常量 = (PI * 2) / 360

	public float deg = 30.0F;

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

2.Epsilon:微小浮点值

浮点数可以与零相差的最小值。
采用以下规则: 任何值 + Epsilon = 任何值 任何值 - Epsilon = 任何值 0 + Epsilon = Epsilon 0 - Epsilon = -Epsilon 介于任何数字与 Epsilon 之间的值会产生任意数字,因为存在 截断误差。

using UnityEngine;

public class Example : MonoBehaviour
{
    
    
    // Compares two floating point numbers and return true if they are the same number.
    // See also Mathf.Approximately, which compares floating point numbers so you dont have
    // to create a function to compare them.

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

3.Infinity:正无穷大的表示形式(只读)

4.NegativeInfinity:负无穷大的表示形式(只读)

5.PI

众所周知的“3.14159265358979…”值(只读)。

圆周长与直径之比。注意,该值是 32 位浮点数,即 /float/。提供大约七位数的精度。

6.Rad2Deg:弧度到度换算常量

弧度到度换算常量(只读)。

这等于“360 / (PI * 2)”。

二、静态函数

1.Abs 绝对值

int num =  Mathf.Abs(-10);

2.Acos

返回 f 的反余弦 - 其余弦为 f 的角度(以弧度为单位)

using UnityEngine;

public class ScriptExample : MonoBehaviour
{
    
    
    void Start()
    {
    
    
        print(Mathf.Acos(0.5f));
    }
}

3.Approximately

比较两个浮点值,如果它们相似,则返回 true。

浮点不精确性使得使用相等运算符比较浮点数不精确。 例如,(1.0 == 10.0 / 10.0) 不会每次都返回 true。 Approximately() 比较两个浮点数,如果它们相互之间的差值处于较小值 (Epsilon) 范围内,则返回 true。

using UnityEngine;

public class ScriptExample : MonoBehaviour
{
    
    
    void Start()
    {
    
    
        if (Mathf.Approximately(1.0f, 10.0f / 10.0f))
        {
    
    
            print("The values are approximately the same");
        }
    }
}

4.Asin

返回 f 的反正弦 - 其正弦为 f 的角度(以弧度为单位)。

using UnityEngine;

public class ScriptExample : MonoBehaviour
{
    
    
    void Start()
    {
    
    
        print(Mathf.Asin(0.5f));
    }
}

5.Atan

返回 f 的反正切 - 其正切为 f 的角度(以弧度为单位)。

using UnityEngine;

public class ScriptExample : MonoBehaviour
{
    
    
    void Start()
    {
    
    
        print(Mathf.Atan(0.5f));
    }
}

6.Atan2

返回其 Tan 为 y/x 的角度(以弧度为单位)。

返回值是 X 轴与 2D 向量(从零开始,在 (x,y) 处终止)之间的 角度。

注意:此函数会考虑 x 为零的情况并返回 正确角度,而不是抛出被零除异常。

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

7.Ceil 向上取整

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

using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour
{
    
    
    void Example()
    {
    
    
        // 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));
    }
}

8.CeilToInt

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

using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour
{
    
    
    void Start()
    {
    
    
        // Prints 10
        Debug.Log(Mathf.CeilToInt(10.0f));
        // Prints 11
        Debug.Log(Mathf.CeilToInt(10.2f));
        // Prints 11
        Debug.Log(Mathf.CeilToInt(10.7f));

        // Prints -10
        Debug.Log(Mathf.CeilToInt(-10.0f));
        // Prints -10
        Debug.Log(Mathf.CeilToInt(-10.2f));
        // Prints -10
        Debug.Log(Mathf.CeilToInt(-10.7f));
    }
}

9.Clamp

将给定值钳制在给定最小数和最大数值定义的范围之间。如果在最小和最大范围内,则返回给定值。

如果给定值小于最小值,则返回最小值。如果给定值大于最大值,则返回最大值。min 和 max 参数包括在内。
例如,Clamp(10, 0, 5) 将返回最大参数为 5,而不是 4。

10.Cos

返回角度 f 的余弦。

注意:如果此函数使用非常大的数字,此方法的输入角度值具有可接受范围,超出该范围,计算会失败。在 Windows 上,可接受范围大约是 -9223372036854775295 到 9223372036854775295 之间。此范围在其他平台上可能有所不同。对于可接受范围之外的值,Cos 方法返回输入值,而不是抛出异常。

//f 	输入角度(以弧度为单位)。float 介于 -1 与 1 之间的返回值。 
public static float Cos (float f); 

11.DeltaAngle

计算两个给定角度(以度为单位给定)之间的最短差异。

using UnityEngine;

public class Example : MonoBehaviour
{
    
    
    void Start()
    {
    
    
        // Prints 90
        Debug.Log(Mathf.DeltaAngle(1080, 90));
    }
}

12.Exp

返回 e 的指定幂。

using UnityEngine;

public class Example : MonoBehaviour
{
    
    
    void Start()
    {
    
    
        print(Mathf.Exp(6));
    }
}

13.Floor 向下取整

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

using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour
{
    
    
    void Example()
    {
    
    
        Debug.Log(Mathf.Floor(10.0F));   // Prints  10
        Debug.Log(Mathf.Floor(10.2F));   // Prints  10
        Debug.Log(Mathf.Floor(10.7F));   // Prints  10
        Debug.Log(Mathf.Floor(-10.0F));  // Prints -10
        Debug.Log(Mathf.Floor(-10.2F));  // Prints -11
        Debug.Log(Mathf.Floor(-10.7F));  // Prints -11
    }
}

14.IsPowerOfTwo

如果值是 2 的幂,则返回 true。

using UnityEngine;

public class Example : MonoBehaviour
{
    
    
    void Start()
    {
    
    
        // prints false
        Debug.Log(Mathf.IsPowerOfTwo(7));

        // prints true
        Debug.Log(Mathf.IsPowerOfTwo(32));
    }
}

15.Lerp 线性插值

在 a 与 b 之间按 t 进行线性插值

参数 t 限制在范围 [0, 1] 内。

当 t = 0 时,返回 a 。
当 t = 1 时,返回 b 。
当 t = 0.5 时,返回 a 和 b 的中点。

using UnityEngine;

public class Example : MonoBehaviour
{
    
    
    // animate the game object from -1 to +1 and back
    public float minimum = -1.0F;
    public float maximum =  1.0F;

    // starting value for the Lerp
    static float t = 0.0f;

    void Update()
    {
    
    
        // animate the position of the game object...
        transform.position = new Vector3(Mathf.Lerp(minimum, maximum, t), 0, 0);

        // .. and increase the t interpolater
        t += 0.5f * Time.deltaTime;

        // now check if the interpolator has reached 1.0
        // and swap maximum and minimum so game object moves
        // in the opposite direction.
        if (t > 1.0f)
        {
    
    
            float temp = maximum;
            maximum = minimum;
            minimum = temp;
            t = 0.0f;
        }
    }
}

16.LerpAngle

与 Lerp 相同,但是在值环绕 360 度时确保值正确插入。

参数 t 限制在范围 [0, 1] 内。假设变量 a 和 b 以度为单位。

using UnityEngine;

public class Example : MonoBehaviour
{
    
    
    float minAngle = 0.0f;
    float maxAngle = 90.0f;

    void Update()
    {
    
    
        float angle = Mathf.LerpAngle(minAngle, maxAngle, Time.time);
        transform.eulerAngles = new Vector3(0, angle, 0);
    }
}

17.LerpUnclamped

在 a 与 b 之间按 t 进行线性插值,t 没有限制。

参数 t 不受限制,支持基于 a 和 b 的值。如果 t 小于零或大于一,则 LerpUnclamped 生成 a 到 b 范围之外的返回值。

假设参数 a = 0.33f,并且 b = 1.5f。如果插值器 t = -0.25f,则返回 值为 0.0375f。

详细信息:计算 (b - a) 为 1.17f。此值按 0.25f 进行缩放,获得 结果 0.2925f。将 a 减去此值(因为插值 t 为负) 得到 0.0375f。

//a 	起点值
//b 	终点值。
//t 	两个浮点数之间的插值。
//返回值float 作为线性插值结果的浮点值。 
public static float LerpUnclamped (float a, float b, float t); 

18.Log

返回指定的数字以指定的底数为底的对数。

using UnityEngine;

public class ScriptExample : MonoBehaviour
{
    
    
    void Start()
    {
    
    
        // logarithm of 6 in base 2
        // prints 2.584963
        Debug.Log(Mathf.Log(6, 2));
    }
}

返回指定的数字的自然(以 e 为底)对数。


using UnityEngine;

public class ScriptExample : MonoBehaviour
{
    
    
    void Start()
    {
    
    
        // natural logarithm of 100
        // prints 4.60517
        Debug.Log(Mathf.Log(100));
    }

19.Log10

返回指定的数字的以 10 为底的对数。

using UnityEngine;

public class Example : MonoBehaviour
{
    
    
    void Start()
    {
    
    
        // logarithm of 100 in base 10
        // Prints 2
        print(Mathf.Log10(100));
    }
}

20.Max

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

using UnityEngine;

public class ScriptExample : MonoBehaviour
{
    
    
    void Start()
    {
    
    
        // prints 2.4
        Debug.Log(Mathf.Max(1.2f, 2.4f));
    }
}

21.Min

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

using UnityEngine;

public class ScriptExample : MonoBehaviour
{
    
    
    void Start()
    {
    
    
        // prints 1.2
        Debug.Log(Mathf.Min(1.2f, 2.4f));
    }
}

22.PingPong

PingPong 返回一个值,该值将在值 0 与 length 之间递增和递减。

PingPong 要求值 t 为自递增值,例如 Time.time 和 Time.unscaledTime。

using UnityEngine;

public class PingPongExample : MonoBehaviour
{
    
    
    Light myLight;

    void Start()
    {
    
    
        myLight = GetComponent<Light>();
    }

    void Update()
    {
    
    
        myLight.intensity = Mathf.PingPong(Time.time, 8);
    }
}

23.Pow

返回 f 的 p 次幂。

using UnityEngine;

public class Example : MonoBehaviour
{
    
    
    void Start()
    {
    
    
        print(Mathf.Pow(6, 1.8f));
    }
}

24.Repeat

对值 t 进行循环,使它不会大于长度,并且不会小于 0。

这类似于取模运算符,但是它适用于浮点数。例如,将 3.0 用于 t 并将 2.5 用于 t,结果是 0.5。而当 t = 5 并且 length = 2.5 时,结果是 0.0。但是请注意,与取模运算符一样,没有为负数定义行为。

在下面的示例中,时间值限制为介于 0.0 与恰好低于 3.0 之间。然后它用于将 x 位置保持在 此范围内。

using UnityEngine;

public class Example : MonoBehaviour
{
    
    
    void Update()
    {
    
    
        // Set the x position to loop between 0 and 3
        transform.position = new Vector3(Mathf.Repeat(Time.time, 3), transform.position.y, transform.position.z);
    }
}

25.Round

返回舍入为最近整数的 /f/。

如果数字结尾是 .5,从而使它处于两个整数正中间(其中一个是偶数,另一个是奇数),则返回偶数。

using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour
{
    
    
    // Use this for initialization
    void Start()
    {
    
    
        // Prints 10
        Debug.Log(Mathf.Round(10.0f));

        // Prints 10
        Debug.Log(Mathf.Round(10.2f));

        // Prints 11
        Debug.Log(Mathf.Round(10.7f));

        // Prints 10
        Debug.Log(Mathf.Round(10.5f));

        // Prints 12
        Debug.Log(Mathf.Round(11.5f));

        // Prints -10
        Debug.Log(Mathf.Round(-10.0f));

        // Prints -10
        Debug.Log(Mathf.Round(-10.2f));

        // Prints -11
        Debug.Log(Mathf.Round(-10.7f));

        // Prints -10
        Debug.Log(Mathf.Round(-10.5f));

        // Prints -12
        Debug.Log(Mathf.Round(-11.5f));
    }
}

26.Sign

返回 f 的符号。

当 f 为正数或零时,返回值为 1,当 f 为负数时,返回值为 -1。

using UnityEngine;

public class Example : MonoBehaviour
{
    
    
    void Start()
    {
    
    
        Debug.Log(Mathf.Sign(-10));
        Debug.Log(Mathf.Sign(10));
    }
}

27.Sin

返回角度 f 的正弦。

注意:如果此函数使用非常大的数字,则此方法的输入角度值具有可接受范围,超出该范围,计算会失败。在 Windows 上,可接受范围大约是 -9223372036854775295 到 9223372036854775295 之间。此范围在其他平台上可能有所不同。对于可接受范围之外的值,Sin 方法返回输入值,而不是抛出异常。

//f 	输入角度(以弧度为单位)。
//返回值float 介于 -1 与 +1 之间的返回值。 
public static float Sin (float f); 

28.SmoothDamp

随时间推移将一个值逐渐改变为所需目标。

值通过某个类似于弹簧-阻尼的函数(它从不超过目标)进行平滑。 该函数可以用于平滑任何类型的值、位置、颜色、标量。
在这里插入图片描述

using UnityEngine;

public class Example : MonoBehaviour
{
    
    
    // Smooth towards the height of the target

    Transform target;
    float smoothTime = 0.3f;
    float yVelocity = 0.0f;

    void Update()
    {
    
    
        float newPosition = Mathf.SmoothDamp(transform.position.y, target.position.y, ref yVelocity, smoothTime);
        transform.position = new Vector3(transform.position.x, newPosition, transform.position.z);
    }
}

29.SmoothDampAngle

随时间推移将以度为单位给定的角度逐渐改变为所需目标角度。

值通过某个类似于弹簧-阻尼的函数(它不会超过目标)进行平滑。该函数可以用于平滑任何类型的值、位置、颜色、标量。 最常见的用法是用于平滑跟随摄像机。
在这里插入图片描述

30.SmoothStep

在 min 与 max 之间进行插值,在限制处进行平滑。

此函数采用与 Lerp 相似的方式在 min 与 max 之间进行插值。 但是,插值会从起点逐渐加速,然后朝着终点减慢。 这可用于创建表现十分自然的动画、淡化和其他过渡。

using UnityEngine;

public class Example : MonoBehaviour
{
    
    
    // Minimum and maximum values for the transition.
    float minimum = 10.0f;
    float maximum = 20.0f;

    // Time taken for the transition.
    float duration = 5.0f;

    float startTime;

    void Start()
    {
    
    
        // Make a note of the time the script started.
        startTime = Time.time;
    }

    void Update()
    {
    
    
        // Calculate the fraction of the total duration that has passed.
        float t = (Time.time - startTime) / duration;
        transform.position = new Vector3(Mathf.SmoothStep(minimum, maximum, t), 0, 0);
    }
}

31.Sqrt

返回 f 的平方根。

using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour
{
    
    
    // The formula made famous by Pythagoras, also used internally by
    // Vector3.Distance and several other standard functions.
    float HypotenuseLength(float sideALength, float sideBLength)
    {
    
    
        return Mathf.Sqrt(sideALength * sideALength + sideBLength * sideBLength);
    }
}

32.Tan

返回角度 f 的正切(以弧度为单位)。、

using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour
{
    
    
    void Example()
    {
    
    
        print(Mathf.Tan(0.5F));
    }
}

猜你喜欢

转载自blog.csdn.net/qq_30144243/article/details/137122197