简体   繁体   English

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

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

相关问题 为什么我的相机在围绕播放器对象旋转时会夹在播放器对象中? - Why does my camera clip into the player object when rotating around it? 即使我在玩家周围旋转,如何让我的玩家面对 cursor? - How to make my player face cursor even when I am rotating around the player? 使用触摸控件围绕中心旋转播放器 - Rotating a player around the centre with touch controls 蹲伏时相机在旋转的播放器上晃动 - Camera shaking on rotating player while crouching in unity 当使用 ik 看目标时,如果目标围绕玩家旋转,而目标来自后面,玩家却没有看它,为什么? - When using ik to look at target if the target is rotating around the player when the target is coming from behind the player is not looking at it why? 奇怪的扫雷炸弹周围的数字行为 - Weird Minesweeper bombs-around numbers behaviour 三角函数 + 角度 - Object 围绕玩家旋转并面向鼠标方向 - Trigonometry + Angles - Object rotating around player and facing mouse direction 为相机设置动画以使播放器转过身,然后继续跟踪播放器 - Animating a camera to turn around the player, then continue tracking player 当玩家转身旋转时,使其跟随玩家 - Make a camera follow the player as the player turns around rotate 使用箭头键在 Unity/C# 中围绕球体旋转相机 - Rotating camera around sphere in Unity/C# using arrow keys
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM