如何在使用 Transform.RotateAround 时使用指数缓动函数?

发布于 01-12 13:38 字数 955 浏览 3 评论 0原文

我有一个场景,其中一个立方体形状的物体在平坦的表面上“滚动”。每次,它都会在 pivotAxis(垂直于运动方向)上围绕 pivotPoint(与表面接触的边缘)完成 90 度旋转。为此,Transform.RotateAround 函数似乎很合适,因为它允许我提供这些参数并应用这种偏心旋转带来的必要位置变化。该运动的时间限制为totalDuration

这是在应用运动的协程内部:

timePassed = 0f;
while (timePassed < totalDuration)
        {
            timePassed += Time.deltaTime;
            rotationAmount = (Time.deltaTime / totalDuration) * 90f;
            cube.RotateAround(pivotPoint, pivotAxis, rotationAmount);

            // Track rotation progress in case the function is interrupted
            offBalanceDegrees += rotationAmount;

            yield return null;
        }
// snap position and rotation of cube to final value, then repeat

这工作得很好,并且给了我线性运动,因为每次迭代中的旋转量是相同的。但是,我希望它具有缓动功能,可以缓慢开始并快速完成。我仍然必须能够指定 pivotPointpivotAxistotalDuration,但我对如何实现这一点有点困惑。

我见过的其他示例围绕对象中心进行旋转,这比我的情况更简单。

I have a scene where a cube-shaped object "rolls" on a flat surface. Each time, it completes a 90-degree rotation around pivotPoint (the edge making contact with the surface) over the pivotAxis (perpendicular to the direction of movement). For this, the Transform.RotateAround function seemed appropriate, as it allows me to supply those parameters and apply the necessary position change that comes with this kind of off-center rotation. The movement has a time constraint of totalDuration.

This is inside the coroutine where movement is applied:

timePassed = 0f;
while (timePassed < totalDuration)
        {
            timePassed += Time.deltaTime;
            rotationAmount = (Time.deltaTime / totalDuration) * 90f;
            cube.RotateAround(pivotPoint, pivotAxis, rotationAmount);

            // Track rotation progress in case the function is interrupted
            offBalanceDegrees += rotationAmount;

            yield return null;
        }
// snap position and rotation of cube to final value, then repeat

This works well enough and gives me linear movement, as the rotationAmount in each iteratio is the same. However, I'd like this to feature an easing function to start off slowly and finish quickly. I must still be able to specify a pivotPoint, pivotAxis and totalDuration, and I'm a bit stuck as to how I could accomplish this.

Other examples I've seen applied rotation around the object's centre, which is a simpler case than what I have.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

℡Ms空城旧梦2025-01-19 13:38:18

如果您的线性 t 介于 0 和 1 之间,则可以使用 tnew = t * t * t; 获得三次方 tnew。然后,您只需重新配置方程以使用从 0 到 1 的 t

总而言之,这可能如下所示:

timePassed = 0f;
offBalanceDegrees = 0f;
while (timePassed < totalDuration)
{
    timePassed += Time.deltaTime;

    float t = timePassed / totalDuration
    t = t * t * t;

    // Track rotation progress in case the function is interrupted
    oldRotationAmount = offBalanceDegrees;
    offBalanceDegrees = t * 90f;

    cube.RotateAround(pivotPoint, pivotAxis, offBalanceDegrees - oldRotationAmount);

    yield return null;
}

If you have a linear t between 0 and 1, you can use tnew = t * t * t; to get a cubic tnew. Then you just need to reconfigure your equation to use a t that goes from 0 to 1.

Altogether, this might look like this:

timePassed = 0f;
offBalanceDegrees = 0f;
while (timePassed < totalDuration)
{
    timePassed += Time.deltaTime;

    float t = timePassed / totalDuration
    t = t * t * t;

    // Track rotation progress in case the function is interrupted
    oldRotationAmount = offBalanceDegrees;
    offBalanceDegrees = t * 90f;

    cube.RotateAround(pivotPoint, pivotAxis, offBalanceDegrees - oldRotationAmount);

    yield return null;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文