我一直在寻找一种在 2D Unity 项目中创建类似这样的滚动效果的方法 https://www.desmos.com/calculator/mrfrugwvm5
问题是我不太擅长数学,所以我不确定如何将函数转换为线条渲染器的点。
有人可以帮我吗?
谢谢!
据我所知(以我的数学能力也有限)基本上有两个主要功能
y = -1 { 0 <= X <= P }
很简单:在 0 到 P 范围内画一条固定
y = -1
的直线
另一个基本上可以翻译为
var x = (1 - t/totalLength) * Mathf.Sin(t) + p;
var y = -(1 - t/totalLength) * Mathdlf.Cos(t);
和一个范围
{t < 14 - p}
并且实际上有两个参数
又是
P
t
,实际上是给定范围0
到14
(不知道为什么他们使用30
tbh)14
是最大总长度(=p
的上限)
这还仅限于
t < 14 - p
据我所知,在线条渲染器中这可能看起来像
public static class LineRendererExtensions
{
// p between 0 and 1
// resolution how many points to use - have in mind that the webpage you link
// basically uses as many points as your screen can render - something we shouldn't do in unity
public void DrawRolledLine(this LineRenderer line, float totalLength, float p, int resolution = 31)
{
p = Mathf.Clamp01(p) * totalLength;
var points = new Vector3[resolution];
line.positionCount = resolution;
// begin of straight line
points[0] = new Vector3(0, -1, 0);
for(var i = 1; i < resolution; i++)
{
// hoping this somewhat correctly maps the range
// of the total length onto the available line points
var t = (i - 1) * (resolution - 1) / totalLength;
var x = (1 - t/totalLength) * Mathf.Sin(t) + p;
var y = -(1 - t/totalLength) * Mathdlf.Cos(t);
points[i] = new Vector3(x,y);
}
line.SetPositions(points);
}
}
仅在手机上输入此内容,因此现在不确定这是否有效。但我希望它能让您了解这些函数如何工作以及或多或少如何将其分配给 LineRenderer。