Пример #1
0
        /**
         * When the server receives the add player command, it creates a dummy object of the player.
         * This is necessary so the client could begin receiving RPC calls.
         * The dummy player is replaced with the actual player in SpawnPlayerAfterRoundStart()
         */
        public override void OnServerAddPlayer(NetworkConnection conn)
        {
            GameObject player = Instantiate(playerDummyPrefab);

            NetworkServer.AddPlayerForConnection(conn, player);
        }
        public override void OnServerAddPlayer(NetworkConnection conn)
        {
            // add player at correct spawn position
            //Transform start = numPlayers == 0 ? leftRacketSpawn : rightRacketSpawn;
            Debug.Log("Server adding player...");
            if (numPlayers == 0)
            {
                foreach (GameObject obj in spawnPrefabs)
                {
                    if (obj.tag == "Player")
                    {
                        player = Instantiate(obj);
                        NetworkServer.Spawn(player, conn);
                        NetworkServer.AddPlayerForConnection(conn, player);
                    }
                }
                foreach (GameObject obj in spawnPrefabs)
                {
                    if (obj.tag == "GoalKeeperPlayer")
                    {
                        player = Instantiate(obj);
                        NetworkServer.Spawn(player, conn);
                        NetworkServer.AddPlayerForConnection(conn, player);
                    }
                }
            }
            else if (numPlayers == 1)
            {
                foreach (GameObject obj in spawnPrefabs)
                {
                    if (obj.tag == "Enemy")
                    {
                        player = Instantiate(obj);
                        NetworkServer.Spawn(player, conn);
                        NetworkServer.AddPlayerForConnection(conn, player);
                    }
                }
                foreach (GameObject obj in spawnPrefabs)
                {
                    if (obj.tag == "GoalKeeperEnemy")
                    {
                        player = Instantiate(obj);
                        NetworkServer.Spawn(player, conn);
                        NetworkServer.AddPlayerForConnection(conn, player);
                    }
                }
            }

            //if(numPlayers == 0)
            //{
            //    player = Instantiate(playerPrefab, start.position, start.rotation);
            //    NetworkServer.AddPlayerForConnection(conn, player);


            //}
            //else if(numPlayers == 1)
            //{
            //    player = Instantiate(EnemyPrefab, start.position, start.rotation);
            //    NetworkServer.AddPlayerForConnection(conn, player);
            //}

            // spawn ball if reach maximum players
            //1 == 1 player , 2 == 2 players
            if (numPlayers == 1)
            {
                ball = Instantiate(spawnPrefabs.Find(prefab => prefab.tag == "Ball"));
                NetworkServer.Spawn(ball);
            }
        }