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
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.
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.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_

