为什么我要求Cmd函数时调用Rpc函数?

问题描述 投票:0回答:1

这是我的问题,当我以客户端身份运行此代码时,

私有IEnumerator Start()

    {
        if (isServer)
        {
            Debug.Log("je suis le serveur");
            RpcAllowTeleportation();
        }
        else
        {
            Debug.Log("Je suis le client");
            CmdAllowTeleportations();
        }
    }

    [Command]
    public void CmdAllowTeleportations()
    {
        Debug.Log("C'est passé par la fonction CmdAllowTeleportation");
        if (!isLocalPlayer) { Teleportation.GetComponent<Teleport>().enabled = false; Debug.Log("cmd : it hasn't the authority"); }
        else { Debug.Log("cmd : It has the authority"); }
    }

    [ClientRpc]
    void RpcAllowTeleportation()
    {

        Debug.Log("C'est passé par la fonction RpcAllowTeleportation");
        if (!hasAuthority) { Teleportation.GetComponent<Teleport>().enabled = false; Debug.Log("rpc : it hasn't the authority"); }
        else { Debug.Log("rpc : It has the authority"); }
    } ##

我有这些日志:

Mes logs

我的日志与代码完全不一致!您能帮我吗?

谢谢

c# unity3d client multiplayer
1个回答
0
投票

因为它在Start中被称为被服务器,所以它调用了

RpcAllowTeleportation();

因此在所有客户端上(因此您)RpcAllowTeleportation被执行。

反之亦然,作为客户端,您永远不会看到来自CmdAllowTeleportations的日志,因为它是仅在服务器上执行的[Command]&rightarrow;。服务器将看到CmdAllowTeleportations的日志。

© www.soinside.com 2019 - 2024. All rights reserved.