Multi client

Options

I have a lobby system and play button . the player who makes the room should be the master client and should only start the game and spawn the other players to the scene....
Any help

Answers

  • 1)use PhotonNetwork.AutomaticallySyncScene in your awake function in your menu scene to sync the scenes of the client with the master when the game starts.
    2)The player who makes the room by default will be the master client.

  • Sidharth
    Options

    Well i tried its not working.....

  • Sidharth
    Options
    My code is

    using UnityEngine;
    using System.Collections;
    using UnityEngine.SceneManagement;


    public enum GameState
    {
    Offline,
    InLobby,
    InRoom,
    InGame,
    Die
    }


    /**
    * This class connects to the mp server and creates the main player
    */
    public class RoomManager : Photon.MonoBehaviour {

    // if you change this version number, people with old versions cannot play until update
    public string verNum = "0.1";

    public Transform[] spawnPoints;

    // Player class prefs
    public GameObject SWAT;

    public GameObject Button;
    public GameObject Fire;
    public GameObject aimin;
    public GameObject aimout;
    public GameObject Mouse;


    public InRoomChat chat;

    public string roomName;
    public string playerName;






    private GameState gameState = GameState.Offline;
    // Temperate var to reference to ragDoll to destroy
    // TODO: remove this var
    private GameObject ragDoll;

    void Start()
    {
    PlayerPrefs.DeleteAll ();
    PhotonNetwork.ConnectUsingSettings(verNum);
    roomName = "Room " + Random.Range(0, 999);
    playerName = "Player " + Random.Range(0, 20);
    gameState = GameState.InLobby;
    Debug.Log("Starting Connection");



    }

    public void spawnPlayer(GameObject playerClass)
    {


    gameState = GameState.InGame;
    GameObject.Find("_NETWORK").GetComponent<FeedManager>().addClassFeed(PhotonNetwork.playerName, playerClass.name);

    Transform randomSpawnPt = spawnPoints[Random.Range(0, spawnPoints.Length)];
    GameObject pl = PhotonNetwork.Instantiate(playerClass.name, randomSpawnPt.position, randomSpawnPt.rotation, 0) as GameObject;

    // Enable the player script
    pl.GetComponent<RigidbodyFPSController>().enabled = true;

    // Enable the camera of the player
    pl.GetComponent<RigidbodyFPSController>().fpsCam.SetActive(true);

    // Disable the graphic of the player on local
    pl.GetComponent<RigidbodyFPSController>().graphics.SetActive(false);
    Button.SetActive (true);
    Fire.SetActive (true);
    aimin.SetActive (true);
    aimout.SetActive (true);
    Mouse.SetActive (true);



    }


    void Awake(){


    PhotonNetwork.automaticallySyncScene = true;

    }




    void Update()
    {
    // update the in-room and in-game chat
    if (gameState == GameState.InRoom || gameState == GameState.InGame || gameState == GameState.Die)
    {
    chat.enabled = true;
    } else
    {
    chat.enabled = false;
    }

    }

    /**
    * Pop up room creation and selection menu
    */
    public void OnJoinedLobby()
    {
    gameState = GameState.InLobby;
    Debug.Log("Starting Server!");


    }

    /**
    * enter the game
    */
    public void OnJoinedRoom()
    {
    PhotonNetwork.playerName = playerName;
    gameState = GameState.InRoom;
    GameObject.Find("_NETWORK").GetComponent<FeedManager>().addRoomFeed(PhotonNetwork.playerName);




    }

    /**
    * Creates a player and a bot at a random spawn point
    */






    public void onDie(GameObject ragDoll)
    {
    gameState = GameState.Die;
    this.ragDoll = ragDoll;


    }



    /*
    * Renders the lobby room and player create menu
    */
    void OnGUI()
    {


    // lobby
    if (gameState == GameState.InLobby)
    {


    GUILayout.BeginArea(new Rect(Screen.width / 2 - 150, Screen.height / 2 - 150, 300, 300));

    playerName = GUI.TextField(new Rect(0,0,500,100),playerName,1000);
    roomName =GUI.TextField(new Rect(0,100,300,100),roomName,1000);
    if (GUI.Button(new Rect(0,200,300,100),"Create Room"))
    {
    PhotonNetwork.JoinOrCreateRoom(roomName, null, null);

    }


    // Display a list of available rooms on the server
    foreach (RoomInfo game in PhotonNetwork.GetRoomList())
    {


    if (GUILayout.Button(game.name + " " + game.playerCount + "/" + game.maxPlayers))
    {
    PhotonNetwork.JoinOrCreateRoom(game.name, null, null);
    }
    }

    GUILayout.EndArea();
    }


    // room
    if (gameState == GameState.InRoom)
    {

    GUILayout.BeginArea(new Rect(Screen.width / 2 - 150, Screen.height / 2 - 150, 300, 300));
    GUI.Box(new Rect(0,0,500,100),"Your current score: " + PhotonNetwork.player.GetScore());

    // Class selection


    if (PhotonNetwork.isMasterClient) {
    if (GUI.Button (new Rect (0, 200, 300, 100), "PLAY GAME")) {

    spawnPlayer (SWAT);

    }
    }




    GUILayout.Label("");
    if (GUI.Button(new Rect(0,100,300,100),"QUIT ROOM"))
    {

    PhotonNetwork.Disconnect();
    SceneManager.LoadScene(0);
    }

    GUILayout.EndArea();
    }

    if (gameState == GameState.Die)
    {


    GUILayout.BeginArea(new Rect(Screen.width / 2 - 150, Screen.height / 2 - 150, 300, 300));
    GUI.Box(new Rect(0,0,500,100),"YOU DIED");
    Button.SetActive (false);
    Fire.SetActive (false);
    aimin.SetActive (false);
    aimout.SetActive (false);
    Mouse.SetActive (false);

    if (GUI.Button(new Rect(0,200,300,100),"BACK TO ROOM"))
    {
    PhotonNetwork.Destroy(ragDoll);
    OnJoinedRoom();
    }

    GUILayout.EndArea();
    }


    }


    }
  • Sidharth
    Options
    Well in my game there is only a single scene (for lobby as well as players).
  • S_Oliver
    S_Oliver ✭✭✭
    Options
    You better start here