![](/img/trans.png)
[英]Why does my camera clip into the player object when rotating around it?
[英]Camera behaviour weird when rotating around player
我想要一個 RuneScape 風格的相機,它使用WASD圍繞玩家旋轉。 水平旋轉效果很好,但是當我將兩者混合在一起時(例如向上或向下傾斜),相機會非常笨拙地圍繞玩家旋轉,我猜相機可能會倒轉或有點像萬向節。
這是我的代碼:
public float pitch;
public float zoomSpeed = 4f;
public float minZoom = 5f;
public float maxZoom = 15f;
public Transform target;
public Vector3 offset;
public float yawSpeed = 100f;
private float currentZoom = 10f;
private float currentYaw = 0f;
private float currentPitch = 0f;
void Update()
{
currentZoom -= Input.GetAxis("Mouse ScrollWheel") * zoomSpeed;
currentZoom = Mathf.Clamp(currentZoom, minZoom, maxZoom);
currentYaw -= Input.GetAxis("Horizontal") * yawSpeed * Time.deltaTime;
currentPitch -= Input.GetAxis("Vertical") * yawSpeed * Time.deltaTime;
Debug.Log("Yaw: " + currentYaw + " Pitch: " + currentPitch);
}
void LateUpdate()
{
transform.position = target.position - offset * currentZoom;
transform.LookAt(target.position + Vector3.up * pitch);
transform.RotateAround(target.position, Vector3.up, currentYaw);
transform.RotateAround(target.position, Vector3.forward, currentPitch);
}
任何幫助將不勝感激!
在我看來,您正在使用 currentPitch,但是圍繞前向軸旋轉它? 哪個會在世界前軸上產生滾動?
如果你的向上向量總是向上,那么你的偏航將起作用。 但是您要做的是在應用偏航后重新計算從當前位置到目標的正確向量。
void LateUpdate() {
transform.position = target.position - offset * currentZoom;
transform.LookAt(target.position + Vector3.up * pitch);
transform.RotateAround(target.position, Vector3.up, currentYaw);
transform.RotateAround(target.position, Vector3.Cross((target.position - transform.position).normalized, Vector3.up), currentPitch);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.