Last active
November 11, 2016 00:29
-
-
Save demonixis/679e891b70ec2b0d0f2c to your computer and use it in GitHub Desktop.
LevelManager script used in my tutorial about Networking with Unity (in French @ http://www.demonixis.net/blog). It's used for spawing players in the level.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System.Collections; | |
public class LevelManager : MonoBehaviour | |
{ | |
public CameraFollow playerCam; | |
public GameObject playerPrefab; | |
public SpawnPoint[] spawnPoints; | |
void Start () | |
{ | |
if (Network.isServer) | |
SpawnPlayer(); | |
else | |
Network.Connect(NetworkManager.GameToJoin); | |
} | |
void OnConnectedToServer() | |
{ | |
Debug.Log ("Un nouveau joueur s'est connecté !"); | |
SpawnPlayer(); | |
} | |
private void SpawnPlayer() | |
{ | |
int index = 0; | |
if (NetworkManager.GameToJoin != null) | |
index = NetworkManager.GameToJoin.connectedPlayers; | |
Debug.Log (index); | |
// Attention ici on utilise Network.Instanciate et pas Object.Instanciate. | |
var player = Network.Instantiate(playerPrefab, spawnPoints[index].transform.position, spawnPoints[index].transform.rotation, 0) as GameObject; | |
playerCam.transform.position = spawnPoints[index].transform.position - new Vector3(0, 0, -10); | |
playerCam.transform.rotation = spawnPoints[index].transform.rotation; | |
// Mise à jour du script de caméra | |
playerCam.target = player.transform; | |
playerCam.enabled = true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment