平滑的角色旋转

发布于 2024-12-14 02:39:18 字数 642 浏览 0 评论 0原文

这是我在四个基本方向上移动的代码:

    if (Input.GetKeyDown(KeyCode.RightArrow)){
        transform.forward = new Vector3(0f, 0f, 1f);
        }
    else if (Input.GetKeyDown(KeyCode.LeftArrow)){
        transform.forward = new Vector3(0f, 0f, -1f);
        }
    else if (Input.GetKeyDown(KeyCode.DownArrow)){
        transform.forward = new Vector3(1f, 0f, 0f);
        }
    else if (Input.GetKeyDown(KeyCode.UpArrow)){
        transform.forward = new Vector3(-1f, 0f, 0f);
        }

我的角色立即在四个行进方向上旋转 我遇到的问题是试图弄清楚如何在方向之间平滑。我想说的是,如果角色指向北,并且您按下按钮向西走,我希望看到角色向西方向平滑地旋转一定角度,而不是立即转向西。

我搜索并尝试了教程,但没有一个起作用。有什么解决办法吗?

Here is my code for moving in the four cardinal directions:

    if (Input.GetKeyDown(KeyCode.RightArrow)){
        transform.forward = new Vector3(0f, 0f, 1f);
        }
    else if (Input.GetKeyDown(KeyCode.LeftArrow)){
        transform.forward = new Vector3(0f, 0f, -1f);
        }
    else if (Input.GetKeyDown(KeyCode.DownArrow)){
        transform.forward = new Vector3(1f, 0f, 0f);
        }
    else if (Input.GetKeyDown(KeyCode.UpArrow)){
        transform.forward = new Vector3(-1f, 0f, 0f);
        }

My character instantly rotates in the four directions of travel The problem I'm having is trying to figure out how I can smooth out inbetween directions. What I'm trying to say is, if the character is pointing north and you push the button to go west, instead of instantly turning west, I would like to see the character rotate smoothly in degrees to the westward direction.

Ive search and tried tutorials, but none of them worked. Any solution?

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

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

发布评论

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

评论(1

江湖彼岸 2024-12-21 02:39:18

看一下 Slerp 函数。这可能会做你想要的。

http://unity3d.com/support/documentation/ScriptReference/Quaternion.Slerp.html

简单的例子:

    newdir.y=-135; // depends on which axis you want to turn...

transform.rotation = Quaternion.Lerp(transform.rotation, newdir, Time.deltaTime * smoothSpeed);

所以你基本上得到了当前旋转的四元数。将其复制到新的四元数中,并增加或减少一个或多个轴上的旋转。

Take a look at the Slerp function. This will probably do what you want.

http://unity3d.com/support/documentation/ScriptReference/Quaternion.Slerp.html

Simple example:

    newdir.y=-135; // depends on which axis you want to turn...

transform.rotation = Quaternion.Lerp(transform.rotation, newdir, Time.deltaTime * smoothSpeed);

So you basically get the Quaternion of your current rotation. Copy that in a new Quaternion and increase or decrease the the rotation on one or more axis.

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