如何与transform.Rotate保持一致的旋转速度?

发布于 2025-01-09 14:02:20 字数 2637 浏览 4 评论 0原文

我正在尝试创建一个“fnaf-esc”控件,其中播放器根据鼠标相对于屏幕宽度的位置绕 Y 轴旋转。我还没有费心去夹紧旋转,因为我面临着达到 180 度标记时旋转速度减慢的问题。

[SerializeField]
private GameObject Player;

// Mouse/Player Rotation Variables
float mouseX;
float mouseY;
Vector3 mousePos = new Vector3(0f, 0f, 0f);

[SerializeField]
private Transform playerBody;

[SerializeField]
private Camera cam;

void Update()
{
        mousePos = Input.mousePosition;

        if (mousePos.x <= (Screen.width * .4))
        {
            if (mousePos.x >= (Screen.width * .33))
            {
                Debug.Log("Turn left slowest");
                Player.transform.Rotate(0f, -0.05f, 0f);

            }
            else
            {
                if (mousePos.x < (Screen.width * .33) && mousePos.x > (Screen.width *.25))
                {
                    Debug.Log("Turn left slower");
                    Player.transform.Rotate(0f, -0.1f, 0f);
                }
                else if (mousePos.x <= (Screen.width * .25) && mousePos.x > (Screen.width * .2))
                {
                    Debug.Log("Turn left");
                    Player.transform.Rotate(0f, -0.15f, 0f);
                }
                else if (mousePos.x <= (Screen.width * .2))
                {
                    Debug.Log("Turn left");
                    Player.transform.Rotate(0f, -0.2f, 0f);
                }
            }
        }

        if (mousePos.x >= (Screen.width * .6))
        {
            if (mousePos.x <= (Screen.width * .66))
            {
                Debug.Log("Turn right slowest");
                Player.transform.Rotate(0f, 0.05f, 0f);
            }
            else
            {
                if (mousePos.x > (Screen.width * .66) && mousePos.x < (Screen.width * .75))
                {
                    Debug.Log("Turn right slower");
                    Player.transform.Rotate(0f, 0.1f, 0f);
                }
                else if (mousePos.x >= (Screen.width * .75) && mousePos.x < (Screen.width * .8))
                {
                    Debug.Log("Turn right");
                    Player.transform.Rotate(0f, 0.15f, 0f);
                }
                else if (mousePos.x >= (Screen.width * .8))
                {
                    Debug.Log("Turn right");
                    Player.transform.Rotate(0f, 0.2f, 0f);
                }
            }
        }
    }

测试时,

我只是将鼠标保持在同一位置,旋转仍然在 180 度左右减慢。

我尝试寻找解决方案,发现“线性插值”是造成这种情况的原因,尽管我找不到任何解决方案一致的转弯速度。

  • 如何才能实现 180 度附近不减慢的转弯速度?
  • Transform.Rotate 是实现此目的的最佳方法吗?

I am trying to create a "fnaf-esc" control where the player rotates around the Y axis based on the mouse's position relative to screen width. I haven't bothered with clamping the rotation yet, because I am facing an issue with the rotation speed slowing down when reaching the 180 degrees mark.

[SerializeField]
private GameObject Player;

// Mouse/Player Rotation Variables
float mouseX;
float mouseY;
Vector3 mousePos = new Vector3(0f, 0f, 0f);

[SerializeField]
private Transform playerBody;

[SerializeField]
private Camera cam;

void Update()
{
        mousePos = Input.mousePosition;

        if (mousePos.x <= (Screen.width * .4))
        {
            if (mousePos.x >= (Screen.width * .33))
            {
                Debug.Log("Turn left slowest");
                Player.transform.Rotate(0f, -0.05f, 0f);

            }
            else
            {
                if (mousePos.x < (Screen.width * .33) && mousePos.x > (Screen.width *.25))
                {
                    Debug.Log("Turn left slower");
                    Player.transform.Rotate(0f, -0.1f, 0f);
                }
                else if (mousePos.x <= (Screen.width * .25) && mousePos.x > (Screen.width * .2))
                {
                    Debug.Log("Turn left");
                    Player.transform.Rotate(0f, -0.15f, 0f);
                }
                else if (mousePos.x <= (Screen.width * .2))
                {
                    Debug.Log("Turn left");
                    Player.transform.Rotate(0f, -0.2f, 0f);
                }
            }
        }

        if (mousePos.x >= (Screen.width * .6))
        {
            if (mousePos.x <= (Screen.width * .66))
            {
                Debug.Log("Turn right slowest");
                Player.transform.Rotate(0f, 0.05f, 0f);
            }
            else
            {
                if (mousePos.x > (Screen.width * .66) && mousePos.x < (Screen.width * .75))
                {
                    Debug.Log("Turn right slower");
                    Player.transform.Rotate(0f, 0.1f, 0f);
                }
                else if (mousePos.x >= (Screen.width * .75) && mousePos.x < (Screen.width * .8))
                {
                    Debug.Log("Turn right");
                    Player.transform.Rotate(0f, 0.15f, 0f);
                }
                else if (mousePos.x >= (Screen.width * .8))
                {
                    Debug.Log("Turn right");
                    Player.transform.Rotate(0f, 0.2f, 0f);
                }
            }
        }
    }

}

When testing, I just kept my mouse in the same position and the rotation was still slowing around 180.

I tried searching for solutions and found that "linear interpolation" is the reason for this, though I can't find any solutions for a consistent turn speed.

  • How can I achieve a turn speed that doesn't slow around the 180 degrees mark?
  • Is transform.Rotate the best method to achieve this?

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

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

发布评论

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

评论(1

走野 2025-01-16 14:02:20

It's better to use RotateAround , take a look at the documentation: https://docs.unity3d.com/ScriptReference/Transform.RotateAround.html

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