I can instantiate a player prefab fine… but my multiplayer game allows only 2 players. How can i make it so those 2## Heading ## players have different prefabs (2 different models)?
Much appreciated.
I can instantiate a player prefab fine… but my multiplayer game allows only 2 players. How can i make it so those 2## Heading ## players have different prefabs (2 different models)?
Much appreciated.
public Transform playerModels; //attach 2 models to this from editor…
// initializing server side model...
void OnServerInitialized()
{
//for first player use model one
Network.Instantiate(playerModels[0],spawnPoint.position,spawnPoint.rotation,0);
}
//called on client side
void OnConnectedToServer()
{
//for second player use model two
Network.Instantiate(playerModels[1],spawnPoint.position,spawnPoint.rotation,0);
}
the above logic is simple use an array! thats all would do the trick.
If you want to assign randomly instead of making it as 0 on server side make it random of 2,
public Transform playerModels; //attach 2 models to this from editor…
int serverSideModelIndex=0;
// initializing server side model...
void OnServerInitialized()
{
//for first player use model one
serverSideModelIndex = Random.Range(0,2);
Network.Instantiate(playerModels[serverSideModelIndex],spawnPoint.position,spawnPoint.rotation,0);
}
//called on srever side when client connects....
void OnPlayerConnected(NetworkPlayer player)
{
networkView.RPC("ServerModelIndex", RPCMode.Others, serverSideModelIndex);
}
[RPC]
void ServerModelIndex(int i)
{
int clientSideModelIndex = i==0?1:0;
Network.Instantiate(playerModels[clientSideModelIndex],spawnPoint.position,spawnPoint.rotation,0);
}
in case it is random, you have to send which index to spawn from server to client, otherwise it can simply be done by assumption.
NOTE: The code is untested and might contain typos or syntax Errors