- Home /
The question is answered, right answer was accepted
Multiplayer problem
Hi,
I make a multiplayer tank fighting game with the new Unity5 network features. In general my multiplayer setup is working now. I have the problem, that I see my host shoots and detonations in both, client and host, but not the client shoots and detonations. This are only visible on the client side.
I know I have to sync it, but the documentation not really complete yet and I don't know when to use [ClientBehavior] [Command] and [Syncvar]
I would be happy about a little help. Thanks. Frank
Answer by n1gth · Jul 02, 2015 at 08:41 PM
You need to do almost everything on the server. Although a client or human player might decide to shoot, it's the decision that happens on the client, not the actual firing. The client commands the server to do it. You can use the Command for this, eg:
[Client] // runs only on client
public void Shoot(Vector3 target)
{
CmdShoot(target, identity);
}
[Command] // runs only on server
private void CmdShoot(Vector3 target)
{
var obj = (GameObject)Instantiate(TankShellPrefab, MyTranform, someRotation);
NetworkServer.Spawn(obj);
// etc
RpcTankEventHappened("shot");
}
[ClientRpc] // runs only on client
public void RpcTankEventHappened(string something)
{
if (something.Equals("shot")) {
// decrease on-screen ammo count
}
}
Note that for this to work you need be in a NetworkBehavior, and have the NetworkIdentity and NetworkTransform on the Prefab, but I think you have these already.
See the docs for the ClientRPC call for more details of server -> client high-level messaging, for informing the client that something's happened (like damage).
thanks for your answer. Looks good, I will check it this evening. But it helps really to understand the commands.
Follow this Question
Related Questions
Unity networking tutorial? 6 Answers
Networking sending info only one direction 0 Answers
Requesting data from server 1 Answer
Using AssignClientAuthority 0 Answers
Can a person be both a client and a server? (and more) 0 Answers