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

Unity相机可转动但玩家无法同步旋转的技术问题

问题分析与解决方法

你的推测是正确的——子对象orientationpos的旋转不会自动同步到父对象Player,Unity中父子对象的变换相互独立,子对象的旋转修改仅作用于自身及它的子对象,父对象不会被动继承子对象的旋转值。

解决方法:同步子对象Y轴旋转到父对象

在更新Orientation旋转的逻辑后,直接修改父对象Player的旋转值,仅同步Y轴(X轴旋转为相机抬头低头动作,无需影响Player朝向)。修改Update方法如下:

private void Update()
{
    if (Orientation == null)
    {
        Orientation = GameObject.FindWithTag("OrientLink").transform;
    }
    else
    {
        float mouseX = Input.GetAxisRaw("Mouse X") * Time.deltaTime * sensX;
        float mouseY = Input.GetAxisRaw("Mouse Y") * Time.deltaTime * sensY;

        yRotation += mouseX;
        xRotation -= mouseY;

        xRotation = Mathf.Clamp(xRotation, -90, 90);

        transform.rotation = Quaternion.Euler(xRotation, yRotation, 0);
        Orientation.rotation = Quaternion.Euler(0, yRotation, 0);

        // 同步子对象Y轴旋转到父对象Player
        if (pv.IsMine) // 仅本地玩家修改自身Player旋转
        {
            Transform playerTransform = Orientation.parent;
            if (playerTransform != null)
            {
                // 保留Player原有X、Z轴旋转,替换Y轴为朝向值
                playerTransform.rotation = Quaternion.Euler(
                    playerTransform.eulerAngles.x,
                    yRotation,
                    playerTransform.eulerAngles.z
                );
            }
        }
    }
}

额外优化建议

  • 避免重复查找对象:Start和Update中重复调用GameObject.FindWithTag会降低性能,建议在本地Player初始化时,直接通过Player脚本获取子对象orientationpos并传递给Camera脚本,而非依赖标签查找。
  • 网络同步配置:若要让其他玩家看到本地Player的朝向,需在Player对象的PhotonView组件中,将Player的Transform添加到「Observed Components」列表,Photon会自动同步Player的旋转到其他客户端。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.06 04:46:13