Unity 的 Mathf.PingPong 实际上是做什么的?

编程入门 行业动态 更新时间:2024-10-27 01:34:49
本文介绍了Unity 的 Mathf.PingPong 实际上是做什么的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..


其中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屋!

更多推荐

[db:关键词]

本文发布于:2023-04-28 17:54:26,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1176887.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:做什么   Unity   Mathf   PingPong

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!