Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Network Switching Levels and Keeping track of players

Discussion in 'Multiplayer' started by Deleted User, Oct 6, 2013.

  1. Deleted User

    Deleted User

    Guest

    Hey Guys,

    I've been using unity for a little while now but dont know too much about its networking aspects, Ive managed to write a simple "network manager" script following tutorials and have been wrapping my head around the concepts slowly but I am stuck on switching / loading levels on say.. when someone collides with a particular geometry?.. even after extensive google-ing I havent come across any tutorials on this either...

    this is the current script for my network manager.. any help would be really awesome.. cheers peeps!

    Code (csharp):
    1. var playerPrefab: GameObject;
    2. var spawnObject: Transform;
    3.  
    4. var gameName:String = "Virtual Museum World";
    5.  
    6. private var refreshing: boolean;
    7. private var hostData: HostData[];
    8.  
    9. var btnX:float;
    10. var btnY:float;
    11. var btnW:float;
    12. var btnH:float;
    13.  
    14. function Start(){
    15. btnX = Screen.width*0.2;
    16. btnY = Screen.height*0.2;
    17. btnW = Screen.width*0.2;
    18. btnH = Screen.width*0.05;
    19. }
    20.  
    21. function startServer(){
    22. Network.InitializeServer(32,25001, !Network.HavePublicAddress);
    23. MasterServer.RegisterHost(gameName,"Virtual Model", "Network Tutorial Environment");
    24. }
    25.  
    26. function refreshHostList(){
    27. MasterServer.RequestHostList(gameName);
    28. refreshing = true;
    29. }
    30.  
    31. function Update(){
    32. if(refreshing){
    33. if(MasterServer.PollHostList().Length >0){
    34. refreshing = false;
    35. Debug.Log(MasterServer.PollHostList().Length);
    36. hostData = MasterServer.PollHostList();
    37. }
    38. }
    39. }
    40.  
    41. function OnServerInitialized(){
    42. Debug.Log("Sever Initialized!");
    43. spawnPlayer();
    44. }
    45.  
    46. function OnConnectedToServer(){
    47. spawnPlayer();
    48. }
    49.  
    50. function spawnPlayer(){
    51. Network.Instantiate(playerPrefab, spawnObject.position, Quaternion.identity,0);
    52. }
    53.  
    54. function OnMasterServerEvent(mse:MasterServerEvent){
    55. if(mse == MasterServerEvent.RegistrationSucceeded){
    56. Debug.Log("Registered Server!");
    57. }
    58. }
    59.  
    60. function OnGUI(){
    61.  
    62. if(!Network.isClient  !Network.isServer){
    63.  
    64. if(GUI.Button(Rect(btnX,btnY,btnW,btnH), "Start Server")){
    65. Debug.Log("Starting Server!");
    66. startServer();
    67. }
    68. if(GUI.Button(Rect(btnX,btnY+100,btnW,btnH), "Refresh Host")){
    69. Debug.Log("Refreshing!");
    70. refreshHostList();
    71. }
    72. if(hostData){
    73. for(var i:int = 0; i < hostData.length; i++){
    74. if(GUI.Button(Rect(btnX*1.5+btnW,btnY+(btnH*i),btnW,btnH), hostData[i].gameName)){
    75. Network.Connect(hostData[i]);
    76. }
    77. }
    78. }
    79. }
    80. }
     
  2. foxter888

    foxter888

    Joined:
    May 3, 2010
    Posts:
    530
    this line: Network.InitializeServer(32,25001, !Network.HavePublicAddress); the last part is a function so !Network.HavePublicAdress() you forgot parentesis.

    anyhow to keep track of players if you are going to use this same script across levels you don't want it to destroy on load so you would put that in the awake function. now you would need to learn about classes and array and lists and make it so when you call on server initialized, on player connected and on connected to server to add that player.

    on the class you might want to have a networkplayer variable besides the name, the rest would be what you want for example in a fps you would put kills and death and so on.