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

Unity第三人称控制器角色朝向与移动方向相反问题求助

Unity第三人称控制器方向反转问题排查

问题现象:

  • 角色移动方向与朝向完全相反:左移时面朝右,前进时面朝后
  • 手动旋转角色后会自动弹回原位
  • 修改HandleRotation中的赋值方式后问题仍未解决

以下是相关脚本代码:

PlayerLocomotion 脚本

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

public class PlayerLocomotion : MonoBehaviour
{
    InputManager inputManager;

    Vector3 moveDirection;
    Transform cameraObject;
    Rigidbody playerRigidBody;

    public float movementSpeed = 7;
    public float rotationSpeed = 15;

    private void Awake()
    {
        inputManager = GetComponent<InputManager>();
        playerRigidBody = GetComponent<Rigidbody>();
        cameraObject = Camera.main.transform;
    }
    public void HandleAllMovement()
    {
        HandleMovement();
        HandleRotation();

    }
    private void HandleMovement()
    {
        moveDirection = cameraObject.forward * inputManager.verticalInput;
        moveDirection = moveDirection + cameraObject.right * inputManager.horizontalInput;
        moveDirection.Normalize();
        moveDirection.y = 0; 
        moveDirection = moveDirection * movementSpeed;
        
        Vector3 movementVelocity = moveDirection; 
        playerRigidBody.velocity = movementVelocity;
    }

    private void HandleRotation()
    {
        Vector3 targetDirection = Vector3.zero;

        targetDirection = cameraObject.forward * inputManager.verticalInput;
        targetDirection = targetDirection + cameraObject.right * inputManager.horizontalInput;
        targetDirection.Normalize();
        targetDirection.y = 0;

        Quaternion targetRotation = Quaternion.LookRotation(targetDirection);
        Quaternion playerRotation = Quaternion.Slerp(transform.rotation, targetRotation, rotationSpeed * Time.deltaTime);
        
        transform.rotation = playerRotation;
    }
}

PlayerManager 脚本

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

public class PlayerManager : MonoBehaviour
{
    InputManager inputManager;
    PlayerLocomotion playerLocomotion;
   private void Awake()
   {
    inputManager = GetComponent<InputManager>();
    playerLocomotion = GetComponent<PlayerLocomotion>();


   }
   private void Update()
   {
       inputManager.HandleAllInputs();
   }

   private void FixedUpdate()
   {
       playerLocomotion.HandleAllMovement();
   }
}

InputManager 脚本

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

public class InputManager : MonoBehaviour
{
 PlayerControls playerControls;

 public Vector2 movementInput; 
 public float verticalInput;
 public float horizontalInput;
 

 private void OnEnable()
 {
    if (playerControls == null)
    {
        playerControls = new PlayerControls();

        playerControls.PlayerMovement.Movement.performed += i => movementInput = i.ReadValue<Vector2>();
        
    }
    playerControls.Enable();

 }

 private void OnDisable()
 {
    playerControls.Disable();
 }
 public void HandleAllInputs()
 {
   HandleMovementInput();
   //HandleJumpingInput
   //HandleActionInput
 }
 private void HandleMovementInput()
 {
   verticalInput = movementInput.y;
   horizontalInput = movementInput.x;
 }
}

排查方向与修复方案

  1. 输入轴方向反转
    检查Unity Input System的绑定设置,若输入轴方向与预期相反,直接在代码中对输入值取反:
    修改InputManager的HandleMovementInput方法:

    private void HandleMovementInput()
    {
      verticalInput = -movementInput.y;
      horizontalInput = -movementInput.x;
    }
    
  2. Rigidbody旋转约束错误
    角色旋转后弹回是物理约束导致的:

    • 选中角色对象,打开Inspector面板的Rigidbody组件
    • 取消Constraints中Freeze Rotation的全选状态,仅保留需要冻结的轴(比如保留Y轴防止角色倾斜)
  3. 相机垂直旋转干扰
    若相机有抬头/低头动作,需剔除垂直方向对角色移动的影响:
    修改PlayerLocomotion中HandleMovement和HandleRotation的方向计算逻辑:

    // 在两个方法中替换相机方向获取代码
    Vector3 cameraForward = Vector3.Scale(cameraObject.forward, new Vector3(1, 0, 1)).normalized;
    Vector3 cameraRight = cameraObject.right;
    // 后续用cameraForward替代cameraObject.forward
    

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.12 10:05:22