直接上代码:
//输入 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是迭代数。
应用场景:
①当你需要重复某个动画时:
②当你需要像乒乓一样来回时:
代码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可能会用于敌人的巡逻。
参考文档:
Today's comments have reached the limit. If you want to comment, please wait until tomorrow (UTC-Time).
There is 18h40m18s left until you can comment.