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

Unity3D角色移动方向与朝向不符问题求助(附Mouse Look与Player Movement脚本)

问题分析与解决方法

Hey there! Let's break down why your player's movement isn't aligning with their facing direction, and fix it step by step.

核心问题原因

Your current movement script uses world-space coordinates (fixed X/Z axes) to calculate movement. But after your character turns via the Mouse Look script, their local coordinate system (their own forward/right directions) is no longer aligned with the world's. This causes WASD inputs to move the player in world directions instead of relative to where they're facing.

Fixed Player Movement Script

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Player : MonoBehaviour {
    Vector3 moveVector = Vector3.zero;
    CharacterController characterController;
    public float MoveSpeed;
    public float JumpSpeed;
    public float Gravity;

    void Start() {
        characterController = GetComponent<CharacterController>();
    }

    void Update() {
        // Get raw horizontal/vertical input values
        float horizontalInput = Input.GetAxis("Horizontal");
        float verticalInput = Input.GetAxis("Vertical");

        // Create movement direction relative to the player's current facing
        Vector3 moveDirection = transform.right * horizontalInput + transform.forward * verticalInput;
        // Normalize to prevent faster diagonal movement
        if (moveDirection.magnitude > 1f)
            moveDirection.Normalize();

        // Apply movement speed to the direction
        moveVector.x = moveDirection.x * MoveSpeed;
        moveVector.z = moveDirection.z * MoveSpeed;

        // Keep existing jump/gravity logic
        if (characterController.isGrounded && Input.GetButton("Jump")) {
            moveVector.y = JumpSpeed;
        }

        moveVector.y -= Gravity * Time.deltaTime;
        characterController.Move(moveVector * Time.deltaTime);
    }
}

Detailed Explanation

  1. Local Space Movement

    • transform.right: This is the player's own right direction (local X-axis), which matches A/D input.
    • transform.forward: This is the player's own forward direction (local Z-axis), which matches W/S input.
    • By multiplying inputs with these local axes, we ensure movement always follows where the player is currently looking, not fixed world directions.
  2. Normalization
    When you press both horizontal and vertical keys (e.g., moving diagonally), the combined vector length would be greater than 1, making the player move faster. Normalize() ensures the vector stays at length 1, so movement speed is consistent in all directions.

  3. Optional Mouse Look Improvement
    Your current Mouse Look script doesn't limit vertical rotation, which lets the player flip upside down. Add this clamp to fix it:

    void Update() {
        Vector2 mC = new Vector2(Input.GetAxisRaw("Mouse X"), Input.GetAxisRaw("Mouse Y"));
        mD += mC;
        // Lock vertical rotation between -90° (looking down) and 90° (looking up)
        mD.y = Mathf.Clamp(mD.y, -90f, 90f);
        
        this.transform.localRotation = Quaternion.AngleAxis(-mD.y, Vector3.right);
        myBody.localRotation = Quaternion.AngleAxis(mD.x, Vector3.up);
    }
    

After these changes, your player's movement will perfectly match their facing direction!

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.29 11:43:11