Unity Netcode(MLAPI)主机移动和客户端连接,但客户端不移动



最近开始使用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;
}
}

我的序言由Transform、Rigidbody、[上面的脚本]、NetworkObject、NetworkTransform和NetworkRigidbody组成。

这是一个未列出的YouTube视频,希望能直观地显示这个问题。

任何帮助都将不胜感激!非常感谢。

在Package Manager下;GameObjects的Netcode";隐藏在";样品";称为";ClientNetwork Transform";。

这完全解决了问题。

NetworkTransform默认情况下是服务器授权的,这意味着只有服务器才能更新NetworkTransform对象。

我们可以通过创建一个客户端网络转换对象来绕过这个问题,该对象将权限更改为客户端权威:

using UnityEngine;
using Unity.Netcode.Components;
[DisallowMultipleComponent]
public class ClientNetworkTransform : NetworkTransform
{
protected override bool OnIsServerAuthoritative()
{
return false;
}
}

目前的解决方案,因为这个过程在今天已经不准确了,可以在文档中找到

ClientNetworkTransform#

NetworkTransform始终将位置从服务器同步到客户端,并且不允许更改客户端上的位置。GameObjects的Netcode附带了一个包含ClientNetworkTransform的示例。这种转换将所有者客户端的位置同步到服务器和所有其他客户端,从而允许客户端进行权威游戏。

ClientNetworkTransform位于Multiplayer Samples Utilities软件包中。您可以通过Unity编辑器中的package Manager窗口添加此包,方法是从Git URL中选择add并添加以下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()

作为解决方案运行,但在git 上发现了一个已关闭的问题

它又坏了还是我用错了?

相关内容

  • 没有找到相关文章

最新更新