我只能在 x 軸上旋轉它,但我也想通過滑鼠的移動在 z 軸上進行旋轉。對不起,錯誤是谷歌翻譯。
[SerializeField] private float camRotationAmount = 0.2f;
public Quaternion newRotation;
newRotation *= Quaternion.Euler(Vector3.up * Input.GetAxis("Mouse X"));
transform.rotation = Quaternion.Lerp(transform.rotation, newRotation, Time.deltaTime * camSmoothness);
我想在不傾斜相機的情況下為 Vector3.left 添加 Input.GetAxis ("Mouse Y")。這是一個rts游戲。謝謝
uj5u.com熱心網友回復:
通常,您需要按以下方式分離Y 和 X 旋轉:
- 在全域空間中繞 Y 旋轉
- 在區域空間中圍繞 X 旋轉。
無論您是純粹通過邏輯還是使用父子層次結構來執行此操作,都取決于您……在我看來,第二個更容易理解。
使用層次結構
在那里你只會有一個層次結構,例如
CameraAnchor
|--Camera
并有一個CameraAnchor類似的腳本,例如
public class CameraAnchorController : MonoBehaviour
{
public float cameraSmoothness = 5f;
private Quaternion targetGlobalRotation;
private Quaternion targetLocalRotation;
private Transform child;
private void Start()
{
child = transform.GetChild(0);
targetGlobalRotation = transform.rotation;
targetLocalRotation = transform.GetChild(0).localRotation;
}
private void Update()
{
targetGlobalRotation *= Quaternion.Euler(Vector3.up * Input.GetAxis("Mouse X"));
targetLocalRotation *= Quaternion.Euler(Vector3.right * -Input.GetAxis("Mouse Y"));
var lerpFactor = cameraSmoothness * Time.deltaTime;
transform.rotation = Quaternion.Lerp(transform.rotation, targetGlobalRotation, lerpFactor);
child.localRotation = Quaternion.Lerp(child.localRotation, targetLocalRotation, lerpFactor);
}
}
使用四元數數學
如前所述,你可以在一個物件中做同樣的事情,但我仍然會保持兩個旋轉分開:
public class CameraController : MonoBehaviour
{
public float cameraSmoothness = 5f;
private Quaternion targetGlobalRotation;
private Quaternion targetLocalRotation = Quaternion.Idendity;
private void Start()
{
targetGlobalRotation = transform.rotation;
}
private void Update()
{
targetGlobalRotation *= Quaternion.Euler(Vector3.up * Input.GetAxis("Mouse X"));
targetLocalRotation *= Quaternion.Euler(Vector3.right * -Input.GetAxis("Mouse Y"));
transform.rotation = Quaternion.Lerp(transform.rotation, targetGlobalRotation * targetLocalRotation, cameraSmoothness * Time.deltaTime);
}
}
現在為什么當我們現在還在使用時這會起作用Vector3.right?
通過這樣做,targetGlobalRotation * targetLocalRotation我們首先圍繞全域 Y 軸旋轉,然后根據已經應用的旋轉在 X 軸上應用旋轉 -> 現在是一個額外的區域旋轉!
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/324765.html
