简体   繁体   English

Unity Mirror,客户端不执行服务器 cmd

[英]Unity Mirror, client doesn't execute server cmd

When player press key if he touched other player he try markPlayer当玩家按下键时,如果他触摸了其他玩家,他会尝试 markPlayer
When client touched host-player, log only "CmdServer" and stop, but on host-player, its full complete all methods and change color on another character;当客户端接触到主机播放器时,只记录“CmdServer”并停止,但在主机播放器上,它完全完成所有方法并在另一个角色上改变颜色;
Client code客户端代码

        //somemovementClass
        private void TriggerEntered(PlayerContext playerContext)
        {
            Debug.Log($"Collided with {playerContext.name}");
            CmdServer(playerContext);
        }

        [Command]
        public void CmdServer(PlayerContext playerContext)
        {
            Debug.Log($"CmdServer");
            playerContext.MarkPlayer();
        }
        // PlayerContext.cs

        [SyncVar(hook = nameof(OnPlayerGetDamage))]
        private bool wasDamaged; 

        [Server]
        public void MarkPlayer()
        {
            Debug.Log("On Server");
            wasDamaged = true;
        }

        private void OnPlayerGetDamage(bool oldState, bool newState)
        {
            Debug.Log("On Get Damage");
            cashedMaterial.color = Color.red;
        }

I think I solved the problem, although I'm not sure that the problem was specifically in this, but still, I didn't use the Monobehaviour class and hoped that the command would compile correctly, because usually mirror throws errors if something is wrong.我想我解决了这个问题,虽然我不确定问题出在这个问题上,但我仍然没有使用 Monobehaviour class 并希望该命令能够正确编译,因为如果出现问题,通常镜像会抛出错误. I just moved the cmd method to a separate class from networkbehaviour我刚刚将 cmd 方法从 networkbehaviour 移到了单独的 class

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

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