- Home /
Why can't the client move a networked object ?
Hello there my future saviors ! I am working on a multiplayer 2D chess game with UNET, but unfortunately got stuck. So, in my game one of the player is the Server as well as the Client at the same time. My idea is to instantiate all the objects, such as the Tiles and Pieces on the Server, and then use NetworkServer.Spawn to propagate the objects on the Clients, so that both of them can see what's going on. However, when it comes to moving the pieces from the Client which is not the Server, the designated object does not move, despite that it recognizes the piece and all, whereas on the other Client-Server moving the same piece works perfectly.
Here is the how I call the functions which create my objects on the Server :
void Start()
{
if (isServer && isLocalPlayer)
{
CreateBoard ();
SetUpPieces ();
Debug.Log (" SERVER ");
}
}
This is the Command function which moves the piece :
[Command]
public void CmdmoveBish(int x , int y)
{
Debug.Log ("Command the server to move bishop : ");
bishop.transform.position = new Vector2 (x,y);
}
I am sure that the command function is executed from both Clients, because I get the message in the console. Btw, I have noticed that when the non-Server-Client tries to move the piece it changes the coordinates of the piece prefab, instead of the object which was instantiated in the game. It sound like there's a simple solution but I've become very confused. Additional information; this is how I instantiate my piece :
public void SetUpPieces()
{
bishop = Instantiate (bishop); // bishop is a GameObject
bishop.transform.position = new Vector2 (2,0);
NetworkServer.Spawn (bishop);
Debug.Log ("bishop is spawned");
}
I appreciate your help and thank you in advance :)
Answer by finlay_morrison · Apr 18, 2018 at 12:58 PM
Inside the command function you need to call another function as an rpc call instead of a command, then from that transform the pieces.
And a little explanation of what an rpc call is, they can only be called by the server (on a function with a command modifier, and that code will be called for all connected clients.
[Command]
public void CmdmoveBish(int x , int y)
{
Debug.Log ("Command the server to move bishop : ");
RpcActmove (x, y);
}
[ClientRpc]
public void RpcActmove(int a, int b)
{
SubBishop.transform.position = new Vector2 (a,b);
Debug.Log ("RPC executed");
}
I've tried and it does not change anything.
The object whose command function you are calling must be the client's player object or an object who has client authority. Read the documentation here. If this is the problem, you should see some warning log messages on the client when calling the command function.
Yes, I understand what you're saying. But my question and problem is, how can both clients move one object that's on the server? It seems like there's no way to spawn an object with client-authority to both clients.