[英]Camera behaviour weird when rotating around player
I am going for a RuneScape-style camera that rotates around the player using WASD .我想要一个 RuneScape 风格的相机,它使用WASD围绕玩家旋转。 Rotating horizontally works fine but when I mix the two (as in pitching up or down) the camera rotates around the player really awkwardly, the camera might invert or will sort of gimbal I guess.
水平旋转效果很好,但是当我将两者混合在一起时(例如向上或向下倾斜),相机会非常笨拙地围绕玩家旋转,我猜相机可能会倒转或有点像万向节。
Here's my code:这是我的代码:
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);
}
Any help would be gladly appreciated!任何帮助将不胜感激!
It looks to me that you are using currentPitch, but rotating it around the forward axis?在我看来,您正在使用 currentPitch,但是围绕前向轴旋转它? Which would create roll on the world foward axis?
哪个会在世界前轴上产生滚动?
If your up vector is always world up, then the yaw you have will work.如果你的向上向量总是向上,那么你的偏航将起作用。 But what you want to do is recalculate the right vector from your current location to your target after you apply the yaw.
但是您要做的是在应用偏航后重新计算从当前位置到目标的正确向量。
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.