我正在尝试创建一个“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 度左右减慢。
我尝试寻找解决方案,发现“线性插值”是造成这种情况的原因,尽管我找不到任何一致转弯速度的解决方案。
最好使用 RotateAround ,看一下文档:https://docs.unity3d.com/ScriptReference/Transform.RotateAround.html