I’m currently working on a VR game and one of the basic interactions should be the manipulation of objects. A player should be able to grab an object and throw it and another player should be able to catch it. For this to work the player should have authority over the object he holds/interacts with. To test this out I’ve created a very basic scene but ran into some problems. I can change PlayerAuthority sometimes but most of the time the player who is also the server (host) has authority.
As a quick and dirty method I’m using triggers to change authority. When a Local Player touches the object he gets authority and can interact with the object. When he leaves the trigger authority will be revoked. This is how the trigger on my test object looks like:
The controller of the local player has the tag “VRController” and the local player himself has the tag “VRLocalPlayer”.
void OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag("VRController"))
{
if (!isServer)
{
GameObject player = GameObject.FindGameObjectWithTag("VRLocalPlayer");
player.GetComponent<VRPlayerController>().CmdSetAuth(netId, player.GetComponent<NetworkIdentity>());
}
}
}
void OnTriggerExit(Collider other)
{
if (other.gameObject.CompareTag("VRController"))
{
if (!isServer)
{
GameObject player = GameObject.FindGameObjectWithTag("VRLocalPlayer");
player.GetComponent<VRPlayerController>().CmdRemAuth(netId, player.GetComponent<NetworkIdentity>());
}
}
}
On the actual Local Player Object are the Commands:
[Command]
public void CmdSetAuth(NetworkInstanceId objectId, NetworkIdentity player)
{
var iObject = NetworkServer.FindLocalObject(objectId);
var networkIdentity = iObject.GetComponent<NetworkIdentity>();
networkIdentity.AssignClientAuthority(player.connectionToClient);
}
[Command]
public void CmdRemAuth(NetworkInstanceId objectId, NetworkIdentity player)
{
var iObject = NetworkServer.FindLocalObject(objectId);
var networkIdentity = iObject.GetComponent<NetworkIdentity>();
networkIdentity.RemoveClientAuthority(player.connectionToClient);
}
This works sometimes but only under certain conditions. Most of the time a client can see what the Host does but not vice versa. With this script enabled the host is also able to see what another client does. But when the host touches an object adn puts it back down the client can pick it up on his end but the host won’t be able to see it.
This is the first time I’m messing with Unet or multiplayer games in general, so I’m pretty sure I’m forgetting alot of things here.