C#Unity Mathf.PingPong无法正常工作 [英] c# Unity Mathf.PingPong not working

查看:473
本文介绍了C#Unity Mathf.PingPong无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的价值保持不变,而不是被乒乓

My value stays the same rather than being pingponged

IEnumerator CrossHairScale(){
    float size = 0;
    float finalsize = Mathf.Lerp (minSize, maxSize, Mathf.PingPong(size, 1));

    while (transform != null) {
        transform.localScale = new Vector3 (finalsize, finalsize, finalsize);
        yield return null;
    }
}

推荐答案

您误解了

You have misunderstood the usage of Mathf.PingPong(float t, float length). It accepts two arguments: t is the value to be limited, length is the maximum value that can be returned. When t is outside the range of [0, length], that's when the ping-pong effect occurs.

这是怎么回事

在您的代码中,您传递给Mathf.PingPong()的唯一t值是不变值0,由

In your code, the only t value you ever pass into Mathf.PingPong() is an unchanging value of 0, given by

float size = 0;

这意味着,下一行代码基本上是

That means, the next line of code is basically

float finalsize = Mathf.Lerp (minSize, maxSize, Mathf.PingPong(0, 1));

Mathf.PingPong(0, 1) = 0,所以您的代码真正在做的是

And Mathf.PingPong(0, 1) = 0, so all your code is really doing is

float finalsize = Mathf.Lerp (minSize, maxSize, 0);

最终,你总是会回来的

float finalsize = minSize;

那么,如何解决这个问题?

您需要做的是增加传递给Mathf.PingPong()t值(在这种情况下为size)-否则,每次总是求值为相同的值,分配给localScale不会更改.考虑改用以下代码:

What you need to be doing is incrementing the t value (in this case, size) you pass into Mathf.PingPong() - otherwise, it's always going to evaluate to the same value each time, and the value you assign to localScale won't change. Consider trying this code instead:

IEnumerator CrossHairScale(){
    float size = 0;

    while (transform != null) {
        // Here, we change size according to the time since the last frame
        size += Time.deltaTime;

        // Now, Mathf.PingPong() will return a value bouncing between 0 and 1
        float finalsize = Mathf.Lerp (minSize, maxSize, Mathf.PingPong(size, 1));

        transform.localScale = new Vector3 (finalsize, finalsize, finalsize);
        yield return null;
    }
}

希望这会有所帮助!如果您有任何问题,请告诉我.

Hope this helps! Let me know if you have any questions.

这篇关于C#Unity Mathf.PingPong无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆