Search code examples
unity-game-enginerandommultiplayerunity-networking

Randomizing players depending on Tag in Multiplayer Network Unity


How to do i randomize players depending on its tag in Multiplayer (Using UNITY Networking Lobby Asset)? for example i spawned players with tag "Flee" then i want to randomize to determine among them who will be the "Chaser". So if there are 5 players, 4 of them should have "Flee" tag and the other one should be "chaser". I've been trying to do this but what happens is in every client, they randomize numbers so sometimes Client A will be tagged as "Chaser" in his view and in other view, he's tagged as "Flee". So basically what happens is it randomizes in every client, but what i want is to randomize in the server only to determine the "Chaser" tag. Help would be greatly appreciated. Thank you!


Solution

  • You need overide function OnServerAddPlayer NetworkManager or add new script to control NetworkManager OnServerAddPlayer .

    I was use overide NetworkManger and i call it CNetworkManger then in inspector i add 2 object attached NetworkIdentity into player prefabs.

    public override void OnServerAddPlayer (NetworkConnection conn, short playerControllerId) {
                var player = (GameObject)GameObject.Instantiate(spawnPrefabs[{{ YOUR TAG INDEX }}], 
                    Vector3.zero, 
                    Quaternion.identity);
            }