0
\$\begingroup\$

I'm doing a cooperation FPS game in which players can push big rigidbody cubes with their rigidbody characters (the one from the standard assets).

The problem is that the player hosting the game has authority on these cubes positions and so when the client tries to push them, he really struggles and the result is 100% disappointing.

So i'm searching a way to pass authority from the server to client and vice versa when the cube is touched by a player.

I'm using Unity 2018.2

Have a nice day.

\$\endgroup\$
  • \$\begingroup\$ Thanks for sharing your knowledge! However, could you word this question part as a question, and move this answer to an answer just below? Asking and answering your own questions is perfectly fine and encouraged, but the site format is to keep them separate, and write the question as though you didn't know the answer yet, like for example in this question and answer from the same user. Let me know if you need any help editing. :) \$\endgroup\$ – Anko Dec 7 '18 at 18:23
0
\$\begingroup\$

And so here's the script I used to solve this problem. However, if both client and host are sending commands through the object, you'll have a problem when the client has authority. I'll try to solve this later.

public class CubeAuthority : NetworkBehaviour {

private void OnCollisionEnter(Collision collision)
{

    if (collision.transform.tag == "Player" && isServer)
    {
        CmdAssignNetworkAuthority(GetComponent<NetworkIdentity>(), collision.transform.GetComponent<NetworkIdentity>());
    }
}

[Command]
public void CmdAssignNetworkAuthority(NetworkIdentity cubeId, NetworkIdentity clientId)
{
    //If -> cube has a owner && owner isn't the actual owner
    if (cubeId.clientAuthorityOwner != null && cubeId.clientAuthorityOwner != clientId.connectionToClient)
    {
        // Remove authority
        cubeId.RemoveClientAuthority(cubeId.clientAuthorityOwner);
    }

    //If -> cube has no owner
    if (cubeId.clientAuthorityOwner == null)
    {
        // Add client as owner
        cubeId.AssignClientAuthority(clientId.connectionToClient);
    }
}
}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.