unity3d Unity Netcode(MLAPI)主机正在移动且客户端正在连接,但客户端未移动

k97glaaz  于 2022-11-16  发布在  其他
关注(0)|答案(3)|浏览(239)

最近开始使用Unity Netcode(MLAPI)来尝试制作多人竞速游戏,但是我遇到了一些麻烦。这是我第一次制作多人游戏,所以我对这个东西还很陌生。
正在发生的情况:客户端连接到主机完全正常,车轮移动就像汽车要移动一样,但客户端根本无法移动。主机可以移动得很好,并能够与客户端碰撞,客户端可以查看所有内容,但客户端就是不动。我假设这与NetworkTransforms有关,但我真的不确定在这一点上。
下面是我为车辆附上的移动脚本:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Unity.Netcode;

public class VehicleMovement : NetworkBehaviour
{
    private const string HORIZONTAL = "Horizontal";
    private const string VERTICAL = "Vertical";

    private float horizontalInput;
    private float verticalInput;
    private float currentSteerAngle;
    private bool isBraking;
    private float currentBrakeForce;

    [SerializeField] private float motorForce;
    [SerializeField] private float brakeForce;
    [SerializeField] private float maxSteeringAngle;

    [SerializeField] private WheelCollider frontLeftWheelCollider;
    [SerializeField] private WheelCollider frontRightWheelCollider;
    [SerializeField] private WheelCollider rearLeftWheelCollider;
    [SerializeField] private WheelCollider rearRightWheelCollider;

    [SerializeField] private Transform frontLeftWheelTransform;
    [SerializeField] private Transform frontRightWheelTransform;
    [SerializeField] private Transform rearLeftWheelTransform;
    [SerializeField] private Transform rearRightWheelTransform;
    private void FixedUpdate()
    {
        if (IsLocalPlayer)
        {
            GetInput();
            HandleMotor();
            HandleSteering();
            UpdateWheels();
        }
    }

    private void HandleMotor()
    {
        frontLeftWheelCollider.motorTorque = verticalInput * motorForce;
        frontRightWheelCollider.motorTorque = verticalInput * motorForce;
        currentBrakeForce = isBraking ? brakeForce : 0f;
        ApplyBraking();
    }

    private void ApplyBraking()
    {
        frontRightWheelCollider.brakeTorque = currentBrakeForce;
        frontLeftWheelCollider.brakeTorque = currentBrakeForce;
        rearRightWheelCollider.brakeTorque = currentBrakeForce;
        rearLeftWheelCollider.brakeTorque = currentBrakeForce;
    }

    private void HandleSteering()
    {
        currentSteerAngle = maxSteeringAngle * horizontalInput;
        frontLeftWheelCollider.steerAngle = currentSteerAngle;
        frontRightWheelCollider.steerAngle = currentSteerAngle;
    }
    private void GetInput()
    {
        horizontalInput = Input.GetAxis(HORIZONTAL);
        verticalInput = Input.GetAxis(VERTICAL);
        isBraking = Input.GetKey(KeyCode.Space);
    }

    private void UpdateWheels()
    {
        UpdateSingleWheel(frontLeftWheelCollider, frontLeftWheelTransform);
        UpdateSingleWheel(frontRightWheelCollider, frontRightWheelTransform);
        UpdateSingleWheel(rearLeftWheelCollider, rearLeftWheelTransform);
        UpdateSingleWheel(rearRightWheelCollider, rearRightWheelTransform);

    }

    private void UpdateSingleWheel(WheelCollider wheelCollider, Transform wheelTransform)
    {
        Vector3 pos;
        Quaternion rot;
        wheelCollider.GetWorldPose(out pos, out rot);
        wheelTransform.rotation = rot;
        wheelTransform.position = pos;
    }
}

我的预制件由变换、刚体、[上面的脚本]、网络对象、网络变换和网络刚体组成。
Here是一个未列出的YouTube视频,希望可以直观地显示该问题。
任何和所有的帮助将不胜感激!谢谢!

sirbozc5

sirbozc51#

原来在包管理器下,有一个可选的导入在“Netcode for GameObjects”中隐藏在“Samples”下,称为“ClientNetwork Transform”。
这就彻底解决了问题。

piah890a

piah890a2#

默认情况下,NetworkTransform是服务器授权的,这意味着只有服务器可以更新NetworkTransform对象。
我们可以通过创建一个客户端网络转换对象来解决这个问题,该对象将权限更改为客户端授权:

using UnityEngine;
using Unity.Netcode.Components;

[DisallowMultipleComponent]
public class ClientNetworkTransform : NetworkTransform
{
    protected override bool OnIsServerAuthoritative()
    {
        return false;
    }
}
sg3maiej

sg3maiej3#

目前,由于此过程不再准确,因此可以在文档中找到解决方案
客户端网络转换编号
NetworkTransform总是将位置从服务器同步到客户端,并且不允许在客户端上更改位置。GameObjects的Netcode附带了一个包含ClientNetworkTransform的示例。此转换将所有者客户端的位置同步到服务器和所有其他客户端,允许客户端进行权威游戏。
ClientNetworkTransform位于Multiplayer Samples Utilities软件包中。您可以通过Unity编辑器中的软件包管理器窗口添加此软件包,方法是选择从Git URL添加并添加以下URL:

https://github.com/Unity-Technologies/com.unity.multiplayer.samples.coop.git?path=/Packages/com.unity.multiplayer.samples.coop#main

或者您可以直接将此行添加到manifest.json

file:= "com.unity.multiplayer.samples.coop": "https://github.com/Unity-Technologies/com.unity.multiplayer.samples.coop.git?path=/Packages/com.unity.multiplayer.samples.coop#main"

我永远无法得到:

protected override bool OnIsServerAuthoritative()

作为解决方案运行,但发现closed issue on git
是又坏了还是我用错了?

相关问题