EKsumic's Blog

let today = new Beginning();

Click the left button to use the catalog.

DE

[Unity] Mathf.Repeat函数和Mathf.PingPong函数

直接上代码:

        //输入 012345678
        //输出 012012012
        for (int i = 0; i < 9; i++)
        {
            Debug.Log(Mathf.Repeat(i,3));
        }

        //输入012345678
        //输出012321012
        for (int i = 0; i < 9; i++)
        {
            Debug.Log(Mathf.PingPong(i,3));
        }

这里i是迭代数。

 

应用场景:

①当你需要重复某个动画时:

单向

②当你需要像乒乓一样来回时:

pingpong

 

代码1:

    Vector3 start;
    float i = 0;
    // Use this for initialization
    void Start () {
        start = transform.position;
    }

    // Update is called once per frame
    void Update () {
        i += 0.1f;
        float l = Mathf.Repeat(i,5);
        transform.position = start + Vector3.left * l;
    }

代码2:

    Vector3 start;
    float i = 0;
    // Use this for initialization
    void Start () {
        start = transform.position;
    }

    // Update is called once per frame
    void Update () {
        i += 0.1f;
        float l = Mathf.PingPong(i,5);
        transform.position = start + Vector3.left * l;
    }

 

Mathf.Repeat函数和Mathf.PingPong函数

相同点:都是在重复

不同点:Repeat是直接重复,123123123……

PingPong是来回重复,123212321……

 

在使用场景上,PingPong可能会用于敌人的巡逻。

 

 

参考文档:

[1] unity笔记3 - Mathf.Repeat and Mathf.PingPong

This article was last edited at 2020-04-08 11:22:04

* *