我要准备一个使用 WASD 围绕播放器旋转的RuneScape风格的相机。水平旋转可以正常工作,但是当我将两者混合使用时(如向上或向下倾斜),摄像头会非常笨拙地绕着播放器旋转,我猜摄像头可能会倒转或会出现一些云台。
这是我的代码:
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);
}
任何帮助将不胜感激!
答案 0 :(得分:1)
在我看来,您正在使用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);
}