void OnCreateTank(NetworkConnection conn, CreateTankMessage message) { // playerPrefab is the one assigned in the inspector in Network // Manager but you can use different prefabs per race for example // Avoir une liste des spawnPoint available List <GameObject> spawnPointsAvailable = new List <GameObject>(); GameObject[] spawnPoints = GameObject.FindGameObjectsWithTag("SpawnPoint"); GameObject[] players = GameObject.FindGameObjectsWithTag("Player"); foreach (GameObject spawnPoint in spawnPoints) { if (SpawnUtility.SpawnPointAvailable(spawnPoint, players, 10)) { spawnPointsAvailable.Add(spawnPoint); } } // Je spawne le player si il reste des spawn Points available if (spawnPointsAvailable.Count > 0) { GameObject selectedSpawnPoint = spawnPointsAvailable[Random.Range(0, spawnPointsAvailable.Count)]; GameObject newPlayer = Instantiate(playerPrefab, selectedSpawnPoint.transform.position, Quaternion.identity); // Apply data from the message however appropriate for your game // Typically Player would be a component you write with syncvars or properties // call this to use this gameobject as the primary controller NetworkServer.AddPlayerForConnection(conn, newPlayer); } }
public void OnTankConnected(NetworkConnection conn, CreateTankMessage msg) { Debug.Log("CONNECTED"); GameObject obj = Instantiate(playerPrefab); obj.GetComponent <TankMovement>().m_PlayerNumber = msg.id + 1; NetworkServer.AddPlayerForConnection(conn, obj); }
/// <summary> /// Called on the client when connected to a server. /// <para>The default implementation of this function sets the client as ready and adds a player. Override the function to dictate what happens when the client connects.</para> /// </summary> /// <param name="conn">Connection to the server.</param> public override void OnClientConnect(NetworkConnection conn) { base.OnClientConnect(conn); // you can send the message here, or wherever else you want CreateTankMessage tankMessage = new CreateTankMessage { name = "MyTank", tankColor = Color.red, tankModel = TankModel.T90 }; conn.Send(tankMessage); }
public override void OnClientConnect(NetworkConnection conn) { base.OnClientConnect(conn); if (connectedPlayers == 0) { playerOneConn = conn; CreateTankMessage msg = new CreateTankMessage { id = connectedPlayers }; connectedPlayers++; conn.Send(msg); } else if (connectedPlayers == 1) { playerTwoConn = conn; CreateTankMessage msg = new CreateTankMessage { id = connectedPlayers }; connectedPlayers++; conn.Send(msg); } }