【Unity】Mathf常用数学函数

这篇具有很好参考价值的文章主要介绍了【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

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

值通过某个类似于弹簧-阻尼的函数(它从不超过目标)进行平滑。 该函数可以用于平滑任何类型的值、位置、颜色、标量。
unity mathf,Unity 程序设计,unity,游戏引擎

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

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

值通过某个类似于弹簧-阻尼的函数(它不会超过目标)进行平滑。该函数可以用于平滑任何类型的值、位置、颜色、标量。 最常见的用法是用于平滑跟随摄像机。
unity mathf,Unity 程序设计,unity,游戏引擎

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 的正切(以弧度为单位)。、文章来源地址https://www.toymoban.com/news/detail-856227.html

using UnityEngine;
using System.Collections;

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

到了这里,关于【Unity】Mathf常用数学函数的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • C# Math和Mathf的使用(小数取整、四舍五入、取绝对值等)

    在C#中我们做一些数学计算时,常会见到Math和Mathf的使用。到底使用哪个,它们有什么区别? 首先了解下它们的定义: Math:是C#中封装好的用于数学计算的一个工具类,命名空间是System; Mathf:是Unity中封装好的用于数学计算的一个工具结构体,命名空间是UnityEngine。 事实上,

    2024年02月07日
    浏览(29)
  • Unity游戏程序设计——3D双人坦克大战

    3D多人坦克大战 ·Unity2019.4.29   ·Visual Studio 2019 双人坦克游戏: 坦克:可移动旋转,发射炮弹 子弹:按一定方向一定速度发射;炮弹周围会产生冲击波,击中坦克或接触地面后爆炸 坦克生命:坦克被击中后血条相应变化;血条不随坦克的旋转而旋转;血条减到小于等于0后爆

    2024年04月28日
    浏览(29)
  • Unity中常用函数使用方法

    Update() 正常更新,创建 JavaScript 脚本时默认添加这个方法,每一帧都会由系统调用一次该方法。 LateUpdate() 推迟更新,此方法在 Update() 方法执行完后调用,每一帧都调用一次。 FixedUpdate() 置于这个函数中的代码每隔一定时间执行一次。 Awake() 脚本唤醒,用于脚本的初始化,在

    2024年02月08日
    浏览(28)
  • Unity常用函数扩展(一)—— GetOrAddComponent()

    实现获取Transform或者GameObject上的组件,若获取不到则自动添加该组件

    2024年02月14日
    浏览(23)
  • Unity 编辑器选择器工具类Selection 常用函数和用法

    点击封面跳转下载页面 在Unity中,Selection类是一个非常有用的工具类,它提供了许多函数和属性,用于操作和管理编辑器中的选择对象。本文将介绍Selection类的常用函数和用法,并提供相应的示例代码。 功能: 获取或设置当前活动的上下文对象。 示例代码: 功能: 获取或

    2024年02月14日
    浏览(30)
  • Unity 编辑器预制体工具类PrefabUtility 常用函数和用法

    在Unity中,预制体(Prefab)是一种非常有用的工具,它允许我们创建可重复使用的对象和场景元素。Unity提供了许多内置的工具和函数来处理预制体,其中一个重要的类就是PrefabUtility。PrefabUtility类提供了一系列函数,用于创建、实例化和管理预制体。在本文中,我们将介绍

    2024年02月14日
    浏览(29)
  • 【Unity】认识常用的生命周期函数(Awake、Start、Update...)

    游戏的本质就是一个死循环 每一次循环都会处理游戏逻辑 并 更新一次游戏画面 之所以能看到画面在动 是因为 切换画面速度达到一定速度时 人眼就会认为画面是动态且流畅的 一帧就是执行了一次循环 Unity底层已经封装好了这个死循环 我们只需要利用Unity的生命周期函数的

    2024年02月02日
    浏览(24)
  • Unity Shader编辑器工具类ShaderUtil 常用函数和用法

    Unity的Shader编辑器工具类ShaderUtil提供了一系列函数,用于编译、导入和管理着色器。本文将介绍ShaderUtil类中的常用函数和用法。 函数签名: public static bool CompileShader(string source, out string error); CompileShader函数用于编译一个着色器。它接受一个字符串参数source,表示要编译的着

    2024年02月14日
    浏览(28)
  • 【学习笔记】unity脚本学习(五)【常用的方法函数Destroy、Instantiate 、SendMessage、invoke 、Coroutine】

    转载请注明出处:🔗https://blog.csdn.net/weixin_44013533/article/details/130233098 视频参考 极客学院Unity3D视频精讲课程 Object体系结构 可以看到MonoBehaviour继承自Component,Component继承自Object MonoBehaviour复习 MonoBehaviour官网API 之前学的start awake等都是Message事件响应函数,它们都是我们在脚本

    2024年02月04日
    浏览(32)
  • 计算数学表达式的程序(Java课程设计)

    团队名称 团队成 员介绍 任务分配 团队成员博客 XQ Warriors 徐维辉 负责计算器数据的算法操作,如平方数、加减乘除,显示历史计算记录 无 邱良厦(组长) 负责计算器的图形设计,把输入和结果显示在最上面的文本框 无 1)图形界面设计参考:Java Swing界面设计UI(全)_sw

    2024年01月21日
    浏览(32)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包