繁体   English   中英

围绕玩家旋转时相机的行为很奇怪

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM