Skip to content

Instantly share code, notes, and snippets.

@demonixis
Last active Nov 11, 2016
Embed
What would you like to do?
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.
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