如何统一旋转 2D 对象 90 度

发布于 2025-01-17 03:54:28 字数 340 浏览 2 评论 0原文

我需要将 2D GameObject 按 z 轴旋转 90 度。所以我已经尝试过 transform.RotateAround 但仍然旋转一整圈:

transform.RotateAround(target.transform.position, new Vector3 (0, 0, -90),
        50f * Time.deltaTime)

我需要将其停止 90 度。我该怎么做?

在此处输入图片说明

I need to rotate 2D GameObject by 90 degrees by z axis. So I already tried transform.RotateAround but still rotates in a complete circle:

transform.RotateAround(target.transform.position, new Vector3 (0, 0, -90),
        50f * Time.deltaTime)

I need to stop it by 90. How can I do it?

enter image description here

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

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

发布评论

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

评论(1

弱骨蛰伏 2025-01-24 03:54:28

创建一个基于 deltaTime 旋转并跟踪的 协程旋转了多远,一旦到达 90 就停止。使用 Mathf.Min 确保旋转不超过 90。

private isRotating = false;

// speed & amount must be positive, change axis to control direction
IEnumerator DoRotation(float speed, float amount, Vector3 axis)
{
    isRotating = true;
    float rot = 0f;
    while (rot < amount)
    {
        yield return null;
        float delta = Mathf.Min(speed * Time.deltaTime, amount - rot);
        transform.RotateAround(target.transform.position, axis, delta);
        rot += delta;
    }
    isRotating = false;
}

然后,当您想要开始旋转时,如果协程尚未旋转,则启动协程:

if (!isRotating)
{
    StartCoroutine(DoRotation(50f, 90f, Vector3.back));
}

保存协程本身而不是使用旗帜。这使您可以执行诸如随时停止旋转之类的操作。

private Coroutine rotatingCoro;

// speed & amount must be positive, change axis to control direction
IEnumerator DoRotation(float speed, float amount, Vector3 axis)
{
    float rot = 0f;
    while (rot < amount)
    {
        yield return null;
        float delta = Mathf.Min(speed * Time.deltaTime, amount - rot);
        transform.RotateAround(target.transform.position, axis, delta);
        rot += delta;
    }

    rotatingCoro = null;
}

// ...

if (rotatingCoro != null)
{
    rotatingCoro = StartCoroutine(DoRotation(50f, 90f, Vector3.back));
}

// ...

// call to stop rotating immediately
void StopRotating()
{
    if (rotatingCoro != null) 
    {
        StopCoroutine(rotatingCoro);
        rotatingCoro = null;
    }
}

Create a coroutine that rotates based on deltaTime and keeps track of how far it has rotated, stopping once it hits 90. Use Mathf.Min to be sure not to rotate past 90.

private isRotating = false;

// speed & amount must be positive, change axis to control direction
IEnumerator DoRotation(float speed, float amount, Vector3 axis)
{
    isRotating = true;
    float rot = 0f;
    while (rot < amount)
    {
        yield return null;
        float delta = Mathf.Min(speed * Time.deltaTime, amount - rot);
        transform.RotateAround(target.transform.position, axis, delta);
        rot += delta;
    }
    isRotating = false;
}

Then when you want to start rotating, start the coroutine if it isnt already rotating:

if (!isRotating)
{
    StartCoroutine(DoRotation(50f, 90f, Vector3.back));
}

It's slightly better form to save the Coroutine itself instead of using a flag. This lets you do stuff like stop the rotation at any point.

private Coroutine rotatingCoro;

// speed & amount must be positive, change axis to control direction
IEnumerator DoRotation(float speed, float amount, Vector3 axis)
{
    float rot = 0f;
    while (rot < amount)
    {
        yield return null;
        float delta = Mathf.Min(speed * Time.deltaTime, amount - rot);
        transform.RotateAround(target.transform.position, axis, delta);
        rot += delta;
    }

    rotatingCoro = null;
}

// ...

if (rotatingCoro != null)
{
    rotatingCoro = StartCoroutine(DoRotation(50f, 90f, Vector3.back));
}

// ...

// call to stop rotating immediately
void StopRotating()
{
    if (rotatingCoro != null) 
    {
        StopCoroutine(rotatingCoro);
        rotatingCoro = null;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文