【问题标题】:How to have consistent rotation speed with transform.Rotate in unity?如何与 transform.Rotate 保持一致的旋转速度?
【发布时间】:2022-02-24 21:16:41
【问题描述】:

我正在尝试创建一个“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 是实现这一目标的最佳方法吗?

【问题讨论】:

  • 您好,为什么您没有使用 RotateArround ? :docs.unity3d.com/ScriptReference/Transform.RotateAround.html
  • @Boaum 嘿,它的工作速度不慢!谢谢!你知道为什么 RotateAround() 不会在 180 度左右减速,而 Rotate() 会吗?
  • @Bouam 我在上面回复时似乎有错字。

标签: c# unity3d rotation quaternions linear-interpolation


【解决方案1】:

最好使用RotateArround,看看文档:docs.unity3d.com/ScriptReference/Transform.RotateAround.html

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-06
    相关资源
    最近更新 更多