Unity的Mathf数学方法使用

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq826364410/article/details/86516214
using UnityEngine;

public class MathfTest : MonoBehaviour
{
    public GameObject cube;
    private float deg = 30f;
    public float minimum = -1.0F;
    public float maximum = 1.0F;
    static float t = 0.0f;
    void Start()
    {
        //角度转化为弧度
        float radians = deg * Mathf.Deg2Rad;
        Debug.Log("Reg2Rad  " + radians);
        //弧度转化为角度
        deg = radians * Mathf.Rad2Deg;
        Debug.Log("Rad2Deg  " + deg);

        // 计算两个角度的最小夹角
        Debug.Log(Mathf.DeltaAngle(1080f, 90f));

        // 向上取整 大于等于当前值的最小float或者int
        // Prints 11
        //Mathf.CeilToInt
        Debug.Log(Mathf.Ceil(10.2F));

        // 大于1,返回1;小于0,返回0;在0-1之间,返回当前值
        Debug.Log(Mathf.Clamp01(10));

        // 返回约束值在1-3之间,大于3,返回3
        Debug.Log(Mathf.Clamp(10, 1, 3));

        // 向下取整 小于等于当前值的最大float或者int
        // Prints 10
        //Mathf.FloorToInt
        Debug.Log(Mathf.Floor(10.2F));

        // 四舍五入 如果数字以.5结尾,无论本身是偶数还是奇数,返回最接近的一个偶数
        // print 10
        Debug.Log(Mathf.RoundToInt(10.0F));
        // print 10
        Debug.Log(Mathf.RoundToInt(10.2F));
        // print 11
        Debug.Log(Mathf.RoundToInt(10.7F));
        // print 10
        Debug.Log(Mathf.RoundToInt(10.5F));
        // print 12
        Debug.Log(Mathf.RoundToInt(11.5F));
        // print -10
        Debug.Log(Mathf.RoundToInt(-10.0F));
        // print -10
        Debug.Log(Mathf.RoundToInt(-10.2F));
        // print -11
        Debug.Log(Mathf.RoundToInt(-10.7F));
        // print -10
        Debug.Log(Mathf.RoundToInt(-10.5F));
        // print -12
        Debug.Log(Mathf.RoundToInt(-11.5F));

        // unity官方给出一个方案,比如repeat用来循环播放动画012,pingpong用来往前播放往后播放动画012321
        //int[] a = { 0, 1, 2, 3, 4, 5, 6, 7, 8 };
        // Will print 0,1,2,0,1,2,0,1,2
        //foreach (var t in a)
        //{
        //    print(Mathf.Repeat(t, 3));
        //}
        // repeat length为3时,会循环输出012012012,当loop值为7时,对length取余
        print(Mathf.Repeat(7, 3));// 1
        print(Mathf.Repeat(7.3f, 3));// 1.3 如果取整大于0,返回对length取余的值
        print(Mathf.Repeat(0.02f, 3));// 0.02 如果取整小于0,直接返回loop值
        // Will print 0,1,2,3,2,1,0,1,2
        //foreach (var t in a)
        //{
        //    print(Mathf.PingPong(t, 3));
        //}

        //foreach (var t in a)
        //{
        //    print(MyPingPong(t, 3));
        //}
        print(Mathf.PingPong(8, 3));// 2
        print(Mathf.PingPong(7.3f, 3));// 1.3 如果取整大于0,返回对length取余的值
        print(Mathf.PingPong(0.02f, 3));// 0.02 如果取整小于0,直接返回loop值
    }

    // pingpong实现
    float MyPingPong(float t, float length)
    {
        var result = Mathf.Repeat(t, length * 2);
        if (result > length)
            result = (2 * length) - result;
        return result;
    }

    // Update is called once per frame
    void Update()
    {
        // pingpong 在5-10之间pingpong
        transform.position = new Vector3(5 + Mathf.PingPong(Time.time, 5), 0, 0);

        // 插值1
        // min 和 max的线性插值,t是一个min 和 max之间的比例
        // t等于0, 返回min, t等于1,返回max;t=0.5,返回平均值
        float lerp = Mathf.Lerp(minimum, maximum, t);
        transform.position = new Vector3(lerp, 0, 0);
        Debug.Log("lerp  " + lerp);
        // 插值增加t
        t += 0.5f * Time.deltaTime;
        // 现在检查插值器是否达到1.0,交换最大值和最小值,使游戏对象朝相反的方向移动。
        if (t > 1.0f)
        {
            float temp = maximum;
            maximum = minimum;
            minimum = temp;
            t = 0.0f;
        }

        // 插值2 先快后慢,插值比例一定,距离终点越近,移动越小。min 和 max的线性插值,t是一个min 和 max之间的比例
        float x = transform.position.x;
        transform.position = new Vector3(Mathf.Lerp(x, 10, Time.deltaTime), 0, 0);

        // Mathf.MoveTowards transform.position.x 以3m/s匀速移动,每秒位置加3米 deltaTime 为0.02s, 每秒50帧,每秒移动一米
        // 不会超过transform.position.x + 10目标值
        transform.position = new Vector3(Mathf.MoveTowards(transform.position.x, transform.position.x + 10, Time.deltaTime * 3), 0, 0);
    }
}

猜你喜欢

转载自blog.csdn.net/qq826364410/article/details/86516214
今日推荐