【问题标题】:Players in network game both spawn as client or server网络游戏中的玩家都作为客户端或服务器生成
【发布时间】:2019-11-21 01:39:17
【问题描述】:

我正在使用 UNET 开发多人游戏。每次我通过在编辑器中玩游戏来主持比赛时,加入的每个玩家都被视为服务器并且游戏按预期运行。如果我使用内置游戏(不使用 Unity 编辑器)主持比赛,加入的每个玩家都被视为客户端,并且他们都在相同的网络生成位置生成,并且每次我的敌人生成器尝试实例化敌人时预制件,我收到一条错误消息。

我真的不明白发生了什么。

我尝试过在线学习其他教程,但没有任何帮助。 我将在下面列出代码和错误。

//为了检查播放器是客户端还是服务器,我在//播放器设置脚本中编写了这段代码:

"if (isServer)
  Debug.Log("server");
 else
 Debug.Log("Client");"

//此代码用于播放器脚本中,以在被敌人子弹击中时受到伤害

private void OnTriggerEnter2D(Collider2D hitInfo)
    {
        if (hitInfo.tag == "Enemy" || hitInfo.tag == "EnemyBullet")
        {
            RpcTakeDamage(10, "hit by enemy");
        }
    }

[ClientRpc]
    public void RpcTakeDamage(int _amount, string _sourceID)
    {
        if (!isHit)
        {
            StartCoroutine("HurtColor");
            currentHealth -= _amount;

    Debug.Log(transform.name + " now has " + currentHealth + " health ");

            if (currentHealth <= 0)
            {
                Die(_sourceID);
            }
        }

    }

//到目前为止的Die函数只是销毁玩家游戏对象

//当我加入内置游戏托管的游戏时会出现这些错误

"Network configuration mismatch detected. The number of networked scripts on the client does not match the number of networked scripts on the server. This could be caused by lazy loading of scripts on the client. This warning can be disabled by the checkbox in NetworkManager Script CRC Check."

"CRC Local Dump PlayerSetup : 0"

"Trying to send command for object without authority."

//每当我的敌人生成器尝试生成一个敌人时,当 //game 由构建的游戏托管并且每个玩家都被视为客户端时,就会发生这种情况 //敌人预制件仍然会按预期生成并移动

"SpawnObject for EnemyUpRight(Clone) (UnityEngine.GameObject), NetworkServer is not active. Cannot spawn objects without an active server." 

//当敌人的子弹击中玩家时,会出现错误而不是受到伤害

"RPC Function RpcTakeDamage called on client."

【问题讨论】:

    标签: c# unity3d networking unity3d-unet


    【解决方案1】:

    你的问题总结是

    “试图为未经授权的对象发送命令。”

    您的大部分错误可能来自您的生成器。您的生成器不是您的服务器播放器,在 u-net 场景中唯一具有权限的元素是服务器播放器。

    所以你有两个选择:

    1. 尝试将生成功能放在播放器上(顺便说一下,这应该是 [Command] 功能!)。我建议您阅读我的回答:Instantiated prefab scale is not displayed correctly on client

    2.现场切换权威,我建议另一个答案更好地理解:Problem in authority shifting of non-player object

    希望对你有帮助!

    【讨论】:

    • 在收到您的回答之前我做了一些更改:尽管显示了错误消息,但敌怪生成器仍然生成敌怪。仍然存在一个问题,当我从内置游戏开始比赛时,玩家会在相同的位置产生,我明白为什么。基本上,所有网络生成位置都没有放置在场景中,它们是在我分配给游戏对象的脚本中使用 OnStartServer 函数生成的。这意味着永远不会调用函数 OnStartServer,因为服务器没有启动!问题不在于当局(我认为),而是服务器没有启动。
    • 然后尝试更新你的答案,因为我在试图理解“敌人生成器仍然生成敌人”和“他们使用 OnStartServer 生成......服务器没有启动! "给我更多的代码或截图,我会试着弄清楚:)
    • 我可以给你GitHub上项目的链接github.com/Ardoriccardo00/Touhou99.git代码可能很乱
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-09-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多