非玩家对象的权限转移问题 [英] Problem in authority shifting of non-player object
问题描述
我正在制作多人游戏,我想让玩家与非玩家对象互动(其变换可以由任何玩家更改).当我与第一个加入游戏的玩家(或主持该游戏的人)互动时,但如果我尝试与另一位玩家(第二参与的玩家)互动,则对象会回到第一个玩家离开的位置他在.
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 player who joined first (or the guy who is hosting) its working but if I try to interact it with the another player (the one who joined second) the objects goes back to the location that the first player left him at.
所以我尝试的是转移非玩家对象的权限,但是我遇到以下错误. 是否有人遇到相同的问题或知道其他方法可以完成上述任务?我正在使用以下代码来更改权限:
So what I tried is to shift the authority of non-player object but I am having the following errors. Anyone is having the same issue or knows any other way to do the above task? I am using the following code to change the authority:
[Command]
void Cmd_AssignLocalAuthority(GameObject obj)
{
print("shifting authority successfully");
NetworkInstanceId nIns = obj.GetComponent<NetworkIdentity>().netId;
GameObject client = NetworkServer.FindLocalObject(nIns);
NetworkIdentity ni = client.GetComponent<NetworkIdentity>();
ni.AssignClientAuthority(connectionToClient);
}
[Command]
void Cmd_RemoveLocalAuthority(GameObject obj)
{
print("reverting authority successfully");
NetworkInstanceId nIns = obj.GetComponent<NetworkIdentity>().netId;
GameObject client = NetworkServer.FindLocalObject(nIns);
NetworkIdentity ni = client.GetComponent<NetworkIdentity>();
ni.RemoveClientAuthority(ni.clientAuthorityOwner);
}
我得到的错误是这个
推荐答案
您应该知道应该从玩家对象而不是对象本身调用更改,因为它没有权限.
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);
}
这篇关于非玩家对象的权限转移问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!