You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

移动相机保持“水平”,忽略X、Z轴旋转的实现问题

Fixing Horizontal Camera Movement (Ignoring Pitch Rotation)

Hey there! I totally get what you're dealing with—when your camera is pitched up or down, hitting W makes it zoom toward/away from the target instead of moving horizontally, right? That's because the camera's default transform.forward includes the vertical (X-axis rotation) component. Let's fix this so your movement stays flat, just like walking forward while looking down at your phone.

The Core Issue

Your camera's forward vector points exactly where the lens is looking—so if you're gazing down, that vector has a downward tilt. When you move along that vector, you're moving toward the ground (or up if looking up) instead of straight ahead horizontally. We need to strip out that vertical tilt to get a pure horizontal movement direction.

The Solution

We'll create a "horizontal-only" rotation for the camera, ignoring its X-axis (pitch) and Z-axis (roll) rotation. Then we'll calculate movement vectors based on this flattened rotation.

Here's how to implement this in Unity (assuming you're using C#—adjust for your engine if needed):

// In your camera movement script
public float moveSpeed = 5f;

void Update()
{
    // 1. Get the camera's current rotation, but ONLY keep the Y-axis (horizontal turn)
    Quaternion horizontalRotation = Quaternion.Euler(0, transform.eulerAngles.y, 0);

    // 2. Calculate horizontal forward/right vectors (no vertical component)
    Vector3 horizontalForward = horizontalRotation * Vector3.forward;
    Vector3 horizontalRight = horizontalRotation * Vector3.right;

    // 3. Normalize to prevent faster diagonal movement
    horizontalForward.Normalize();
    horizontalRight.Normalize();

    // 4. Build your movement direction using WSAD/arrow keys
    float verticalInput = Input.GetAxis("Vertical"); // W/S or Up/Down
    float horizontalInput = Input.GetAxis("Horizontal"); // A/D or Left/Right

    Vector3 moveDirection = horizontalForward * verticalInput + horizontalRight * horizontalInput;
    moveDirection.y = 0; // Lock Y-axis movement (remove this if you want vertical movement too)

    // 5. Apply the movement to the camera
    transform.Translate(moveDirection * moveSpeed * Time.deltaTime, Space.World);
}

How This Works

  • horizontalRotation takes your camera's current horizontal facing (Y-axis rotation) and resets pitch/roll to 0. This gives us a "flat" version of where the camera is turned.
  • horizontalForward and horizontalRight are calculated using this flat rotation, so they always stay parallel to the ground—no matter how much you tilt the camera up or down.
  • By setting moveDirection.y = 0, we ensure the camera never moves up/down from this input (you can remove this line if you want to keep vertical movement from other sources, like jumping).

Quick Notes

  • If your camera is a child of another object (like a player controller), keep using transform instead of Camera.main.transform if the script is attached directly to the camera.
  • If you already have code for looking at selected characters, this movement logic will play nicely with it—your camera can still pitch to focus on the character, but WSAD will always move you horizontally.

This should make your camera move exactly like you want: looking up/down doesn't affect whether W moves you forward horizontally, just like how you walk straight even when staring at your phone.

内容的提问来源于stack exchange,提问作者Marek M.

火山引擎 最新活动