简体   繁体   English

统一/镜像网络没有客户端同步

[英]Unity/Mirror networking no client sync

Goal: Object interact-able by both players and its position and rotation is synced(LAN).目标: 两个玩家都可以交互的对象,并且它的位置和旋转是同步的(局域网)。

Scene setup : simple scene similar to pong example in mirror,when both players are spawned a cube is spawned that is interact-able by both players with sync.场景设置:简单的场景类似于镜子中的乒乓球示例,当两个玩家都生成时,生成一个立方体,两个玩家可以同步进行交互。

Test cube - Registered as spawn-able prefab in network manager with network identity/network transform and spawned when both players join and simple rotation script that moves it when "z" or "x" is pressed,测试立方体 - 在网络管理器中注册为可生成的预制件,具有网络身份/网络转换,并在两个玩家加入时生成,并在按下“z”或“x”时移动它的简单旋转脚本,

public Transform LeftPlayerSpawn;
public Transform RightPlayertSpawn;
GameObject Testcube;

public override void OnServerAddPlayer(NetworkConnection conn)
{
    Transform start = numPlayers == 0 ? LeftPlayerSpawn : RightPlayertSpawn;
    GameObject player = Instantiate(playerPrefab, start.position, start.rotation);
    NetworkServer.AddPlayerForConnection(conn, player);

    if (numPlayers == 2)
    {
        Testcube = Instantiate(spawnPrefabs.Find(prefab => prefab.name == "TestCube"));
        NetworkServer.Spawn(Testcube);
    }
}

Problem : Any change made in server is reflected in clients,and local player has control over the object but not synced with other client.问题:服务器中所做的任何更改都会反映在客户端中,并且本地玩家可以控制对象但不与其他客户端同步。 Video of No-Sync不同步视频

Question : What is the purpose of the bool- client authority in Networktransform component,should it automatically sync that particular transform when client with authority moves it or does it allow user to sync manually through scripts?问题:Networktransform 组件中 bool-client 权限的目的是什么,它应该在具有权限的客户端移动时自动同步该特定转换还是允许用户通过脚本手动同步?

From Mirror docs - "By default, Network Transform is server-authoritative unless you check the box for Client Authority. Client Authority applies to player objects as well as non-player objects that have been specifically assigned to a client, but only for this component. With this enabled, position changes are send from the client to the server".来自镜像文档 - “默认情况下,网络转换是服务器权威的,除非您选中客户端权限框。客户端权限适用于玩家对象以及已专门分配给客户端的非玩家对象,但仅适用于此组件. 启用此功能后,位置更改将从客户端发送到服务器”。

Also tried to update position and rotation through [Command]/[CientRpc] through SyncPosRot script attached to the test cube,还尝试通过附加到测试立方体的 SyncPosRot 脚本通过 [Command]/[CientRpc] 更新位置和旋转,

void Update()
{
    if (isLocalPlayer) 
    {
        CmdSyncPosRot(transform.localPosition, transform.localRotation);
    } 
}

[Command]
void CmdSyncPosRot(Vector3 localPosition, Quaternion localRotation)
{
    RpcSyncPosRot(localPosition, localRotation);
}

[ClientRpc]
void RpcSyncPosRot(Vector3 localPosition, Quaternion localRotation)
{
    if (!isLocalPlayer) 
    {
        transform.localPosition = localPosition;
        transform.localRotation = localRotation;
    }
}

Tried and failed :尝试过但失败了:

  1. Tried with and without SyncPosRot script,尝试使用和不使用 SyncPosRot 脚本,
  2. Tried with and without bool- client authority in network transform component,在网络转换组件中尝试使用和不使用 bool-client 权限,
  3. Moving the network identity/network transform to top and bottom of the inspector as this seemed to fix sync for others who had similar issue(not for me),将网络身份/网络转换移动到检查器的顶部和底部,因为这似乎为其他有类似问题的人(不是我)解决了同步问题,

All the above after multiple builds and no results,以上都是多次构建后没有结果,

If the fix is something simple as spawning object from server - on which all players have ownership over can anyone please guide me?如果修复很简单,比如从服务器生成对象 - 所有玩家都拥有所有权,有人可以指导我吗?

Also if there is an easier way(need not be a better way/best practice) please let me know.另外,如果有更简单的方法(不必是更好的方法/最佳实践),请告诉我。

Try this:尝试这个:

if (isServer)
{
    RpcSyncPosRot(transform.localPosition, transform.localRotation);
}
else
{
    CmdSyncPosRot(transform.localPosition, transform.localRotation);
}

Did not add owner when spawning Test cube :生成测试立方体时未添加所有者:

    if (numPlayers == 0)
    {
        player1 = Instantiate(playerPrefab, LeftPlayerSpawn.position, LeftPlayerSpawn.rotation);
        NetworkServer.AddPlayerForConnection(conn, player1);
        spawn1 = true;
    }
    if (numPlayers == 1)
    {
        player2 = Instantiate(playerPrefab, RightPlayertSpawn.position, RightPlayertSpawn.rotation);
        NetworkServer.AddPlayerForConnection(conn, player2);
        spawn2 = true;
    }
    if (spawn1 && spawn2)
    {
        Testcube = Instantiate(spawnPrefabs.Find(prefab => prefab.name == "TestCube"));
        NetworkServer.Spawn(Testcube,player1);
    }

This Syncs rotation and position from player1 input but not player2.这同步了玩家 1 输入但不是玩家 2 的旋转和位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM