You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Unity技术求助:WASD控制玩家移动始终相对世界而非自身

解决WASD移动沿世界坐标系而非局部坐标系的问题

核心修复代码

替换你当前的移动逻辑,使用基于玩家局部坐标系的方向计算:

using UnityEngine;

public class PlayerMovement : MonoBehaviour
{
    public float moveSpeed = 5f;

    void Update()
    {
        float horizontal = Input.GetAxis("Horizontal");
        float vertical = Input.GetAxis("Vertical");

        // 用玩家自身的局部轴方向计算移动向量
        Vector3 moveDir = transform.right * horizontal + transform.forward * vertical;
        // 锁定Y轴,避免上下位移
        moveDir.y = 0f;
        // 归一化防止斜向移动速度翻倍
        if (moveDir.magnitude > 1f) moveDir.Normalize();

        // 应用移动,默认使用局部空间(Space.Self)
        transform.Translate(moveDir * moveSpeed * Time.deltaTime);
    }
}

关键排查点

  • 方向向量错误:不要用Vector3.right/Vector3.forward(世界轴方向),必须用transform.right/transform.forward(玩家局部轴方向)。
  • Translate空间参数:如果显式指定了Space.World,会强制沿世界轴移动,改成Space.Self或者省略该参数(默认就是局部空间)。
  • Player旋转同步:如果你的相机环视只旋转了相机子对象,没有同步旋转Player本体,那Player的局部轴方向不会变化,移动自然还是世界轴方向——要确保环视时是旋转Player对象,让transform.forward随视角转向更新。
  • 脚本冲突:检查是否有其他脚本修改了Player的位置、旋转,或者覆盖了移动逻辑。
  • 物理系统适配:如果用Rigidbody,不要直接改transform.position,改用Rigidbody.MovePosition;同时确保没开启freezeRotation(除非你手动控制旋转)。

特殊场景适配(仅旋转相机不旋转Player)

如果你的设计是只旋转相机子对象,不旋转Player本体,那需要基于相机的水平方向计算移动:

using UnityEngine;

public class PlayerMovement : MonoBehaviour
{
    public float moveSpeed = 5f;

    void Update()
    {
        float horizontal = Input.GetAxis("Horizontal");
        float vertical = Input.GetAxis("Vertical");

        // 获取相机的水平方向(忽略Y轴)
        Vector3 camForward = Camera.main.transform.forward;
        camForward.y = 0f;
        camForward.Normalize();
        Vector3 camRight = Camera.main.transform.right;
        camRight.y = 0f;
        camRight.Normalize();

        // 基于相机方向计算移动向量
        Vector3 moveDir = camRight * horizontal + camForward * vertical;
        if (moveDir.magnitude > 1f) moveDir.Normalize();

        transform.Translate(moveDir * moveSpeed * Time.deltaTime);
    }
}

内容的提问来源于stack exchange,提问作者The_CATCODE

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.26 11:00:13