如何与transform.Rotate保持一致的旋转速度?
我正在尝试创建一个“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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
最好使用 RotateAround ,看看文档: https://docs.unity3d .com/ScriptReference/Transform.RotateAround.html
It's better to use RotateAround , take a look at the documentation: https://docs.unity3d.com/ScriptReference/Transform.RotateAround.html