unity中如何让transform.Rotate的转速保持一致?
How to have consistent rotation speed with transform.Rotate in unity?
我正在尝试创建一个“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
我正在尝试创建一个“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