【问题标题】:Can't execute server command from client无法从客户端执行服务器命令
【发布时间】:2017-01-24 16:01:30
【问题描述】:

我试图在玩家点击屏幕时生成子弹。当我在配对中创建服务器时,我可以生成子弹,我也可以在客户端看到它们。但是当我在配对时像客户端一样连接到服务器时,我无法生成子弹。我在服务器端收到此错误:

在 Player(Clone) >(UnityEngine.GameObject) 上未发现传入 [Command:InvokeCmdCmd_Fire] 的行为,服务器和客户端应具有相同的 NetworkBehaviour 实例 [netId=2]。
UnityEngine.Networking.NetworkIdentity:UNetStaticUpdate()

我的代码:

for (int x = 0; x < Input.touchCount; x++) 
        {
            touchpos = Camera.main.ScreenToWorldPoint(touch[x].position);
            if ((touchpos.x > -4.5f || touchpos.y > -1.2f))
            {
               pos = transform.position;

               if (magazine > 0)
                {
                    if (time > firetime && autoriffle)
                    {
                        Cmd_Fire();

                        time = 0;
                        magazine--;

                    }

        time += Time.deltaTime;

    }

    [Command]
    void Cmd_Fire()
    {      
        GameObject bullet = Instantiate(bulet, pos, Quaternion.FromToRotation(Vector3.up, new Vector3(touchpos.x, touchpos.y, transform.position.z) - transform.position)) as GameObject;

        bullet.GetComponent<Rigidbody>().AddForce(bullet.transform.up * bulletspeed, ForceMode.Impulse);

        NetworkServer.Spawn(bullet);

        // I try NetworkServer.SpawnWithClientAuthority(bullet, connectionToClient); too but same reason
    }

我的播放器预制件上有这个脚本。我添加了播放器和子弹预制网络身份和网络转换。在播放器上,我检查了本地播放器权限。

我还尝试创建一个新项目,将官方 Unity Multiplayer Networking 教程中的所有内容都放入其中,但也没有成功。

感谢您的帮助。

【问题讨论】:

    标签: c# unity3d unity5


    【解决方案1】:

    我想通了。如果它没有附加到本地播放器,我正在销毁这个脚本。我应该只做-

    if(!isLocalPlayer)
       return;`
    

    不是

    if(!isLocalPlayer)
    {
       Destroy(this);
       return;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-03-15
      • 2018-01-31
      • 1970-01-01
      • 1970-01-01
      • 2010-10-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多