Unity 的 Mathf.PingPong 实际上是做什么的? [英] What does Unity's Mathf.PingPong actually do?

查看:57
本文介绍了Unity 的 Mathf.PingPong 实际上是做什么的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


其中length是每条线段的长度,t是函数的X值(返回值是图上对应的Y位置)

Mathf.PingPong(float t, float length) 看起来更像这样

同样,length 描述每个三角形的高度,t 给出 X 位置

一个常见的用例是我们希望一些值随着这个图一起变化,就像沿着它稳步增加的 X 值走一样,并在每一步取 Y 的值.最简单的方法是将 Time.time 作为 t 参数传入,这将获得该函数在相应 X 位置的值.

float Y = Mathf.PingPong(Time.time,Max_Y_Value);

The Unity Docs for Mathf.PingPong says:

PingPongs the value t, so that it is never larger than length and never smaller than 0.

I get that it's rotating a value between 0 and length, what I don't get is what is the value t doing and how does it relate to how PingPong works?

If I set t to any constant, I always get that value back

void Update()
{
// always prints: 1
    print(Mathf.PingPong(1f, 1f));

// always prints 0
    print(Mathf.PingPong(0f, 1f));
}

Every time I see PingPong used in examples it has Time.time used for the t value (or some maths based on Time.time). Why?

The only explanation I've seen was from this question: c# Unity Mathf.PingPong not working which implies that the value of t must always be changing, but again it's not clear why or what's going on.

So, what is Mathf.PingPong actually doing with t / what is the value t actually for, and how do you use the function properly?

解决方案

So Mathf.PingPong() uses a function calls Mathf.Repeat()

These are likely intended as tweening/easing helper functions

So Mathf.Repeat(float t, float length) will create a graph like this

Where length is the length of each line segment, and t is the X value of the function (the return value being the corresponding Y position on the graph)

What Mathf.PingPong(float t, float length) does looks more like this

Again where length describes the height of each triangle and t gives the X position

A common use case for this is we want some value to change along with this graph, as in walking along it with a steadily increasing X value, and take the value of Y at each step. The easiest way to do this is to pass Time.time in as the t argument, which will get the value of this function at the corresponding X position.

float Y = Mathf.PingPong(Time.time,Max_Y_Value);

这篇关于Unity 的 Mathf.PingPong 实际上是做什么的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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