- Home /
Multiplayer server controls all players
I am coding for a game that has single and multiplayer. When I host a multiplayer server and use another window of the build to act as a client, the host controls both the server and client player prefabs. Below is the input controller that moves the players;
void OnConnectedToServer()
{
if(!networkView.isMine)
{enabled = false;}
}
void Start ()
{
//If in multiplayer, make sure only the right net player has control
if((Network.peerType == NetworkPeerType.Client || Network.peerType == NetworkPeerType.Server))
{
if(networkView.isMine)
{
SetUpPlayerCamera();
enabled = true;
}
else
{
enabled = false;
}
}
//Otherwise if in SP, give control
else
{
SetUpPlayerCamera();
}
}
void OnNetworkInstantiate(NetworkMessageInfo msg)
{
if(!networkView.isMine)
{enabled = false;}
}
void Update ()
{
//If in multiplayer, make sure the right player controls this
if((Network.peerType == NetworkPeerType.Client || Network.peerType == NetworkPeerType.Server))
{
if(networkView.isMine)
{ InputControl();}
}
else
{
InputControl();
}
}
I used the networkView.isMine method which is in every other answer but for me the scripts are always enabled for the host and not the client, or any client for that matter.
What am I doing wrong? Help would be appreciated.
I don't really see what you're trying to do there in your code, but it looks like you have the right idea. I'm also not sure why you bother to check if the network peer type is either client or server; why check at all?
What I like to do is destroy components that aren't going to be used on peer machines; this way that component only exists on the controlling client machine. In that case I have a special class for serializing information across the network which sends if it networkView.is$$anonymous$$ine is true and receives when it isn't.
I see no movement scripts here, but an easy way would be to have the an if(networkView.is$$anonymous$$ine) { //movement script here }