【问题标题】:Unity / Mirror - Is there a way to keep my camera independent from the player object's prefab after start?Unity / Mirror - 有没有办法让我的相机在开始后独立于玩家对象的预制件?
【发布时间】:2022-08-07 17:21:40
【问题描述】:

我目前正在尝试使用镜像创建多人游戏! 到目前为止,我已经成功地创建了一个大厅,并使用我从 YouTube 上的 Sebastian Graves 那里学到的玩家运动脚本建立了一个简单的角色模型(你可能知道他是黑暗之魂 III 教程的人)

这个玩家移动脚本使用了统一的包\'Input System\',并且还依赖于使用相机的.forward 和.right 方向来确定玩家移动和旋转的位置,而不是使用刚体上的力。这意味着您实际上需要在场景中释放相机并且不受玩家影响。

这是用于旋转我的角色的 HandleRotation() 函数(不是相机的旋转函数):

private void HandleRotation()
    {
        // target direction is the way we want our player to rotate and move // setting it to 0
        Vector3 targetDirection = Vector3.zero;
        
        targetDirection = cameraManager.cameraTransform.forward * inputHandler.verticalInput;
        targetDirection += cameraManager.cameraTransform.right * inputHandler.horizontalInput;
        targetDirection.Normalize();
        targetDirection.y = 0;
        
        if (targetDirection == Vector3.zero)
        {
            // keep our rotation facing the way we stopped
            targetDirection = transform.forward;
        }
        
        // Quaternion\'s are used to calculate rotations
        // Look towards our target direction
        Quaternion targetRotation = Quaternion.LookRotation(targetDirection);
        // Slerp = rotation between current rotation and target rotation by the speed you want to rotate * constant time regardless of framerates
        Quaternion playerRotation = Quaternion.Slerp(transform.rotation, targetRotation, rotationSpeed * Time.deltaTime);

        transform.rotation = playerRotation;
    }

值得一提的是,我没有使用 Cinemachine,但我对学习 Cinemachine 持开放态度,因为它可能对未来有益。

但是,从我学到并设法找到的有关镜像的信息中,您必须在玩家对象的预制件下设置主摄像机,这样当多人加载时,就会创建多个摄像机。这必须发生在 Start() 函数或类似 OnStartLocalPlayer() 的函数上。

public override void OnStartLocalPlayer()
    {
        if (mainCam != null)
        {
            // configure and make camera a child of player
            mainCam.orthographic = false;
            mainCam.transform.SetParent(cameraPivotTransform);
            mainCam.transform.localPosition = new Vector3(0f, 0f, -3f);
            mainCam.transform.localEulerAngles = new Vector3(0f, 0f, 0f);

            cameraTransform = GetComponentInChildren<Camera>().transform;
            defaultPosition = cameraTransform.localPosition.z;
        }
    }

但是当然,这意味着相机不再独立于玩家,因此最终发生的事情是当玩家旋转时相机也会旋转。 例如。如果我在游戏中并且看向我的玩家模型的右侧,然后按“w”键朝摄像机所面对的方向走,我的摄像机将旋转,而玩家在我的玩家旋转时保持相同的旋转为了尝试沿着相机所面对的方向行走。

我的问题是:有没有办法使用镜像创建系统没有需要在开始时将相机作为播放器对象预制件的父级,并且 INSTEAD 通过使其在场景中保持独立来工作?

(我知道在刚体上使用力是创建玩家运动脚本的另一种方法,但如果可能的话我想避免这种情况)

如果有人可以提供帮助,将不胜感激,谢谢! =]

    标签: unity3d unity3d-mirror cinemachine


    【解决方案1】:

    由于您的相机被连接到播放器的限制,您将不得不调整相机逻辑。您可以做的是为相机实例化一个代理变换,并将属性变换到您的相机的副本,全局,每一帧。这父约束组件可能会为您执行此操作。然后在该代理上进行所有转换。

    带有实例化的相机脚本:

    Transform proxy;
    
    void Start(){
      proxy = (new GameObject()).transform;
    }
    
    void Update(){
      //transformations on proxy
      transform.position = proxy.position;
      transform.rotation = proxy.rotation;
    }
    

    【讨论】:

    • 非常感谢您的回答!您是否愿意为此提供一些示例语法,因为我比较新并且没有使用过 .Instantiate();前?我将研究父约束组件。
    猜你喜欢
    • 1970-01-01
    • 2022-08-07
    • 2015-11-20
    • 2018-04-08
    • 1970-01-01
    • 2021-04-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多