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

Unity俯视角第三人称控制器:相机相对移动与角色独立旋转问题

俯视角相机相对移动解决方案(适配角色旋转独立)

问题出在你直接用了相机原始的forward和right向量——俯视角相机是倾斜的,这两个向量自带Y轴分量,自然会让角色往上下动。解决思路很简单:把相机的方向向量投影到水平面上(Y=0),再用来计算移动方向。

保留你原代码的前3行逻辑,只需要修改finalMoveDirection到Controller.Move之间的部分,完整代码如下:

Vector2 targetHorizontalMoveDirection = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"));
Vector2 horizontalMoveDirection = Vector2.MoveTowards(horizontalMoveDirection, targetHorizontalMoveDirection.normalized * maxSpeed, movementAcceleration*Time.deltaTime);
Vector3 finalMoveDirection = new Vector3(horizontalMoveDirection.x, 0, horizontalMoveDirection.y);

if(finalMoveDirection != Vector3.zero)
{
    // 处理相机方向,剔除Y轴分量,得到水平面上的相机朝向
    Vector3 camForward = Vector3.Scale(cam.transform.forward, new Vector3(1, 0, 1)).normalized;
    Vector3 camRight = Vector3.Scale(cam.transform.right, new Vector3(1, 0, 1)).normalized;

    // 用处理后的相机方向构建最终移动向量
    Vector3 cameraRelativeMove = finalMoveDirection.x * camRight + finalMoveDirection.z * camForward;
    
    playerCharacterController.Move(cameraRelativeMove * Time.deltaTime);
}

关键说明:

  • Vector3.Scale用来把相机方向的Y轴分量直接置0,确保只保留水平面上的前后左右
  • 归一化是为了避免相机倾斜时,向量长度被压缩导致移动速度不一致
  • 这样处理后,不管角色朝向哪个方向,W/S都会对应相机视角的前后,A/D对应相机视角的左右,完全符合你要的规则

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.17 00:12:01