我正在嘗試創建一個“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 是實作這一目標的最佳方法嗎?
uj5u.com熱心網友回復:
最好使用 RotateArround ,看看檔案:docs.unity3d.com/ScriptReference/Transform.RotateAround.html
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/432318.html
上一篇:嘗試創建嵌套的ScriptableObject:“AddAssetToSameFile失敗,因為其他資產不是持久的”
