【Unity】Mathf

【Unity】Mathf

1.Math与Mathf

推荐使用Mathf

  • Math是C#中封装好的用于数学计算的工具 类,Math位于System命名空间中
  • Mathf是Unity中封装好的用于数学计算的工具 结构体,Mathf位于UnityEngine命名空间中
  • Mathf比Math方法多,不仅包含Math中的方法 还多了适用于游戏开发的方法

2.常用成员

  • Π PI
Debug.Log(Mathf.PI);//3.141593
  • 绝对值 Abs
Debug.Log(Mathf.Abs(-1));//1
  • 向上取整 CeilToInt
Debug.Log(Mathf.CeilToInt(1.1f));//2
  • 向下取整 FloorToInt
Debug.Log(Mathf.FloorToInt(1.99f));//1
  • 钳制函数 Clamp
Debug.Log(Mathf.Clamp(5, 1, 10));//5
Debug.Log(Mathf.Clamp(0, 1, 10));//1
Debug.Log(Mathf.Clamp(11, 1, 10));//10
  • 最大值 Max
Debug.Log(Mathf.Max(1, 2, 3, 4, 5, 8, 9, 10));//10
  • 最小值 Min
Debug.Log(Mathf.Min(5, 6, 8, 1, 8));//1
  • 数的n次幂 Pow
Debug.Log(Mathf.Pow(2, 4));//16
  • 四舍五入 RoundToInt
Debug.Log(Mathf.RoundToInt(1.5f));//2
Debug.Log(Mathf.RoundToInt(1.49f));//1
  • 平方根 Sqrt
Debug.Log(Mathf.Sqrt(64));//8
  • 是否是2的n次方 IsPowerOfTwo
Debug.Log(Mathf.IsPowerOfTwo(4));//true
Debug.Log(Mathf.IsPowerOfTwo(3));//false
  • 是否正负数 Sign
Debug.Log(Mathf.Sign(0));//1
Debug.Log(Mathf.Sign(-10));//-1
Debug.Log(Mathf.Sign(11));//1
  • 角度转换成弧度 Deg2Rad
180° == Π 弧度
Debug.Log(30 * Mathf.Deg2Rad);//0.5235988
  • 弧度转换角度 Rad2Deg
Debug.Log(0.5235988 * Mathf.Rad2Deg);//30.0000017482986
  • 正弦(弧度) Sin
Debug.Log(Mathf.Sin(30 * Mathf.Deg2Rad));//0.5
  • 余弦(弧度) Cos
Debug.Log(Mathf.Cos(60 * Mathf.Deg2Rad));//0.5
  • 反正弦 得到弧度 Asin
Debug.Log(Mathf.Asin(0.5f) * Mathf.Rad2Deg);//30
  • 反余弦 得到弧度 Acos
Debug.Log(Mathf.Acos(0.5f) * Mathf.Rad2Deg);//60

3.插值Mathf.Lerp

公式 Lerp : result = start + (end - start) * t

private float start = 0;
private float end = 0;
private float time = 0;
// Update is called once per frame
void Update()
{
    //插值Lerp : result = start + (end - start) * t
    //先快后慢 (end - start) 由大到小, 无限接近 但不会到达目标
    start = Mathf.Lerp(start, 10, Time.deltaTime);

    //优化 匀速变化 会达到目标
    time += Time.deltaTime;
    end = Mathf.Lerp(start, 10, time);
}

4.案例跟随

public GameObject Player;
public float speed;
private Vector3 pos;

private Vector3 targetPos;
private Vector3 startPos;
private float time;

void Update()
{
    //先快后慢
    //pos = transform.position;
    //pos.x = Mathf.Lerp(pos.x, Player.transform.position.x, Time.deltaTime * speed);
    //pos.y = Mathf.Lerp(pos.y, Player.transform.position.y, Time.deltaTime * speed);
    //pos.z = Mathf.Lerp(pos.z, Player.transform.position.z, Time.deltaTime * speed);
    //transform.position = pos;

    //匀速
    if (targetPos != Player.transform.position)
    {
        time = 0;
        targetPos = Player.transform.position;
        startPos = transform.position;
    }
    time += Time.deltaTime;
    pos.x = Mathf.Lerp(startPos.x, targetPos.x, time * speed);
    pos.y = Mathf.Lerp(startPos.y, targetPos.y, time * speed);
    pos.z = Mathf.Lerp(startPos.z, targetPos.z, time * speed);
    transform.position = pos;
}

因为作者精力有限,文章中难免出现一些错漏,敬请广大专家和网友批评、指正。

相关推荐

最近更新

  1. TCP协议是安全的吗?

    2024-05-04 12:42:03       19 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-05-04 12:42:03       20 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-05-04 12:42:03       20 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-05-04 12:42:03       20 阅读

热门阅读

  1. x86的开机过程(上)

    2024-05-04 12:42:03       9 阅读
  2. python经典百题之计算字符串中子串出现的次数

    2024-05-04 12:42:03       12 阅读
  3. 城市反无人机技术

    2024-05-04 12:42:03       9 阅读
  4. 算法提高之货币系统

    2024-05-04 12:42:03       10 阅读
  5. VIT(transformer在计算机视觉方面的应用)

    2024-05-04 12:42:03       12 阅读
  6. 锁定“用户人群”,远离“ 需求取舍难”

    2024-05-04 12:42:03       12 阅读
  7. SQL-慢查询的定位及优化

    2024-05-04 12:42:03       13 阅读
  8. 1、FreeCAD概述与架构

    2024-05-04 12:42:03       14 阅读