Problem in authority shifting of non-player object

前端 未结 1 449
借酒劲吻你
借酒劲吻你 2021-01-26 12:03

I am making a multiplayer game and I want to have player to interact with a non-player object (whose transform can be changed by any player). When I interact them with the playe

相关标签:
1条回答
  • 2021-01-26 12:58

    You need to know that the changes SHOULD be called from a player object, not the object itself, as it do not have authority.

    For setting authority you should do something like this:

    [Command]
    public void CmdSetAuth(NetworkInstanceId objectId, NetworkIdentity player)
    {
        GameObject iObject = NetworkServer.FindLocalObject(objectId);
        NetworkIdentity networkIdentity = iObject.GetComponent<NetworkIdentity>();
    
        //Checks if anyone else has authority and removes it and lastly gives the authority to the player who interacts with object
        NetworkConnection otherOwner = networkIdentity.clientAuthorityOwner;
        if (otherOwner == player.connectionToClient)
        {
            return;
        }
        else
        {
            if (otherOwner != null)
            {
                networkIdentity.RemoveClientAuthority(otherOwner);
            }
            networkIdentity.AssignClientAuthority(player.connectionToClient);
        }
    
        networkIdentity.AssignClientAuthority(player.connectionToClient);
    }
    
    0 讨论(0)
提交回复
热议问题