Skip to content

Instantly share code, notes, and snippets.

@demonixis
Last active November 11, 2016 00:29
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save demonixis/679e891b70ec2b0d0f2c to your computer and use it in GitHub Desktop.
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.
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